Add some polish to i18n

package Qt translations so that standard buttons etc. get l10n.
This commit is contained in:
Bill Somerville 2020-05-25 22:44:05 +01:00
parent 77f49bae7b
commit 19c81eecb6
No known key found for this signature in database
GPG Key ID: D864B06D1E81618F
2 changed files with 68 additions and 20 deletions

View File

@ -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)

View File

@ -17,6 +17,7 @@
#include <QLibraryInfo>
#include <QSysInfo>
#include <QDir>
#include <QDirIterator>
#include <QStandardPaths>
#include <QStringList>
#include <QLockFile>
@ -107,9 +108,23 @@ 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 ();
std::unique_ptr<QStringList> early_messages {new QStringList};
QLocale locale; // get the current system locale
*early_messages << QString {"locale: language: %1 script: %2 country: %3 ui-languages: %4"}
.arg (QLocale::languageToString (locale.language ()))
.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
@ -122,8 +137,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
@ -136,19 +157,27 @@ 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 (base_translator_from_resources.load ("wsjtx_" + language, ":/Translations"))
{
qDebug () << QString {"Loaded base translation file from :/Translations based on language %1"}.arg (language);
a.installTranslator (&base_translator_from_resources);
break;
*early_messages << QString {"Trying %1"}.arg (language);
if (base_translator_from_resources.load ("wsjtx_" + language, translations_dir))
{
*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;
}
}
}
// 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))
{
qDebug () << "Loaded translations for current locale from resources";
a.installTranslator (&translator_from_resources);
@ -204,35 +233,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))
{
qDebug () << 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))
{
qDebug () << 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
@ -243,6 +274,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);
@ -255,20 +287,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", "_"))
{
qDebug () << "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 ('-', '_');