Removes misleading "Qt translations not found!" warning
[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
12dc4c95 15bool translate(const QString &name, const QString &owner = QString());
949dba2f
AS
16/************************************************
17
18 ************************************************/
19QStringList *getSearchPaths()
20{
21 static QStringList *searchPath = 0;
22
23 if (searchPath == 0)
24 {
25 searchPath = new QStringList();
51a92980
LP
26 *searchPath << QString(LXQT_SHARE_TRANSLATIONS_DIR);
27 *searchPath << XdgDirs::dataDirs(LXQT_RELATIVE_SHARE_TRANSLATIONS_DIR);
28 searchPath->removeDuplicates();
949dba2f
AS
29 }
30
31 return searchPath;
32}
33
34
35/************************************************
36
37 ************************************************/
38QStringList LxQt::Translator::translationSearchPaths()
39{
40 return *(getSearchPaths());
41}
42
43
44/************************************************
45
46 ************************************************/
47void Translator::setTranslationSearchPaths(const QStringList &paths)
48{
49 QStringList *p = getSearchPaths();
50 p->clear();
51 *p << paths;
52}
53
54
55/************************************************
56
57 ************************************************/
12dc4c95 58bool translate(const QString &name, const QString &owner)
949dba2f
AS
59{
60 QString locale = QLocale::system().name();
498999ee 61 QTranslator *appTranslator = new QTranslator(qApp);
949dba2f
AS
62
63 QStringList *paths = getSearchPaths();
64 foreach(QString path, *paths)
65 {
51a92980 66 QStringList subPaths;
12dc4c95
LP
67
68 if (!owner.isEmpty())
69 {
70 subPaths << path % QChar('/') % owner % QChar('/') % name;
71 }
72 else
73 {
74 subPaths << path % QChar('/') % name;
75 subPaths << path;
76 }
51a92980
LP
77
78 foreach(QString p, subPaths)
1e972434 79 {
51a92980
LP
80 if (appTranslator->load(name + "_" + locale, p))
81 {
82 QCoreApplication::installTranslator(appTranslator);
83 return true;
84 }
85 else if (locale == QLatin1String("C") ||
86 locale.startsWith(QLatin1String("en")))
87 {
88 // English is the default. Even if there isn't an translation
89 // file, we return true. It's translated anyway.
90 delete appTranslator;
91 return true;
92 }
1e972434 93 }
949dba2f 94 }
eb381449
LP
95
96 // If we got here, no translation was loaded. appTranslator has no use.
97 delete appTranslator;
949dba2f
AS
98 return false;
99}
100
101
102/************************************************
103
104 ************************************************/
105bool Translator::translateApplication(const QString &applicationName)
106{
107 QString locale = QLocale::system().name();
108 QTranslator *qtTranslator = new QTranslator(qApp);
949dba2f 109
c289112e
LP
110 if (qtTranslator->load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
111 {
112 qApp->installTranslator(qtTranslator);
113 }
114 else
115 {
116 delete qtTranslator;
c289112e 117 }
949dba2f
AS
118
119 if (!applicationName.isEmpty())
120 return translate(applicationName);
121 else
122 return translate(QFileInfo(QCoreApplication::applicationFilePath()).baseName());
123}
124
125
126/************************************************
127
128 ************************************************/
129bool Translator::translateLibrary(const QString &libraryName)
130{
131 static QSet<QString> loadedLibs;
132
133 if (loadedLibs.contains(libraryName))
134 return true;
135
136 loadedLibs.insert(libraryName);
137
138 return translate(libraryName);
139}
172bde85 140
12dc4c95
LP
141bool Translator::translatePlugin(const QString &pluginName, const QString& type)
142{
143 static QSet<QString> loadedPlugins;
144
145 QString fullName = type % QChar('/') % pluginName;
146 if (loadedPlugins.contains(fullName))
147 return true;
148
149 loadedPlugins.insert(pluginName);
150 return translate(pluginName, type);
151}
152
172bde85
LP
153static void loadSelfTranslation()
154{
155 Translator::translateLibrary(QLatin1String("liblxqt"));
156}
157
158Q_COREAPP_STARTUP_FUNCTION(loadSelfTranslation)