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