From 9745f804317f949843ec10099c6bd9fb95621133 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Sat, 26 Mar 2022 14:03:55 -0700 Subject: [PATCH] translations: load parent language translations Many language have country specific differences. We recognize different flavors of English (US, UK (and South Africa)), German (Germany and Switzerland), and Portuguese (Brazil and Portugal). For many other flavors of the languages that we have translations for we have no support and the way we hard-coded the fallbacks in the past was odd and meant that in the cases where we do have two flavors, missing strings in one weren't taken from the other (English as the default language being the exception). This tries to do a better job of recognizing some of those parent languages and loading translators for them, first. Which means if we then find a translator for the specific language (i.e., de_CH), strings missing in that translation are next searched in the parent language (de_DE), before finally providing the source language string (en_US). Signed-off-by: Dirk Hohndel --- core/qt-init.cpp | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/core/qt-init.cpp b/core/qt-init.cpp index b6e05d14c..20052c20e 100644 --- a/core/qt-init.cpp +++ b/core/qt-init.cpp @@ -11,7 +11,7 @@ #include "core/settings/qPref.h" char *settings_suffix = NULL; -static QTranslator qtTranslator, ssrfTranslator; +static QTranslator qtTranslator, ssrfTranslator, parentLanguageTranslator; void init_qt_late() { @@ -61,19 +61,9 @@ void init_qt_late() QLocale loc; // assign en_GB for use in South African locale - // and capture other French and Spanish speaking countries with the corresponding canonical locales if (loc.country() == QLocale::SouthAfrica) { loc.setDefault(QLocale("en_GB")); loc = QLocale(); - } else if (loc.language() == QLocale::French) { - loc.setDefault(QLocale("fr_FR")); - loc = QLocale(); - } else if (loc.language() == QLocale::Spanish) { - loc.setDefault(QLocale("es_ES")); - loc = QLocale(); - } else if (loc.language() == QLocale::German && loc.country() != QLocale::Switzerland) { - loc.setDefault(QLocale("de_DE")); - loc = QLocale(); } initUiLanguage(); QString uiLang = getUiLanguage(); @@ -107,10 +97,40 @@ void init_qt_late() } } } + // when creating our language names, we didn't define generic languages with additional + // country specific extensions, instead we included the "primary" country in the + // translation designations. This breaks the way Qt finds fall-back translations. + // Changing this now seems rather tedious, so instead we manually create a few + // obvious fallbacks + QPair parents[] = { + { QLocale::German, QLocale::Germany }, + { QLocale::Portuguese, QLocale::Brazil }, + { QLocale::French, QLocale::France}, + { QLocale::Spanish, QLocale::Spain} + }; + for (auto parent: parents) { + if (loc.language() == parent.first && loc.country() != parent.second) { + // first load de_DE so it's used as fall-back + QLocale parentLoc = QLocale(parent.first, parent.second); + if (parentLanguageTranslator.load(parentLoc, "subsurface", "_") || + parentLanguageTranslator.load(parentLoc, "subsurface", "_", translationLocation) || + parentLanguageTranslator.load(parentLoc, "subsurface", "_", getSubsurfaceDataPath("translations")) || + parentLanguageTranslator.load(parentLoc, "subsurface", "_", getSubsurfaceDataPath("../translations"))) { + if (verbose) + qDebug() << "loading" << parentLoc.name() << "translations"; + application->installTranslator(&parentLanguageTranslator); + } else { + qDebug() << "can't find Subsurface localization for locale" << parentLoc.name(); + } + } + + } if (ssrfTranslator.load(loc, "subsurface", "_") || ssrfTranslator.load(loc, "subsurface", "_", translationLocation) || ssrfTranslator.load(loc, "subsurface", "_", getSubsurfaceDataPath("translations")) || ssrfTranslator.load(loc, "subsurface", "_", getSubsurfaceDataPath("../translations"))) { + if (verbose) + qDebug() << "loading" << uiLang << "translations"; application->installTranslator(&ssrfTranslator); } else { qDebug() << "can't find Subsurface localization for locale" << uiLang;