5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "translations.h"
24 #include <QCoreApplication>
26 #include <QLibraryInfo>
27 #include <QTranslator>
29 // List of available Companion translations
30 // todo: make dynamic eg. from directory listing
31 // todo: make locale name country-agnostic unless translation is specifically for a country (like zh_CN)
32 QStringList
const Translations::getAvailableTranslations()
34 static QStringList locales
;
36 if (!locales
.size()) {
55 QStringList
const Translations::getAvailableLanguages()
57 static QStringList languages
;
58 if (!languages
.size()) {
59 foreach (const QString
& loc
, getAvailableTranslations())
60 languages
.append(loc
.left(2));
65 QStringList
const Translations::getTranslationPaths()
67 // Look for translation files in the following locations, in order of priority
70 // Prefer path set in environment variable, makes it possible to test/replace translations w/out rebuilding.
71 if (qEnvironmentVariableIsSet("OPENTX_APP_TRANSLATIONS_PATH") && QDir(qgetenv("OPENTX_APP_TRANSLATIONS_PATH").constData()).exists()) {
72 paths
<< qgetenv("OPENTX_APP_TRANSLATIONS_PATH").constData();
74 // Try application subfolder first, also eg. to test/replace translations quickly.
75 paths
<< APP_TRANSLATIONS_FILE_PATH
;
76 // Then the resource file
77 paths
<< APP_TRANSLATIONS_RESOURCE_PATH
;
78 // Finally the system folder (more likely for Qt translations than Companion ones)
79 paths
<< QLibraryInfo::location(QLibraryInfo::TranslationsPath
);
84 void Translations::installTranslators()
86 Q_INIT_RESOURCE(translations
);
88 static QList
<QTranslator
*> appTranslators
;
90 // Determine the locale
92 QLocale locale
; // defaults to system locale
93 if (!g
.locale().isEmpty()) {
94 locale
= QLocale(g
.locale()); // reverts to "C" locale if invalid
95 if (locale
.language() == QLocale::C
) {
97 locale
= QLocale::system();
101 qDebug() << "Locale name:" << locale
.name() << "language:" << locale
.nativeLanguageName() << "country:" << locale
.nativeCountryName();
103 // Remove any existing translators, this lets us re-translate w/out restart.
104 foreach (QTranslator
* t
, appTranslators
) {
106 QCoreApplication::removeTranslator(t
);
110 appTranslators
.clear();
112 /* Multiple translation files can be installed.
113 * Translations are searched for in the reverse order from which they were installed (last added is searched first).
114 * The search stops as soon as a translation containing a matching string is found.
117 // Look for translation files in several locations
118 QStringList tryPaths
= getTranslationPaths();
120 // First try to install Qt translations for common GUI elements.
122 QStringList qtFiles
= QStringList() << "qt";
123 // After Qt5.3 some translation files are broken up into modules. We only need "qtbase" for now.
124 #if (QT_VERSION >= QT_VERSION_CHECK(5, 3, 0))
128 foreach (const QString
& qtfile
, qtFiles
) {
129 QTranslator
* translator
= new QTranslator(qApp
);
130 if (tryLoadTranslator(translator
, locale
, qtfile
, tryPaths
)) {
131 appTranslators
.append(translator
);
135 //qDebug() << "Could not find Qt translations for:" << locale.name() << "In file:" << qtfile << "Using paths:" << tryPaths;
139 // Now try to install our custom translations if we support the current locale/language.
140 if (!getAvailableTranslations().contains(locale
.name()) && !getAvailableLanguages().contains(locale
.name().left(2)))
143 QTranslator
* translator
= new QTranslator(qApp
);
144 if (tryLoadTranslator(translator
, locale
, "companion", tryPaths
)) {
145 appTranslators
.append(translator
);
149 qWarning() << "Could not find Companion translations for:" << locale
.name() << "Using paths:" << tryPaths
;
153 bool Translations::tryLoadTranslator(QTranslator
* t
, const QLocale
& locale
, const QString
& baseFile
, const QStringList
& paths
)
155 foreach (const QString
& path
, paths
) {
156 if (t
->load(locale
, baseFile
, "_", path
)) {
157 if (QCoreApplication::installTranslator(t
)) {
158 qDebug() << "Installed translation file" << baseFile
<< "from" << path
<< "for" << locale
.name();
162 qWarning() << "Error installing translation file" << baseFile
<< "for:" << locale
.name() << "from:" << path
;