From 7425f13abcf198cf281fe02a038b6359c7c481ef Mon Sep 17 00:00:00 2001 From: Bill Somerville Date: Mon, 25 May 2020 22:44:05 +0100 Subject: [PATCH] Add some polish to i18n package Qt translations so that standard buttons etc. get l10n. --- CMakeLists.txt | 17 +++++++++++++++- main.cpp | 54 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d347e6e7..307de7fd1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1045,6 +1045,7 @@ endif () # stuff only qmake can tell us # get_target_property (QMAKE_EXECUTABLE Qt5::qmake LOCATION) +get_target_property (LCONVERT_EXECUTABLE Qt5::lconvert LOCATION) function (QUERY_QMAKE VAR RESULT) exec_program (${QMAKE_EXECUTABLE} ARGS "-query ${VAR}" RETURN_VALUE return_code OUTPUT_VARIABLE output) if (NOT return_code) @@ -1055,6 +1056,7 @@ function (QUERY_QMAKE VAR RESULT) endfunction (QUERY_QMAKE) query_qmake (QT_INSTALL_PLUGINS QT_PLUGINS_DIR) +query_qmake (QT_INSTALL_TRANSLATIONS QT_TRANSLATIONS_DIR) query_qmake (QT_INSTALL_IMPORTS QT_IMPORTS_DIR) query_qmake (QT_HOST_DATA QT_DATA_DIR) set (QT_MKSPECS_DIR ${QT_DATA_DIR}/mkspecs) @@ -1102,6 +1104,19 @@ set (LANGUAGES foreach (lang_ ${LANGUAGES}) file (TO_NATIVE_PATH translations/wsjtx_${lang_}.ts ts_) list (APPEND TS_FILES ${ts_}) + set (qt_translations_ "${QT_TRANSLATIONS_DIR}/qtbase_${lang_}.qm") + if (NOT EXISTS "${qt_translations_}") + string (REGEX REPLACE "([a-z][a-z])_[A-Z][A-Z]" "\\1" lang_ ${lang_}) + set (qt_translations_ "${QT_TRANSLATIONS_DIR}/qtbase_${lang_}.qm") + endif () + if (EXISTS "${qt_translations_}") + add_custom_command ( + OUTPUT "${CMAKE_BINARY_DIR}/qt_${lang_}.qm" + COMMAND ${LCONVERT_EXECUTABLE} -o "${CMAKE_BINARY_DIR}/qt_${lang_}.qm" ${qt_translations_} + COMMENT "Building required Qt translations for language ${lang_}" + ) + list (APPEND QM_FILES "${CMAKE_BINARY_DIR}/qt_${lang_}.qm") + endif () endforeach () if (UPDATE_TRANSLATIONS) message (STATUS "UPDATE_TRANSLATIONS option is set.") @@ -1712,7 +1727,7 @@ if (NOT is_debug_build) # PATTERN "*quick*${CMAKE_SHARED_LIBRARY_SUFFIX}" EXCLUDE # ) - # add plugins path for WIN32 + # add paths for WIN32 file (RELATIVE_PATH _plugins_path "${CMAKE_INSTALL_PREFIX}/${WSJT_QT_CONF_DESTINATION}" "${CMAKE_INSTALL_PREFIX}/${WSJT_PLUGIN_DESTINATION}") install (CODE " get_filename_component (the_qt_conf \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${WSJT_QT_CONF_DESTINATION}/qt.conf\" REALPATH) diff --git a/main.cpp b/main.cpp index e8ab70317..fcd19e127 100644 --- a/main.cpp +++ b/main.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -116,6 +117,15 @@ int main(int argc, char *argv[]) .arg (QLocale::scriptToString (locale.script ())) .arg (QLocale::countryToString (locale.country ())) .arg (locale.uiLanguages ().join (", ")); + // qDebug () << "+++++++++++++++++++++++++++ Resources ++++++++++++++++++++++++++++"; + // { + // QDirIterator resources_iter {":/", QDirIterator::Subdirectories}; + // while (resources_iter.hasNext ()) + // { + // qDebug () << resources_iter.next (); + // } + // } + // qDebug () << "--------------------------- Resources ----------------------------"; setlocale (LC_NUMERIC, "C"); // ensure number forms are in // consistent format, do this after // instantiating QApplication so @@ -128,8 +138,14 @@ int main(int argc, char *argv[]) // // Enable base i18n // - QTranslator translator_from_resources; - QTranslator base_translator_from_resources; + QString translations_dir {":/Translations"}; + QTranslator qt_translator; + if (qt_translator.load (locale, "qt", "_", translations_dir)) + { + *early_messages << "Loaded Qt translations for current locale from resources"; + a.installTranslator (&qt_translator); + } + // Default translations for releases use translations stored in // the resources file system under the Translations // directory. These are built by the CMake build system from .ts @@ -142,15 +158,18 @@ int main(int argc, char *argv[]) // source control for translators to access and update. // try and load the base translation + QTranslator base_translator_from_resources; for (QString locale_name : locale.uiLanguages ()) { auto language = locale_name.left (2); if (locale.uiLanguages ().front ().left (2) == language) { *early_messages << QString {"Trying %1"}.arg (language); - if (base_translator_from_resources.load ("wsjtx_" + language, ":/Translations")) + if (base_translator_from_resources.load ("wsjtx_" + language, translations_dir)) { - *early_messages << QString {"Loaded base translation file from :/Translations based on language %1"}.arg (language); + *early_messages << QString {"Loaded base translation file from %1 based on language %2"} + .arg (translations_dir) + .arg (language); a.installTranslator (&base_translator_from_resources); break; } @@ -158,7 +177,8 @@ int main(int argc, char *argv[]) } // now try and load the most specific translations (may be a // duplicate but we shouldn't care) - if (translator_from_resources.load (locale, "wsjtx", "_", ":/Translations")) + QTranslator translator_from_resources; + if (translator_from_resources.load (locale, "wsjtx", "_", translations_dir)) { *early_messages << "Loaded translations for current locale from resources"; a.installTranslator (&translator_from_resources); @@ -214,35 +234,37 @@ int main(int argc, char *argv[]) // Complete i18n // - QTranslator translation_override_from_resources; - QTranslator base_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. + QTranslator base_translation_override_from_resources; + QTranslator translation_override_from_resources; if (parser.isSet (lang_option)) { auto language = parser.value (lang_option).replace ('-', '_'); // try and load the base translation auto base_language = language.left (2); - if (base_translation_override_from_resources.load ("wsjtx_" + base_language, ":/Translations")) + if (base_translation_override_from_resources.load ("wsjtx_" + base_language, translations_dir)) { - *early_messages << QString {"Loaded base translation file from :/Translations based on language %1"}.arg (base_language); + *early_messages << QString {"Loaded base translation file from %1 based on language %2"} + .arg (translations_dir) + .arg (base_language); a.installTranslator (&base_translation_override_from_resources); } // now load the requested translations (may be a duplicate // but we shouldn't care) - if (translation_override_from_resources.load ("wsjtx_" + language , ":/Translations")) + if (translation_override_from_resources.load ("wsjtx_" + language, translations_dir)) { - *early_messages << QString {"Loaded translation file from :/Translations based on language %1"}.arg (language); + *early_messages << QString {"Loaded translation file from %1 based on language %2"} + .arg (translations_dir) + .arg (language); a.installTranslator (&translation_override_from_resources); } } - QTranslator translator_from_cwd; - QTranslator base_translator_from_cwd; // 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 @@ -253,6 +275,7 @@ int main(int argc, char *argv[]) // on non-Windows system. // try and load the base translation + QTranslator base_translator_from_cwd; for (QString locale_name : locale.uiLanguages ()) { auto language = locale_name.left (2); @@ -269,20 +292,21 @@ int main(int argc, char *argv[]) } // now try and load the most specific translations (may be a // duplicate but we shouldn't care) + QTranslator translator_from_cwd; if (translator_from_cwd.load (locale, "wsjtx", "_")) { *early_messages << "loaded translations for current locale from a file"; a.installTranslator (&translator_from_cwd); } - QTranslator translation_override_from_cwd; - QTranslator base_translation_override_from_cwd; // 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. + QTranslator base_translation_override_from_cwd; + QTranslator translation_override_from_cwd; if (parser.isSet (lang_option)) { auto language = parser.value (lang_option).replace ('-', '_');