From 9a7b9572fdf9b4acbba46ac4ea5cc0c27bc919aa Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Mon, 5 Feb 2024 19:13:32 +0400 Subject: [PATCH] Deploy base Qt translations when needed --- CMakeLists.txt | 39 ++++++++++++++++++++++++++---- apps/launcher/main.cpp | 13 ++-------- apps/wizard/main.cpp | 13 ++-------- components/CMakeLists.txt | 5 ++++ components/l10n/qttranslations.cpp | 37 ++++++++++++++++++++++++++++ components/l10n/qttranslations.hpp | 16 ++++++++++++ 6 files changed, 96 insertions(+), 27 deletions(-) create mode 100644 components/l10n/qttranslations.cpp create mode 100644 components/l10n/qttranslations.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 313cc46cbe..3f5745d4a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,14 @@ if(OPENMW_GL4ES_MANUAL_INIT) add_definitions(-DOPENMW_GL4ES_MANUAL_INIT) endif() +if (APPLE OR WIN32) + set(DEPLOY_QT_TRANSLATIONS_DEFAULT ON) +else () + set(DEPLOY_QT_TRANSLATIONS_DEFAULT OFF) +endif () + +option(DEPLOY_QT_TRANSLATIONS "Deploy standard Qt translations to resources folder. Needed when OpenMW applications are deployed with Qt libraries" ${DEPLOY_QT_TRANSLATIONS_DEFAULT}) + # Apps and tools option(BUILD_OPENMW "Build OpenMW" ON) option(BUILD_LAUNCHER "Build Launcher" ON) @@ -1099,13 +1107,13 @@ if (USE_QT) if (BUILD_LAUNCHER OR BUILD_WIZARD) if (APPLE) - set(QT_TRANSLATIONS_PATH "${APP_BUNDLE_DIR}/Contents/Resources/resources/translations") + set(QT_OPENMW_TRANSLATIONS_PATH "${APP_BUNDLE_DIR}/Contents/Resources/resources/translations") else () get_generator_is_multi_config(multi_config) if (multi_config) - set(QT_TRANSLATIONS_PATH "${OpenMW_BINARY_DIR}/$/resources/translations") + set(QT_OPENMW_TRANSLATIONS_PATH "${OpenMW_BINARY_DIR}/$/resources/translations") else () - set(QT_TRANSLATIONS_PATH "${OpenMW_BINARY_DIR}/resources/translations") + set(QT_OPENMW_TRANSLATIONS_PATH "${OpenMW_BINARY_DIR}/resources/translations") endif () endif () @@ -1121,9 +1129,30 @@ if (USE_QT) qt_add_translation(QM_FILES ${TS_FILES} OPTIONS -silent) + if (DEPLOY_QT_TRANSLATIONS) + # Once we set a Qt 6.2.0 as a minimum required version, we may use "qtpaths --qt-query" instead. + get_target_property(QT_QMAKE_EXECUTABLE Qt::qmake IMPORTED_LOCATION) + execute_process(COMMAND "${QT_QMAKE_EXECUTABLE}" -query QT_INSTALL_TRANSLATIONS + OUTPUT_VARIABLE QT_TRANSLATIONS_DIR OUTPUT_STRIP_TRAILING_WHITESPACE) + + foreach(QM_FILE ${QM_FILES}) + get_filename_component(QM_BASENAME ${QM_FILE} NAME) + string(REGEX REPLACE "[^_]+_(.*)\\.qm" "\\1" LANG_NAME ${QM_BASENAME}) + if (EXISTS "${QT_TRANSLATIONS_DIR}/qtbase_${LANG_NAME}.qm") + set(QM_FILES ${QM_FILES} "${QT_TRANSLATIONS_DIR}/qtbase_${LANG_NAME}.qm") + elseif (EXISTS "${QT_TRANSLATIONS_DIR}/qt_${LANG_NAME}.qm") + set(QM_FILES ${QM_FILES} "${QT_TRANSLATIONS_DIR}/qt_${LANG_NAME}.qm") + else () + message(FATAL_ERROR "Qt translations for '${LANG_NAME}' locale are not found in the '${QT_TRANSLATIONS_DIR}' folder.") + endif () + endforeach(QM_FILE) + + list(REMOVE_DUPLICATES QM_FILES) + endif () + add_custom_target(qm-files - COMMAND ${CMAKE_COMMAND} -E make_directory ${QT_TRANSLATIONS_PATH} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${QM_FILES} ${QT_TRANSLATIONS_PATH} + COMMAND ${CMAKE_COMMAND} -E make_directory ${QT_OPENMW_TRANSLATIONS_PATH} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${QM_FILES} ${QT_OPENMW_TRANSLATIONS_PATH} DEPENDS ${QM_FILES} COMMENT "Copy *.qm files to resources folder") endif () diff --git a/apps/launcher/main.cpp b/apps/launcher/main.cpp index 3cbd1f5092..7fdcac77ab 100644 --- a/apps/launcher/main.cpp +++ b/apps/launcher/main.cpp @@ -1,7 +1,6 @@ #include #include -#include #include #include @@ -9,6 +8,7 @@ #include #include #include +#include #include #ifdef MAC_OS_X_VERSION_MIN_REQUIRED @@ -41,16 +41,7 @@ int runLauncher(int argc, char* argv[]) resourcesPath = Files::pathToQString(variables["resources"].as().u8string()); } - // Internationalization - QString locale = QLocale::system().name().section('_', 0, 0); - - QTranslator appTranslator; - appTranslator.load(resourcesPath + "/translations/launcher_" + locale + ".qm"); - app.installTranslator(&appTranslator); - - QTranslator componentsAppTranslator; - componentsAppTranslator.load(resourcesPath + "/translations/components_" + locale + ".qm"); - app.installTranslator(&componentsAppTranslator); + l10n::installQtTranslations(app, "launcher", resourcesPath); Launcher::MainDialog mainWin(configurationManager); diff --git a/apps/wizard/main.cpp b/apps/wizard/main.cpp index a911ac7350..b62428f946 100644 --- a/apps/wizard/main.cpp +++ b/apps/wizard/main.cpp @@ -1,11 +1,11 @@ #include #include -#include #include #include #include +#include #include "mainwizard.hpp" @@ -45,16 +45,7 @@ int main(int argc, char* argv[]) resourcesPath = Files::pathToQString(variables["resources"].as().u8string()); } - // Internationalization - QString locale = QLocale::system().name().section('_', 0, 0); - - QTranslator appTranslator; - appTranslator.load(resourcesPath + "/translations/wizard_" + locale + ".qm"); - app.installTranslator(&appTranslator); - - QTranslator componentsAppTranslator; - componentsAppTranslator.load(resourcesPath + "/translations/components_" + locale + ".qm"); - app.installTranslator(&componentsAppTranslator); + l10n::installQtTranslations(app, "wizard", resourcesPath); Wizard::MainWizard wizard; diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index bb6fd1dd37..317786e96a 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -500,11 +500,16 @@ if (USE_QT) model/loadordererror view/combobox view/contentselector ) + add_component_qt_dir (config gamesettings launchersettings ) + add_component_qt_dir (l10n + qttranslations + ) + add_component_qt_dir (process processinvoker ) diff --git a/components/l10n/qttranslations.cpp b/components/l10n/qttranslations.cpp new file mode 100644 index 0000000000..9bc146699e --- /dev/null +++ b/components/l10n/qttranslations.cpp @@ -0,0 +1,37 @@ +#include "qttranslations.hpp" + +#include +#include + +namespace l10n +{ + QTranslator AppTranslator{}; + QTranslator ComponentsTranslator{}; + QTranslator QtBaseAppTranslator{}; + + void installQtTranslations(QApplication& app, const QString& appName, const QString& resourcesPath) + { + // Try to load OpenMW translations from resources folder first. + // If we loaded them, try to load Qt translations from both + // resources folder and default translations folder as well. +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + auto qtPath = QLibraryInfo::path(QLibraryInfo::TranslationsPath); +#else + auto qtPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath); +#endif + auto localPath = resourcesPath + "/translations"; + + if (AppTranslator.load(QLocale::system(), appName, "_", localPath) + && ComponentsTranslator.load(QLocale::system(), "components", "_", localPath)) + { + app.installTranslator(&AppTranslator); + app.installTranslator(&ComponentsTranslator); + + if (QtBaseAppTranslator.load(QLocale::system(), "qtbase", "_", localPath) + || QtBaseAppTranslator.load(QLocale::system(), "qt", "_", localPath) + || QtBaseAppTranslator.load(QLocale::system(), "qtbase", "_", qtPath) + || QtBaseAppTranslator.load(QLocale::system(), "qt", "_", qtPath)) + app.installTranslator(&QtBaseAppTranslator); + } + } +} diff --git a/components/l10n/qttranslations.hpp b/components/l10n/qttranslations.hpp new file mode 100644 index 0000000000..3ce87f0837 --- /dev/null +++ b/components/l10n/qttranslations.hpp @@ -0,0 +1,16 @@ +#ifndef COMPONENTS_L10N_QTTRANSLATIONS_H +#define COMPONENTS_L10N_QTTRANSLATIONS_H + +#include +#include + +namespace l10n +{ + extern QTranslator AppTranslator; + extern QTranslator ComponentsTranslator; + extern QTranslator QtBaseAppTranslator; + + void installQtTranslations(QApplication& app, const QString& appName, const QString& resourcesPath); +} + +#endif // COMPONENTS_L10N_QTTRANSLATIONS_H