strange behaviour of QLocale with QTranslator
-
Hi,
when I use QLocale with default constructor, then QTranslator fails to load translated texts. When I print the settings of QLocale - they are the same, as with second usage using explicit constructor.
This is the code:
//#define USE_SYSTEM_LOCALE int main(int argc, char *argv[]) { QTranslator translator; QApplication a(argc, argv); #ifdef USE_SYSTEM_LOCALE QLocale curLocale; /* * qDebug output: * * current locale settings - lang: QLocale::German country: QLocale::Germany * locale messages found: false */ #else QLocale curLocale(QLocale::German, QLocale::Germany); /* * qDebug output: * * current locale settings - lang: QLocale::German country: QLocale::Germany * locale messages found: true */ #endif bool ok = translator.load(curLocale , "QtScreen" , "_" , "../QtScreen/src/i18n"); qDebug() << "current locale settings - lang: " << curLocale.language() << "\tcountry: " << curLocale.country(); qDebug() << "locale messages found: " << ok; a.installTranslator(&translator); MainWindow w; w.show(); return a.exec(); }Any idea, how I could load localized strings without hardcoding my locale?
-
Hi,
ridiculous and strange, but I found a workaround:
QLocale sysLocale; QLocale::Language lang = sysLocale.language(); QLocale::Country country = sysLocale.country(); QLocale curLocale(lang, country);with that curLocale QTranslator finds the messages =:>
-
Hi,
Pretty strange indeed...
Which version of Qt are you using ?
On which platform ? -
I use qt5.15.2 at debian 11 (bullseye)
The solution sounds so unbelievable, that I did't check it earlier.
But hey - the workaround does what I want, so ... :) -
Would it be possible for you to provide a minimal compilable example that triggers this ?
-
Hi,
you could have a look at https://github.com/DjangoReinhard/QtUi
Its not minimal, but compilable.You just need to look at main.cpp - current version is fixed to English.
So add a few comments and it should run/fail as described. -
Did you check what
sysLocale.name()returns ?I just tested with sysLocale on my system and it worked correctly (using a copy of your file renamed using my sysLocale name).
-
Result:
current locale settings - lang: QLocale::German country: QLocale::Germany name: "de_DE" syslocale.name: "de_DE" locale messages found: falsefrom code:
int main(int argc, char *argv[]) { QTranslator translator; QApplication a(argc, argv); QLocale sysLocale; QLocale::Language lang = sysLocale.language(); QLocale::Country country = sysLocale.country(); QLocale curLocale; // (lang, country); bool ok = translator.load(curLocale , "QtUi" , "_" , "../QtUi/src/i18n"); qDebug() << "current locale settings - lang: " << curLocale.language() << "\tcountry: " << curLocale.country() << "\tname: " << curLocale.name(); qDebug() << "syslocale.name: " << sysLocale.name(); qDebug() << "locale messages found: " << ok; a.installTranslator(&translator); MainWindow w; w.show(); return a.exec(); }... by the way: using Qt 6.2 works as expected :)
-
5.15.2 from the online installer ?
Did you try with the one provided with your distribution ?On my side it's 5.15.2 on macOS.
-
@SGaist said in strange behaviour of QLocale with QTranslator:
Did you try with the one provided with your distribution ?
Of cause, that was my first choice. Qt 5.15.2 on debian bullseye
Then with increasing issues I was curious about newer qt releases, so i started to use the online installer ...
-
In that case, can you test the 5.15.2 from the installer ?
-
Hi,
I wasn't sure about my memory, so I repeated the tests:
- 5.15.2 provided by debian bullseye: fail
- 5.15.2 online installer: fail
- 6.2 online installer: works as expected
-
Did you try to compare the system QLocale with the one you build by hand ?
-
If you look at my post before your last 3 questions you see the code, I'm using.