Companion: Russian UI (#7180)
[opentx.git] / companion / src / translations.cpp
blob76d63b42545f6a13a71c7e5fa87117a1c409a233
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
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"
22 #include "appdata.h"
24 #include <QCoreApplication>
25 #include <QDir>
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()) {
37 locales << "cs_CZ"
38 << "de_DE"
39 << "en"
40 << "es_ES"
41 << "fi_FI"
42 << "fr_FR"
43 //<< "he_IL"
44 << "it_IT"
45 //<< "nl_NL"
46 << "pl_PL"
47 //<< "pt_PT"
48 << "ru_RU"
49 << "sv_SE"
50 << "zh_CN"
51 << "ja_JP" ;
53 return locales;
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));
63 return languages;
66 QStringList const Translations::getTranslationPaths()
68 // Look for translation files in the following locations, in order of priority
69 QStringList paths;
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);
82 return paths;
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) {
97 // reset
98 locale = QLocale::system();
99 g.locale("");
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) {
106 if (t) {
107 QCoreApplication::removeTranslator(t);
108 delete 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))
126 qtFiles << "qtbase";
127 #endif
129 foreach (const QString & qtfile, qtFiles) {
130 QTranslator * translator = new QTranslator(qApp);
131 if (tryLoadTranslator(translator, locale, qtfile, tryPaths)) {
132 appTranslators.append(translator);
134 else {
135 delete 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)))
142 return;
144 QTranslator * translator = new QTranslator(qApp);
145 if (tryLoadTranslator(translator, locale, "companion", tryPaths)) {
146 appTranslators.append(translator);
148 else {
149 delete 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();
160 return true;
162 else {
163 qWarning() << "Error installing translation file" << baseFile << "for:" << locale.name() << "from:" << path;
164 return false;
168 return false;