Simplify translate() code
[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);
85 qtTranslator->load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
86 qApp->installTranslator(qtTranslator);
87
88
89 if (!applicationName.isEmpty())
90 return translate(applicationName);
91 else
92 return translate(QFileInfo(QCoreApplication::applicationFilePath()).baseName());
93}
94
95
96/************************************************
97
98 ************************************************/
99bool Translator::translateLibrary(const QString &libraryName)
100{
101 static QSet<QString> loadedLibs;
102
103 if (loadedLibs.contains(libraryName))
104 return true;
105
106 loadedLibs.insert(libraryName);
107
108 return translate(libraryName);
109}