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()) {
56 QStringList
const Translations::getAvailableLanguages()
58 static QStringList languages
;
59 if (!languages
.size()) {
60 foreach (const QString
& loc
, getAvailableTranslations())
61 languages
.append(loc
.left(2));
66 QStringList
const Translations::getTranslationPaths()
68 // Look for translation files in the following locations, in order of priority
71 // Prefer path set in environment variable, makes it possible to test/replace translations w/out rebuilding.
72 if (qEnvironmentVariableIsSet("OPENTX_APP_TRANSLATIONS_PATH") && QDir(qgetenv("OPENTX_APP_TRANSLATIONS_PATH").constData()).exists()) {
73 paths
<< qgetenv("OPENTX_APP_TRANSLATIONS_PATH").constData();
75 // Try application subfolder first, also eg. to test/replace translations quickly.
76 paths
<< APP_TRANSLATIONS_FILE_PATH
;
77 // Then the resource file
78 paths
<< APP_TRANSLATIONS_RESOURCE_PATH
;
79 // Finally the system folder (more likely for Qt translations than Companion ones)
80 paths
<< QLibraryInfo::location(QLibraryInfo::TranslationsPath
);
85 void Translations::installTranslators()
87 Q_INIT_RESOURCE(translations
);
89 static QList
<QTranslator
*> appTranslators
;
91 // Determine the locale
93 QLocale locale
; // defaults to system locale
94 if (!g
.locale().isEmpty()) {
95 locale
= QLocale(g
.locale()); // reverts to "C" locale if invalid
96 if (locale
.language() == QLocale::C
) {
98 locale
= QLocale::system();
102 qDebug() << "Locale name:" << locale
.name() << "language:" << locale
.nativeLanguageName() << "country:" << locale
.nativeCountryName();
104 // Remove any existing translators, this lets us re-translate w/out restart.
105 foreach (QTranslator
* t
, appTranslators
) {
107 QCoreApplication::removeTranslator(t
);
111 appTranslators
.clear();
113 /* Multiple translation files can be installed.
114 * Translations are searched for in the reverse order from which they were installed (last added is searched first).
115 * The search stops as soon as a translation containing a matching string is found.
118 // Look for translation files in several locations
119 QStringList tryPaths
= getTranslationPaths();
121 // First try to install Qt translations for common GUI elements.
123 QStringList qtFiles
= QStringList() << "qt";
124 // After Qt5.3 some translation files are broken up into modules. We only need "qtbase" for now.
125 #if (QT_VERSION >= QT_VERSION_CHECK(5, 3, 0))
129 foreach (const QString
& qtfile
, qtFiles
) {
130 QTranslator
* translator
= new QTranslator(qApp
);
131 if (tryLoadTranslator(translator
, locale
, qtfile
, tryPaths
)) {
132 appTranslators
.append(translator
);
136 //qDebug() << "Could not find Qt translations for:" << locale.name() << "In file:" << qtfile << "Using paths:" << tryPaths;
140 // Now try to install our custom translations if we support the current locale/language.
141 if (!getAvailableTranslations().contains(locale
.name()) && !getAvailableLanguages().contains(locale
.name().left(2)))
144 QTranslator
* translator
= new QTranslator(qApp
);
145 if (tryLoadTranslator(translator
, locale
, "companion", tryPaths
)) {
146 appTranslators
.append(translator
);
150 qWarning() << "Could not find Companion translations for:" << locale
.name() << "Using paths:" << tryPaths
;
154 bool Translations::tryLoadTranslator(QTranslator
* t
, const QLocale
& locale
, const QString
& baseFile
, const QStringList
& paths
)
156 foreach (const QString
& path
, paths
) {
157 if (t
->load(locale
, baseFile
, "_", path
)) {
158 if (QCoreApplication::installTranslator(t
)) {
159 qDebug() << "Installed translation file" << baseFile
<< "from" << path
<< "for" << locale
.name();
163 qWarning() << "Error installing translation file" << baseFile
<< "for:" << locale
.name() << "from:" << path
;