Use newer version of cmake 2.8.9
[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
afd3b116
JL
10#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
11#include <qt5xdg/XdgDirs>
12#else
13#include <qtxdg/XdgDirs>
14#endif
15
949dba2f
AS
16using namespace LxQt;
17
18/************************************************
19
20 ************************************************/
21QStringList *getSearchPaths()
22{
23 static QStringList *searchPath = 0;
24
25 if (searchPath == 0)
26 {
27 searchPath = new QStringList();
28 *searchPath << XdgDirs::dataHome(false) + "/lxqt/translations";
29 *searchPath << XdgDirs::dataDirs("/lxqt/translations");
30 *searchPath << "/usr/local/share/lxqt/translations";
31 *searchPath << "/usr/share/lxqt/translations";
32 }
33
34 return searchPath;
35}
36
37
38/************************************************
39
40 ************************************************/
41QStringList LxQt::Translator::translationSearchPaths()
42{
43 return *(getSearchPaths());
44}
45
46
47/************************************************
48
49 ************************************************/
50void Translator::setTranslationSearchPaths(const QStringList &paths)
51{
52 QStringList *p = getSearchPaths();
53 p->clear();
54 *p << paths;
55}
56
57
58/************************************************
59
60 ************************************************/
61bool translate(const QString &name)
62{
63 QString locale = QLocale::system().name();
64
65 QStringList *paths = getSearchPaths();
66 foreach(QString path, *paths)
67 {
68 QTranslator *appTranslator = new QTranslator(qApp);
69 bool ok = appTranslator->load(name + "_" + locale, path);
70 if (ok)
71 {
72 QCoreApplication::installTranslator(appTranslator);
73 return ok;
74 }
75 }
76 return false;
77}
78
79
80/************************************************
81
82 ************************************************/
83bool Translator::translateApplication(const QString &applicationName)
84{
85 QString locale = QLocale::system().name();
86 QTranslator *qtTranslator = new QTranslator(qApp);
87 qtTranslator->load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
88 qApp->installTranslator(qtTranslator);
89
90
91 if (!applicationName.isEmpty())
92 return translate(applicationName);
93 else
94 return translate(QFileInfo(QCoreApplication::applicationFilePath()).baseName());
95}
96
97
98/************************************************
99
100 ************************************************/
101bool Translator::translateLibrary(const QString &libraryName)
102{
103 static QSet<QString> loadedLibs;
104
105 if (loadedLibs.contains(libraryName))
106 return true;
107
108 loadedLibs.insert(libraryName);
109
110 return translate(libraryName);
111}