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