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 <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2022-03-26 14:03:55 -07:00
parent 0a8344e613
commit 8e43384a85
2 changed files with 34 additions and 23 deletions

View file

@ -1,15 +1,6 @@
- profile: include profile editing in undo system
- mobile: Add a dark theme for statistics
- core: avoid crash with corrupted cloud storage
- mobile: fix profile scaling issue on high DPR devices
- mobile: bring back profile icons
- mobile/Android: add logfiles as attachment to support emails
- planner: make ESC (cancel plan) work when moving handles
- dive list: make dive guide visible in dive list [#3382]
- general: rename dive master to dive guide
- desktop: Don't lose cursor position in notes when switching between windows [#3369]
- Uemis support: fix the ability disconnect/reconnect the Zurich when its filesystem is full
- libdivecomputer: add support for latest BLE hardware in OSTC dive computers
- fix missing libraries in Windows and Mac installers that caused the maps not to show
- provide reasonable fall-back for translations so missing translations in country-specific
translations are picked from (hopefully more complete) "parent" languages
---
* Always add new entries at the very top of this file above other existing entries and this note.

View file

@ -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<QLocale::Language, QLocale::Country> parents[] = {
{ QLocale::German, QLocale::Germany },
{ QLocale::Portuguese, QLocale::Portugal },
{ 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;