Translation does not work with Qt 6.7
-
Hello All,
I found a problem with application translations that do not work since Qt 6.7 (it works fine with Qt 6.6.3). It seems that the problem is with loading of the translation files because *.qm files are generated as before.
CMakeLists.txt
set(TSFILES l10n/${TARGET}_cs.ts l10n/${TARGET}_de.ts l10n/${TARGET}_pl.ts ) qt_add_translations(${TARGET} TS_FILES ${TSFILES} LUPDATE_OPTIONS "-noobsolete" )
main.cpp
QLocale::Language lang = QLocale::codeToLanguage(settings->value("language", QString("")).toString()); QTranslator translator; if (QLocale::AnyLanguage == lang) { // system default if (translator.load(QLocale(), QLatin1String("AbracaDABra"), QLatin1String("_"), QLatin1String(":/i18n"))) { a.installTranslator(&translator); } } else if (QLocale::English != lang) { // user selected translation if (translator.load(QString("AbracaDABra_%1").arg(QLocale::languageToCode(lang)), QLatin1String(":/i18n"))) { a.installTranslator(&translator); } }
translator.load()
returns false in both cases and no translation is loaded.
I found there are some changes in qt_add_translations but I think I call it in compatible way.Any idea? Thanks!
-
They should be added according to documentation
Qt 6.5.3
":/i18n" ":/i18n/AbracaDABra_pl.qm" ":/i18n/AbracaDABra_cs.qm" ":/i18n/AbracaDABra_de.qm"
Qt 6.7.0
":/i18n" ":/i18n/gui" ":/i18n/gui/AbracaDABra_pl.qm" ":/i18n/gui/AbracaDABra_cs.qm" ":/i18n/gui/AbracaDABra_de.qm"
So it is added in both cases but 6.7 adds subdirectory according to project structure (translation files are handled from
CMakeLists.txt
located ingui
directory). It is probably correct behavior but it makes the code incompatible. I will solve it by compiler switch and make this topic as solved.Thank you for your help!
-
How to you add the files to your resource? Make sure they are at the location you're expecting them. Use e.g. QFile::exists().
-
I do not actually add them in resources explicitly but it was always working so far. The complete source code of my app is on GitHub: https://github.com/KejPi/AbracaDABra
-
Please check with QFile::exists() ...
-
Yes, that's it.
Application compiled with:
Qt6.5.3: File exists? ":/i18n/AbracaDABra_de.qm" true
Qt6.7.0: File exists? ":/i18n/AbracaDABra_de.qm" false
So it means that with Qt 6.7 it is not added to resources automatically. So the question is how to add it to resources automatically as it is done with Qt < 6.7?
Note: I can see the file is generated in both cases. -
@KejPi said in Translation does not work with Qt 6.7:
So it means that with Qt 6.7 it is not added to resources automatically.
Please iterate the resource with QDirIterator to see where it was added. I doubt it is not added at all. From your CMakeLists.txt I also don't see why they should be under "i18n" subdirectory.
-
They should be added according to documentation
Qt 6.5.3
":/i18n" ":/i18n/AbracaDABra_pl.qm" ":/i18n/AbracaDABra_cs.qm" ":/i18n/AbracaDABra_de.qm"
Qt 6.7.0
":/i18n" ":/i18n/gui" ":/i18n/gui/AbracaDABra_pl.qm" ":/i18n/gui/AbracaDABra_cs.qm" ":/i18n/gui/AbracaDABra_de.qm"
So it is added in both cases but 6.7 adds subdirectory according to project structure (translation files are handled from
CMakeLists.txt
located ingui
directory). It is probably correct behavior but it makes the code incompatible. I will solve it by compiler switch and make this topic as solved.Thank you for your help!
-