Deploy base Qt translations when needed

BindlessTest
Andrei Kortunov 11 months ago
parent 66b2d428c8
commit 9a7b9572fd

@ -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}/$<CONFIG>/resources/translations")
set(QT_OPENMW_TRANSLATIONS_PATH "${OpenMW_BINARY_DIR}/$<CONFIG>/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 ()

@ -1,7 +1,6 @@
#include <iostream>
#include <QDir>
#include <QTranslator>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/variables_map.hpp>
@ -9,6 +8,7 @@
#include <components/debug/debugging.hpp>
#include <components/files/configurationmanager.hpp>
#include <components/files/qtconversion.hpp>
#include <components/l10n/qttranslations.hpp>
#include <components/platform/platform.hpp>
#ifdef MAC_OS_X_VERSION_MIN_REQUIRED
@ -41,16 +41,7 @@ int runLauncher(int argc, char* argv[])
resourcesPath = Files::pathToQString(variables["resources"].as<Files::MaybeQuotedPath>().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);

@ -1,11 +1,11 @@
#include <QApplication>
#include <QDir>
#include <QTranslator>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/variables_map.hpp>
#include <components/files/qtconversion.hpp>
#include <components/l10n/qttranslations.hpp>
#include "mainwizard.hpp"
@ -45,16 +45,7 @@ int main(int argc, char* argv[])
resourcesPath = Files::pathToQString(variables["resources"].as<Files::MaybeQuotedPath>().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;

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

@ -0,0 +1,37 @@
#include "qttranslations.hpp"
#include <QLibraryInfo>
#include <QLocale>
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);
}
}
}

@ -0,0 +1,16 @@
#ifndef COMPONENTS_L10N_QTTRANSLATIONS_H
#define COMPONENTS_L10N_QTTRANSLATIONS_H
#include <QApplication>
#include <QTranslator>
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
Loading…
Cancel
Save