From 9501cdf27104ecc9b54d39b93ff1fdde16b785e7 Mon Sep 17 00:00:00 2001 From: Bill Somerville Date: Mon, 18 May 2020 23:00:02 +0100 Subject: [PATCH] Sorting out how to test translation files Windows makes this more complex than necessary. On all systems the packaged translation file in the resources :/Translations directory wsjtx_.qm will be loaded if matches the current system locale. Otherwise the native translatable strings will be used (en_US is assumed for the native language). On all systems a wsjtx_.qm compiled translation file in the current working directory will be loaded if matches the current system locale language and country (wsjtx_en_GB.qm for a locale of en-GB). On non-Windows systems the locale used above can be set for just the wsjtx instance being tested by setting the LANG environment variable, e.g. LANG=ca-ES wsjtx On Windows systems the current locale can only be changed by installing the relevant Windows Language Pack, selecting the UI language either as the default or as an override (Set-WinUILanguageOverride -Language ca-ES) and the signing out and back in. The two translations file sources above cam be overridden using a new command line option: [-l | -language] [-] e.g. -language ca-ES which will load the first readable translation file as found in the following order: :/Translations/wsjtx_ca_ES.qm, :/Translation/wsjtx_ca.qm, :/Translations/wsjtx.qm. This search will be preceded by the normal translation file load from resources described above. Following that and the normal load from the current working directory described above, the first readable translation file as found in the following order: $cwd/wsjtx_ca_ES.qm, $cwd/wsjtx_ca.qm, $cwd/wsjtx.qm. This allows Windows testers to change the WSJT-X UI language without having to change the system UI language and installing the relevant language pack. Note that using this method will only change the translated strings, number and date formatting will not change. Because of this it should only be used for basic testing. --- main.cpp | 100 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 77 insertions(+), 23 deletions(-) diff --git a/main.cpp b/main.cpp index 74174e378..40e0247a7 100644 --- a/main.cpp +++ b/main.cpp @@ -107,8 +107,20 @@ int main(int argc, char *argv[]) ExceptionCatchingApplication a(argc, argv); try { + QLocale locale; // get the current system locale + qDebug () << "locale: language:" << locale.language () << "script:" << locale.script () + << "country:" << locale.country () << "ui-languages:" << locale.uiLanguages (); + setlocale (LC_NUMERIC, "C"); // ensure number forms are in + // consistent format, do this after + // instantiating QApplication so + // that GUI has correct l18n + + // Override programs executable basename as application name. + a.setApplicationName ("WSJT-X"); + a.setApplicationVersion (version ()); + // - // Enable i18n + // Enable base i18n // QTranslator translator_from_resources; // Default translations for releases use translations stored in @@ -121,28 +133,11 @@ int main(int argc, char *argv[]) // translations but should only be set when adding new // languages. The resulting .ts files should be checked info // source control for translators to access and update. - translator_from_resources.load (QLocale::system (), "wsjtx", "_", ":/Translations"); - a.installTranslator (&translator_from_resources); - - QTranslator translator_from_files; - // Load any matching translation from the current directory - // using the locale name. This allows translators to easily test - // their translations by releasing (lrelease) a .qm file into - // the current directory with a suitable name - // (e.g. wsjtx_en_GB.qm), then running wsjtx to view the - // results. Either the system locale setting or the environment - // variable LANG can be used to select the target language. - translator_from_files.load (QString {"wsjtx_"} + QLocale::system ().name ()); - a.installTranslator (&translator_from_files); - - setlocale (LC_NUMERIC, "C"); // ensure number forms are in - // consistent format, do this after - // instantiating QApplication so - // that GUI has correct l18n - - // Override programs executable basename as application name. - a.setApplicationName ("WSJT-X"); - a.setApplicationVersion (version ()); + if (translator_from_resources.load (locale, "wsjtx", "_", ":/Translations")) + { + qDebug () << "Loaded translations for current locale from resources"; + a.installTranslator (&translator_from_resources); + } QCommandLineParser parser; parser.setApplicationDescription ("\n" PROJECT_SUMMARY_DESCRIPTION); @@ -161,6 +156,12 @@ int main(int argc, char *argv[]) , a.translate ("main", "configuration")); parser.addOption (cfg_option); + // support for UI language override (useful on Windows) + QCommandLineOption lang_option (QStringList {} << "l" << "language" + , a.translate ("main", "Where is [-].") + , a.translate ("main", "language")); + parser.addOption (lang_option); + QCommandLineOption test_option (QStringList {} << "test-mode" , a.translate ("main", "Writable files in test location. Use with caution, for testing only.")); parser.addOption (test_option); @@ -184,6 +185,59 @@ int main(int argc, char *argv[]) } } + // + // Complete i18n + // + + QTranslator translation_override_from_resources; + // Load any matching translation from the current directory + // using the command line option language override. This allows + // translators to easily test their translations by releasing + // (lrelease) a .qm file into the current directory with a + // suitable name (e.g. wsjtx_en_GB.qm), then running wsjtx to + // view the results. + if (parser.isSet (lang_option)) + { + auto language = parser.value (lang_option).replace ('-', '_'); + if (translation_override_from_resources.load ("wsjtx_" + language , ":/Translations")) + { + qDebug () << QString {"loaded translation file :/Translations/wsjtx_%1.qm"}.arg (language); + a.installTranslator (&translation_override_from_resources); + } + } + + QTranslator translator_from_files; + // Load any matching translation from the current directory + // using the current locale. This allows translators to easily + // test their translations by releasing (lrelease) a .qm file + // into the current directory with a suitable name (e.g. + // wsjtx_en_GB.qm), then running wsjtx to view the results. The + // system locale setting will be used to select the translation + // file which can be overridden by the LANG environment variable + // on non-Windows system. + if (translator_from_files.load (locale, "wsjtx", "_")) + { + qDebug () << "loaded translations for current locale from a file"; + a.installTranslator (&translator_from_files); + } + + QTranslator translation_override_from_files; + // Load any matching translation from the current directory + // using the command line option language override. This allows + // translators to easily test their translations on Windows by + // releasing (lrelease) a .qm file into the current directory + // with a suitable name (e.g. wsjtx_en_GB.qm), then running + // wsjtx to view the results. + if (parser.isSet (lang_option)) + { + auto language = parser.value (lang_option).replace ('-', '_'); + if (translation_override_from_files.load ("wsjtx_" + language)) + { + qDebug () << QString {"loaded translation file $cwd/wsjtx_%1.qm"}.arg (language); + a.installTranslator (&translation_override_from_files); + } + } + QStandardPaths::setTestModeEnabled (parser.isSet (test_option)); // support for multiple instances running from a single installation