Updates translations
[lxde/liblxqt.git] / lxqttranslator.cpp
CommitLineData
949dba2f
AS
1#include "lxqttranslator.h"
2#include <QTranslator>
3#include <QLocale>
4#include <QDebug>
5#include <QCoreApplication>
6#include <QLibraryInfo>
7#include <QStringList>
949dba2f
AS
8#include <QFileInfo>
9
e6389bfc 10#include <XdgDirs>
afd3b116 11
949dba2f
AS
12using namespace LxQt;
13
14/************************************************
15
16 ************************************************/
17QStringList *getSearchPaths()
18{
19 static QStringList *searchPath = 0;
20
21 if (searchPath == 0)
22 {
23 searchPath = new QStringList();
24 *searchPath << XdgDirs::dataHome(false) + "/lxqt/translations";
25 *searchPath << XdgDirs::dataDirs("/lxqt/translations");
26 *searchPath << "/usr/local/share/lxqt/translations";
27 *searchPath << "/usr/share/lxqt/translations";
28 }
29
30 return searchPath;
31}
32
33
34/************************************************
35
36 ************************************************/
37QStringList LxQt::Translator::translationSearchPaths()
38{
39 return *(getSearchPaths());
40}
41
42
43/************************************************
44
45 ************************************************/
46void Translator::setTranslationSearchPaths(const QStringList &paths)
47{
48 QStringList *p = getSearchPaths();
49 p->clear();
50 *p << paths;
51}
52
53
54/************************************************
55
56 ************************************************/
57bool translate(const QString &name)
58{
59 QString locale = QLocale::system().name();
498999ee 60 QTranslator *appTranslator = new QTranslator(qApp);
949dba2f
AS
61
62 QStringList *paths = getSearchPaths();
63 foreach(QString path, *paths)
64 {
637aedef 65 if (appTranslator->load(name + "_" + locale, path))
949dba2f
AS
66 {
67 QCoreApplication::installTranslator(appTranslator);
637aedef 68 return true;
949dba2f
AS
69 }
70 }
eb381449
LP
71
72 // If we got here, no translation was loaded. appTranslator has no use.
73 delete appTranslator;
949dba2f
AS
74 return false;
75}
76
77
78/************************************************
79
80 ************************************************/
81bool Translator::translateApplication(const QString &applicationName)
82{
83 QString locale = QLocale::system().name();
84 QTranslator *qtTranslator = new QTranslator(qApp);
949dba2f 85
c289112e
LP
86 if (qtTranslator->load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
87 {
88 qApp->installTranslator(qtTranslator);
89 }
90 else
91 {
92 delete qtTranslator;
93 qWarning("LxQt::Translator: Qt translations not found");
94 }
949dba2f
AS
95
96 if (!applicationName.isEmpty())
97 return translate(applicationName);
98 else
99 return translate(QFileInfo(QCoreApplication::applicationFilePath()).baseName());
100}
101
102
103/************************************************
104
105 ************************************************/
106bool Translator::translateLibrary(const QString &libraryName)
107{
108 static QSet<QString> loadedLibs;
109
110 if (loadedLibs.contains(libraryName))
111 return true;
112
113 loadedLibs.insert(libraryName);
114
115 return translate(libraryName);
116}