Qt C++ How to get locale language ?
-
Hello, my C++ application works but it's only in my language which is french. I'm trying to make it appear in English when user's locale is not french. I used QT Linguist and create two .tr files, one for french and another one for english, then I created the .qm files and now I'm trying to use those .qm files into my main function that is now :
#include "mainwindow.h" #include <QApplication> #include <QTranslator> int main(int argc, char *argv[]) { QApplication a(argc, argv); QTranslator translator; QLocale locale = QLocale(); QString lang = QLocale::languageToString(locale.language()); qInfo() << locale; if (translator.load(QLocale(), lang == "French" ? "UI1_fr.qm" : "UI1_en.qm")) QApplication::installTranslator(&translator); MainWindow w; w.show(); return a.exec(); }
To test I changed my system language to English but my code always shows "French" as language string. Maybe
QLocale locale = QLocale();
is not the good way to retrieve the system locale ? Or maybe do I need to restart the system (I'm on Ubuntu 22.04 and will also test on Windows 11) ?
-
Well, with your help I was able to make it work. I needed to replace
":/Translations"
by
"Translations"I don't know the meaning of the ":/" at the beginning of the string, (maybe does it mean "from the current directory" ?) but without it it worked.
here's my working main()
int main(int argc, char *argv[]) { QApplication a(argc, argv); QTranslator translator; // fichier de la forme ":/Translations/App_xx.qm" QString tr_folder="Translations"; QString tr_file="UI1_"+QLocale::system().name(); if(translator.load(tr_file,tr_folder)) QApplication::installTranslator(&translator); else { // pas trouvé qCritical("\"%s\" file not Found. No translation will occur.",qPrintable(tr_folder+"/"+tr_file)); } MainWindow w; w.show(); return a.exec(); }
-
@Gilboonet well, what is your system's locale? And, if you want to change that for your application, you can use the set default method.
-
@mzimmers I'm not willing to change my locale at runtime, only read what is the default locale and if it's french load the french .qm otherwise load the english .qm
@mzimmers I just reboot and after that, my system's locale (which I changed from French to English) was correctly retrieved. But my Application is still in french, so I supposed I did something wrong with QT Linguist.
Here's what I did with QT Linguist :
- edit each string (mainly menus entries) and translate them
- save the .tr file
And from the C++ project, I used Tools/Extern/Linguist/lrelease
-
On a side note, you are not building the file path correctly. See the load method you are using documentation .
-
Hi @Gilboonet
Here the code that I'm using:
// fichier de la forme ":/Translations/App_xx.qm" QString tr_folder=":/Translations"; QString tr_file="App_"+QLocale::system().name(); translator=new QTranslator(this); if(translator->load(tr_file,tr_folder)) installTranslator(translator); else { // pas trouvé qCritical("\"%s\" file not Found. No translation will occur.",qPrintable(tr_folder+"/"+tr_file)); }
-
Well, with your help I was able to make it work. I needed to replace
":/Translations"
by
"Translations"I don't know the meaning of the ":/" at the beginning of the string, (maybe does it mean "from the current directory" ?) but without it it worked.
here's my working main()
int main(int argc, char *argv[]) { QApplication a(argc, argv); QTranslator translator; // fichier de la forme ":/Translations/App_xx.qm" QString tr_folder="Translations"; QString tr_file="UI1_"+QLocale::system().name(); if(translator.load(tr_file,tr_folder)) QApplication::installTranslator(&translator); else { // pas trouvé qCritical("\"%s\" file not Found. No translation will occur.",qPrintable(tr_folder+"/"+tr_file)); } MainWindow w; w.show(); return a.exec(); }
-
-
The ":/" is the root of Qt's resource system path.