Updates translations installation and search paths
[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>
51a92980 8#include <QStringBuilder>
949dba2f
AS
9#include <QFileInfo>
10
e6389bfc 11#include <XdgDirs>
afd3b116 12
949dba2f
AS
13using namespace LxQt;
14
15/************************************************
16
17 ************************************************/
18QStringList *getSearchPaths()
19{
20 static QStringList *searchPath = 0;
21
22 if (searchPath == 0)
23 {
24 searchPath = new QStringList();
51a92980
LP
25 *searchPath << QString(LXQT_SHARE_TRANSLATIONS_DIR);
26 *searchPath << XdgDirs::dataDirs(LXQT_RELATIVE_SHARE_TRANSLATIONS_DIR);
27 searchPath->removeDuplicates();
949dba2f
AS
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 {
51a92980
LP
65 QStringList subPaths;
66 subPaths << path % QChar('/') % name;
67 subPaths << path;
68
69 foreach(QString p, subPaths)
1e972434 70 {
51a92980
LP
71 if (appTranslator->load(name + "_" + locale, p))
72 {
73 QCoreApplication::installTranslator(appTranslator);
74 return true;
75 }
76 else if (locale == QLatin1String("C") ||
77 locale.startsWith(QLatin1String("en")))
78 {
79 // English is the default. Even if there isn't an translation
80 // file, we return true. It's translated anyway.
81 delete appTranslator;
82 return true;
83 }
1e972434 84 }
949dba2f 85 }
eb381449
LP
86
87 // If we got here, no translation was loaded. appTranslator has no use.
88 delete appTranslator;
949dba2f
AS
89 return false;
90}
91
92
93/************************************************
94
95 ************************************************/
96bool Translator::translateApplication(const QString &applicationName)
97{
98 QString locale = QLocale::system().name();
99 QTranslator *qtTranslator = new QTranslator(qApp);
949dba2f 100
c289112e
LP
101 if (qtTranslator->load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
102 {
103 qApp->installTranslator(qtTranslator);
104 }
105 else
106 {
107 delete qtTranslator;
108 qWarning("LxQt::Translator: Qt translations not found");
109 }
949dba2f
AS
110
111 if (!applicationName.isEmpty())
112 return translate(applicationName);
113 else
114 return translate(QFileInfo(QCoreApplication::applicationFilePath()).baseName());
115}
116
117
118/************************************************
119
120 ************************************************/
121bool Translator::translateLibrary(const QString &libraryName)
122{
123 static QSet<QString> loadedLibs;
124
125 if (loadedLibs.contains(libraryName))
126 return true;
127
128 loadedLibs.insert(libraryName);
129
130 return translate(libraryName);
131}