Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Adam <dev@robert-adam.de>2021-03-16 20:39:39 +0300
committerRobert Adam <dev@robert-adam.de>2021-03-16 20:39:39 +0300
commit6fdcb61fcabb5cfa7e3174a158a61b4cf3fb41ad (patch)
treeb3e6f9c7ec64ac8697b4ef0aea8116d45f3944fc
parentf5177b8bfac723c950e78a1143e5633c275b926c (diff)
FIX(client): Make translations work again
Commit 0dcc1f614b2ab4f0d4bbac5fd29676595072a133 introduced a regression that caused translations to no longer work (the UI would always remain in English). The reason for this is that the translator objects installed into the application have to remain alive in order to function. By moving them into a dedicated function they went out of scope very quickly after their creation and thus none of them actually performed any work. This commit fixes this by creating the translator objects on the heap and using a smart-pointer-like structure for making sure they are only deleted once they are really not needed anymore. Fixes #4856
-rw-r--r--src/mumble/Translations.cpp74
-rw-r--r--src/mumble/Translations.h24
-rw-r--r--src/mumble/main.cpp2
3 files changed, 78 insertions, 22 deletions
diff --git a/src/mumble/Translations.cpp b/src/mumble/Translations.cpp
index bf93f78da..90ae8b1e9 100644
--- a/src/mumble/Translations.cpp
+++ b/src/mumble/Translations.cpp
@@ -14,6 +14,20 @@
namespace Mumble {
namespace Translations {
+ LifetimeGuard::LifetimeGuard(LifetimeGuard &&old) : m_bundledTranslator(old.m_bundledTranslator),
+ m_overwriteTranslator(old.m_overwriteTranslator), m_qtTranslator(old.m_qtTranslator) {
+ // Reset values of old
+ old.m_bundledTranslator = nullptr;
+ old.m_overwriteTranslator = nullptr;
+ old.m_qtTranslator = nullptr;
+ }
+
+ LifetimeGuard::~LifetimeGuard() {
+ delete m_bundledTranslator;
+ delete m_overwriteTranslator;
+ delete m_qtTranslator;
+ }
+
QStringList getDefaultTranslationDirectories() {
QStringList translationDirs;
@@ -50,32 +64,46 @@ namespace Translations {
return directories;
}
- void installTranslators(const QLocale &locale, QApplication &app, const QStringList &extraDirectories) {
+ LifetimeGuard installTranslators(const QLocale &locale, QApplication &app, const QStringList &extraDirectories) {
+ LifetimeGuard guard;
+
// First install a translator that uses the bundled translations
- QTranslator bundledTranslator;
- if (bundledTranslator.load(locale, ":mumble_")) {
- app.installTranslator(&bundledTranslator);
+ guard.m_bundledTranslator = new QTranslator();
+ if (guard.m_bundledTranslator->load(locale, ":mumble_")) {
+ app.installTranslator(guard.m_bundledTranslator);
} else {
qWarning("Unable to find bundled translations for locale \"%s\"", qUtf8Printable(locale.name()));
+
+ delete guard.m_bundledTranslator;
+ guard.m_bundledTranslator = nullptr;
}
// Now try to add another translator that can overwrite the bundled translations based
// on translations found in one of the translation directories.
// The first matching translation file that is found, will be used (first come, first served)
- QTranslator overwriteTranslator;
+ guard.m_overwriteTranslator = new QTranslator();
const QString prefix = "";
+ bool overwriteTranslatorFound = false;
for (const QString &currentDir : getTranslationDirectories(app, extraDirectories)) {
- if (overwriteTranslator.load(locale, "mumble_", prefix, currentDir)) {
- app.installTranslator(&overwriteTranslator);
+ if (guard.m_overwriteTranslator->load(locale, "mumble_", prefix, currentDir)) {
+ app.installTranslator(guard.m_overwriteTranslator);
qWarning("Using extra translation file for locale \"%s\" from directory \"%s\"",
qUtf8Printable(locale.name()), qUtf8Printable(currentDir));
+
+ overwriteTranslatorFound = true;
+
break;
}
}
+ if (!overwriteTranslatorFound) {
+ delete guard.m_overwriteTranslator;
+ guard.m_overwriteTranslator = nullptr;
+ }
+
// With modularization of Qt 5 some - but not all - of the qt_<locale>.ts files have become
// so-called meta catalogues which no longer contain actual translations but refer to other
// more specific ts files like qtbase_<locale>.ts . To successfully load a meta catalogue all
@@ -84,24 +112,30 @@ namespace Translations {
// qtbase_<locale>.ts if that failed.
//
// See http://doc.qt.io/qt-5/linguist-programmers.html#deploying-translations for more information
- QTranslator qttranslator;
+ guard.m_qtTranslator = new QTranslator();
// First we try and see if there is a translation packaged with Mumble that shall overwrite any potentially
// existing Qt translations. If not, we try to load the qt-translations installed on the host-machine and if
// that fails as well, we try to load translations bundled in Mumble. Note: Resource starting with :/ are
// bundled resources specified in a .qrc file
- if (qttranslator.load(locale, ":/mumble_overwrite_qt_")) {
- app.installTranslator(&qttranslator);
- } else if (qttranslator.load(locale, ":/mumble_overwrite_qtbase_")) {
- app.installTranslator(&qttranslator);
- } else if (qttranslator.load(locale, "qt_", QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
- app.installTranslator(&qttranslator);
- } else if (qttranslator.load(locale, "qtbase_", QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
- app.installTranslator(&qttranslator);
- } else if (qttranslator.load(locale, ":/qt_")) {
- app.installTranslator(&qttranslator);
- } else if (qttranslator.load(locale, ":/qtbase_")) {
- app.installTranslator(&qttranslator);
+ if (guard.m_qtTranslator->load(locale, ":/mumble_overwrite_qt_")) {
+ app.installTranslator(guard.m_qtTranslator);
+ } else if (guard.m_qtTranslator->load(locale, ":/mumble_overwrite_qtbase_")) {
+ app.installTranslator(guard.m_qtTranslator);
+ } else if (guard.m_qtTranslator->load(locale, "qt_", QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
+ app.installTranslator(guard.m_qtTranslator);
+ } else if (guard.m_qtTranslator->load(locale, "qtbase_", QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
+ app.installTranslator(guard.m_qtTranslator);
+ } else if (guard.m_qtTranslator->load(locale, ":/qt_")) {
+ app.installTranslator(guard.m_qtTranslator);
+ } else if (guard.m_qtTranslator->load(locale, ":/qtbase_")) {
+ app.installTranslator(guard.m_qtTranslator);
+ } else {
+ // Did not install Qt translator
+ delete guard.m_qtTranslator;
+ guard.m_qtTranslator = nullptr;
}
+
+ return guard;
}
}; // namespace Translations
}; // namespace Mumble
diff --git a/src/mumble/Translations.h b/src/mumble/Translations.h
index 4e472e635..2a0b8f3d7 100644
--- a/src/mumble/Translations.h
+++ b/src/mumble/Translations.h
@@ -10,15 +10,37 @@
class QApplication;
class QLocale;
+class QTranslator;
namespace Mumble {
namespace Translations {
+ /**
+ * A lifetime guard for translators. Basically a smart-pointer for an ensemble of translators
+ */
+ struct LifetimeGuard {
+ QTranslator *m_bundledTranslator = nullptr;
+ QTranslator *m_overwriteTranslator = nullptr;
+ QTranslator *m_qtTranslator = nullptr;
+
+ LifetimeGuard() = default;
+ LifetimeGuard(LifetimeGuard &&old);
+ LifetimeGuard(const LifetimeGuard &) = delete;
+ ~LifetimeGuard();
+ };
+
QStringList getDefaultTranslationDirectories();
QStringList getTranslationDirectories(const QApplication &app, const QStringList &extraDirectories);
- void installTranslators(const QLocale &locale, QApplication &app, const QStringList &extraDirectories);
+ /**
+ * Installs translators for the given locale to the given application. If extraDirectories are provided,
+ * they will be used for the overwrite translator.
+ *
+ * @returns The lifetime guard of the created translators. This object must not be destroyed before
+ * the given application has terminated.
+ */
+ LifetimeGuard installTranslators(const QLocale &locale, QApplication &app, const QStringList &extraDirectories);
}; // namespace Translations
}; // namespace Mumble
diff --git a/src/mumble/main.cpp b/src/mumble/main.cpp
index 42991c805..acd2934c6 100644
--- a/src/mumble/main.cpp
+++ b/src/mumble/main.cpp
@@ -585,7 +585,7 @@ int main(int argc, char **argv) {
qWarning("Locale is \"%s\" (System: \"%s\")", qUtf8Printable(settingsLocale.name()), qUtf8Printable(systemLocale.name()));
- Mumble::Translations::installTranslators(settingsLocale, a, extraTranslationDirs);
+ Mumble::Translations::LifetimeGuard translationGuard = Mumble::Translations::installTranslators(settingsLocale, a, extraTranslationDirs);
// Initialize proxy settings
NetworkConfig::SetupProxy();