qt: Use upstream Qt Base translations when available

This commit is contained in:
OpenSauce04 2026-05-25 17:00:38 +01:00 committed by OpenSauce
parent c03248f158
commit c96d44895e
4 changed files with 34 additions and 13 deletions

View file

@ -218,7 +218,8 @@ if (ENABLE_QT_UPDATE_CHECKER)
endif() endif()
if (ENABLE_QT_TRANSLATION) if (ENABLE_QT_TRANSLATION)
set(CITRA_QT_LANGUAGES "${PROJECT_SOURCE_DIR}/dist/languages" CACHE PATH "Path to the translation bundle for the Qt frontend") set(BASE_QT_LANGUAGES "${QT_HOST_PATH}/translations" CACHE PATH "Path to the Qt's base translations")
set(CITRA_QT_LANGUAGES "${PROJECT_SOURCE_DIR}/dist/languages" CACHE PATH "Path to Citra's translations for the Qt frontend")
option(GENERATE_QT_TRANSLATION "Generate en.ts as the translation source file" OFF) option(GENERATE_QT_TRANSLATION "Generate en.ts as the translation source file" OFF)
# Update source TS file if enabled # Update source TS file if enabled
@ -235,17 +236,23 @@ if (ENABLE_QT_TRANSLATION)
add_custom_target(translation ALL DEPENDS citra_qt_lupdate) add_custom_target(translation ALL DEPENDS citra_qt_lupdate)
endif() endif()
# Find all TS files except en.ts # Find all TS files for Citra translations except en.ts
file(GLOB_RECURSE LANGUAGES_TS ${CITRA_QT_LANGUAGES}/*.ts) file(GLOB_RECURSE CITRA_LANGUAGES_TS ${CITRA_QT_LANGUAGES}/*.ts)
list(REMOVE_ITEM LANGUAGES_TS ${CITRA_QT_LANGUAGES}/en.ts) list(REMOVE_ITEM CITRA_LANGUAGES_TS ${CITRA_QT_LANGUAGES}/en.ts)
# Compile TS files to QM files # Compile Citra TS files to QM files
qt_add_lrelease(citra_qt TS_FILES ${LANGUAGES_TS} NO_GLOBAL_TARGET QM_FILES_OUTPUT_VARIABLE LANGUAGES_QM) qt_add_lrelease(citra_qt TS_FILES ${CITRA_LANGUAGES_TS} NO_GLOBAL_TARGET QM_FILES_OUTPUT_VARIABLE CITRA_LANGUAGES_QM)
# Find all QM files for Qt translations
file(GLOB_RECURSE QT_LANGUAGES_QM ${BASE_QT_LANGUAGES}/qtbase_*.qm)
# Copy base QT QM files into build directory
file(COPY ${QT_LANGUAGES_QM} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
# Build a QRC file from the QM file list # Build a QRC file from the QM file list
set(LANGUAGES_QRC ${CMAKE_CURRENT_BINARY_DIR}/languages.qrc) set(LANGUAGES_QRC ${CMAKE_CURRENT_BINARY_DIR}/languages.qrc)
file(WRITE ${LANGUAGES_QRC} "<RCC><qresource prefix=\"languages\">\n") file(WRITE ${LANGUAGES_QRC} "<RCC><qresource prefix=\"languages\">\n")
foreach (QM ${LANGUAGES_QM}) foreach (QM ${QT_LANGUAGES_QM} ${CITRA_LANGUAGES_QM})
get_filename_component(QM_FILE ${QM} NAME) get_filename_component(QM_FILE ${QM} NAME)
file(APPEND ${LANGUAGES_QRC} "<file>${QM_FILE}</file>\n") file(APPEND ${LANGUAGES_QRC} "<file>${QM_FILE}</file>\n")
endforeach (QM) endforeach (QM)

View file

@ -7,6 +7,7 @@
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <thread> #include <thread>
#include <unordered_map>
#include <QFileDialog> #include <QFileDialog>
#include <QFutureWatcher> #include <QFutureWatcher>
#include <QIcon> #include <QIcon>
@ -4087,16 +4088,22 @@ void GMainWindow::LoadTranslation() {
bool loaded; bool loaded;
const QString qtbase_prefix = QStringLiteral("qtbase_");
if (UISettings::values.language.isEmpty()) { if (UISettings::values.language.isEmpty()) {
// Use the system's default locale // Use the system's default locale
loaded = translator.load(QLocale::system(), {}, {}, QStringLiteral(":/languages/")); qtTranslator.load(qtbase_prefix + QLocale::system().name(), {}, {},
QStringLiteral(":/languages/"));
loaded = citraTranslator.load(QLocale::system(), {}, {}, QStringLiteral(":/languages/"));
} else { } else {
// Otherwise load from the specified file // Otherwise load from the specified file
loaded = translator.load(UISettings::values.language, QStringLiteral(":/languages/")); qtTranslator.load(qtbase_prefix + UISettings::values.language,
QStringLiteral(":/languages/"));
loaded = citraTranslator.load(UISettings::values.language, QStringLiteral(":/languages/"));
} }
if (loaded) { if (loaded) {
qApp->installTranslator(&translator); qApp->installTranslator(&qtTranslator);
qApp->installTranslator(&citraTranslator);
} else { } else {
UISettings::values.language = QStringLiteral("en"); UISettings::values.language = QStringLiteral("en");
} }
@ -4104,7 +4111,8 @@ void GMainWindow::LoadTranslation() {
void GMainWindow::OnLanguageChanged(const QString& locale) { void GMainWindow::OnLanguageChanged(const QString& locale) {
if (UISettings::values.language != QStringLiteral("en")) { if (UISettings::values.language != QStringLiteral("en")) {
qApp->removeTranslator(&translator); qApp->removeTranslator(&qtTranslator);
qApp->removeTranslator(&citraTranslator);
} }
UISettings::values.language = locale; UISettings::values.language = locale;

View file

@ -439,7 +439,8 @@ private:
QAction* action_secondary_swap_screen; QAction* action_secondary_swap_screen;
QAction* action_secondary_rotate_screen; QAction* action_secondary_rotate_screen;
QTranslator translator; QTranslator qtTranslator;
QTranslator citraTranslator;
// stores default icon theme search paths for the platform // stores default icon theme search paths for the platform
QStringList default_theme_paths; QStringList default_theme_paths;

View file

@ -24,12 +24,17 @@ ConfigureUi::~ConfigureUi() = default;
void ConfigureUi::InitializeLanguageComboBox() { void ConfigureUi::InitializeLanguageComboBox() {
ui->language_combobox->addItem(tr("<System>"), QString{}); ui->language_combobox->addItem(tr("<System>"), QString{});
ui->language_combobox->addItem(tr("English"), QStringLiteral("en")); ui->language_combobox->addItem(QStringLiteral("English"), QStringLiteral("en"));
QDirIterator it(QStringLiteral(":/languages"), QDirIterator::NoIteratorFlags); QDirIterator it(QStringLiteral(":/languages"), QDirIterator::NoIteratorFlags);
while (it.hasNext()) { while (it.hasNext()) {
QString locale = it.next(); QString locale = it.next();
locale.truncate(locale.lastIndexOf(QLatin1Char{'.'})); locale.truncate(locale.lastIndexOf(QLatin1Char{'.'}));
locale.remove(0, locale.lastIndexOf(QLatin1Char{'/'}) + 1); locale.remove(0, locale.lastIndexOf(QLatin1Char{'/'}) + 1);
if (locale.startsWith(QStringLiteral("qtbase"))) {
// The Qt Base QM translation files are lumped in with ours,
// so don't show them in the language list!
continue;
}
QString lang = QLocale::languageToString(QLocale(locale).language()); QString lang = QLocale::languageToString(QLocale(locale).language());
const QString country = QLocale::territoryToString(QLocale(locale).territory()); const QString country = QLocale::territoryToString(QLocale(locale).territory());
if (locale == QString::fromStdString("ca_ES_valencia")) { if (locale == QString::fromStdString("ca_ES_valencia")) {