Overhaul loading Qt base translations:

- Now comprehensively handles all possible locations and filenames
- Embedded translation files don't have to be renamed anymore
- Fixed Qt 6 deprecation warnings
This commit is contained in:
Alexander Babikov
2025-08-15 00:31:28 +05:00
parent 9d750f579b
commit bc3caa557f
3 changed files with 51 additions and 22 deletions

View File

@@ -575,7 +575,6 @@ foreach(po_file ${po_files})
string(REGEX MATCH "[A-Z]+$" PO_COUNTRY ${PO_FILE_NAME})
# Find the base Qt translation for the language and country
set(qt_translation_file_dest "qt_${PO_LANGUAGE}_${PO_COUNTRY}.qm")
if (EXISTS "${QT_TRANSLATIONS_DIR}/qtbase_${PO_LANGUAGE}_${PO_COUNTRY}.qm")
set(qt_translation_file "qtbase_${PO_LANGUAGE}_${PO_COUNTRY}.qm")
# Fall back to just the language if country isn't found
@@ -594,13 +593,9 @@ foreach(po_file ${po_files})
# Copy the translation file to the build directory
if (qt_translation_file)
file(COPY "${QT_TRANSLATIONS_DIR}/${qt_translation_file}" DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
if (NOT (qt_translation_file STREQUAL qt_translation_file_dest))
# Rename the file for consistency
file(RENAME "${CMAKE_CURRENT_BINARY_DIR}/${qt_translation_file}" "${CMAKE_CURRENT_BINARY_DIR}/${qt_translation_file_dest}")
endif()
# Add the file to the translations list
string(APPEND QT_TRANSLATIONS_LIST " <file>${qt_translation_file_dest}</file>\n")
list(APPEND QM_FILES "${CMAKE_CURRENT_BINARY_DIR}/${qt_translation_file_dest}")
string(APPEND QT_TRANSLATIONS_LIST " <file>${qt_translation_file}</file>\n")
list(APPEND QM_FILES "${CMAKE_CURRENT_BINARY_DIR}/${qt_translation_file}")
endif()
endif()

View File

@@ -197,31 +197,64 @@ ProgSettings::loadTranslators(QObject *parent)
for (int i = 0; i < QLocale::system().uiLanguages().size(); i++) {
localetofilename = QLocale::system().uiLanguages()[i];
if (translator->load(QLatin1String("86box_") + localetofilename, QLatin1String(":/"))) {
qDebug() << "Translations loaded.\n";
qDebug() << "Translations loaded.";
QCoreApplication::installTranslator(translator);
if (!qtTranslator->load(QLatin1String("qtbase_") + localetofilename.replace('-', '_'), QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
if (!qtTranslator->load(QLatin1String("qtbase_") + localetofilename.left(localetofilename.indexOf('-')), QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
if (!qtTranslator->load(QLatin1String("qt_") + localetofilename.replace('-', '_'), QApplication::applicationDirPath() + "/./translations/"))
qtTranslator->load(QLatin1String("qt_") + localetofilename.replace('-', '_'), QLatin1String(":/"));
if (QApplication::installTranslator(qtTranslator)) {
qDebug() << "Qt translations loaded."
<< "\n";
}
/* First try qtbase */
if (!loadQtTranslations(QLatin1String("qtbase_") + localetofilename.replace('-', '_')))
/* If that fails, try legacy qt_* translations */
if (!loadQtTranslations(QLatin1String("qt_") + localetofilename.replace('-', '_')))
qDebug() << "Failed to find Qt translations!";
if (QCoreApplication::installTranslator(qtTranslator))
qDebug() << "Qt translations loaded.";
break;
}
}
} else {
translator->load(QLatin1String("86box_") + languages[lang_id].first, QLatin1String(":/"));
if (translator->load(QLatin1String("86box_") + languages[lang_id].first, QLatin1String(":/")))
qDebug() << "Translations loaded.";
QCoreApplication::installTranslator(translator);
if (!qtTranslator->load(QLatin1String("qtbase_") + QString(languages[lang_id].first).replace('-', '_'), QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
if (!qtTranslator->load(QLatin1String("qtbase_") + QString(languages[lang_id].first).left(QString(languages[lang_id].first).indexOf('-')), QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
if(!qtTranslator->load(QLatin1String("qt_") + QString(languages[lang_id].first).replace('-', '_'), QApplication::applicationDirPath() + "/./translations/"))
qtTranslator->load(QLatin1String("qt_") + QString(languages[lang_id].first).replace('-', '_'), QLatin1String(":/"));
/* First try qtbase */
if (!loadQtTranslations(QLatin1String("qtbase_") + QString(languages[lang_id].first).replace('-', '_')))
/* If that fails, try legacy qt_* translations */
if (!loadQtTranslations(QLatin1String("qt_") + QString(languages[lang_id].first).replace('-', '_')))
qDebug() << "Failed to find Qt translations!";
QCoreApplication::installTranslator(qtTranslator);
if (QCoreApplication::installTranslator(qtTranslator))
qDebug() << "Qt translations loaded.";
}
}
bool
ProgSettings::loadQtTranslations(const QString name)
{
QString name_lang_only = name.left(name.indexOf('_'));
/* System-wide translations */
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
if (qtTranslator->load(name, QLibraryInfo::path(QLibraryInfo::TranslationsPath)))
#else
if (qtTranslator->load(name, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
#endif
return true;
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
else if (qtTranslator->load(name_lang_only, QLibraryInfo::path(QLibraryInfo::TranslationsPath)))
#else
else if (qtTranslator->load(name_lang_only, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
#endif
return true;
/* Bundled translations (embedded) */
else if (qtTranslator->load(name, QLatin1String(":/")))
return true;
else if (qtTranslator->load(name_lang_only, QLatin1String(":/")))
return true;
/* Bundled translations (external) */
else if (qtTranslator->load(name, QApplication::applicationDirPath() + "/./translations/"))
return true;
else if (qtTranslator->load(name_lang_only, QApplication::applicationDirPath() + "/./translations/"))
return true;
else
return false;
}
void
ProgSettings::on_pushButtonLanguage_released()
{

View File

@@ -49,6 +49,7 @@ private slots:
private:
Ui::ProgSettings *ui;
static bool loadQtTranslations(const QString name);
friend class MainWindow;
double mouseSensitivity;