PDA

View Full Version : qmake and localization



gordebak
10th November 2012, 12:30
Hello everyone!

This is my first post on the forums, so if I posted it in the wrong place, sorry. Moderators feel free to move it where it belongs.

I have a small Qt app, and it has a localization for Czech. But I can't seem to get it to work. Till now I tried the following

I put these lines in my app.pro file:



TRANSLATIONS += cs_CZ.ts \

isEmpty(QMAKE_LRELEASE) {
win32|os2:QMAKE_LRELEASE = $$[QT_INSTALL_BINS]\lrelease.exe
else:QMAKE_LRELEASE = $$[QT_INSTALL_BINS]/lrelease
unix {
!exists($$QMAKE_LRELEASE) { QMAKE_LRELEASE = lrelease-qt4 }
} else {
!exists($$QMAKE_LRELEASE) { QMAKE_LRELEASE = lrelease }
}
}

updateqm.input = TRANSLATIONS
updateqm.output = qm/${QMAKE_FILE_BASE}.qm
updateqm.commands = $$QMAKE_LRELEASE -silent ${QMAKE_FILE_IN} -qm qm/${QMAKE_FILE_BASE}.qm
updateqm.CONFIG += no_link target_predeps
QMAKE_EXTRA_COMPILERS += updateqm

INSTALLS += translations

translations.path = /usr/share/app
translations.files = qm/cs_CZ.qm


It compiles the translation and installs fine. But when I run the app translation doesn't work. Any ideas?

mousa_mk
11th November 2012, 10:49
Introducing the translation files in the pro file is not enough. You have to write code in your application to change the locale.

You can do this:
In the MainWindow's constructor, before calling setupUi() function (or in the main function, before creating MainWindow object) write these lines:


QTranslator translator;
translator.load("path/to/qm/file/cs_CZ.qm");
qApp->installTranslator(&translator); //Or in the case of main function: a.installTranslator(&translator);


You can get more information from the documentation of QTranslator class:
http://doc.qt.digia.com/latest/qtranslator.html

gordebak
11th November 2012, 11:11
Thank you very much. It works.

One last question, because I couldn't figure out from the documentation: Should I create a QTranslator for every translation, or can I load more than one translation into one?

Sorry if it's obvious, I couldn't figure out.

Edit: Sorry, I figured out. One for each. Thanks.

mousa_mk
11th November 2012, 16:34
In that case you can move the definition of QTranslator object into your main window class (or everywhere else, just somewhere that'll keep it) and load it like before:

at mainwindow.h file:


private:
QTranslator translator;


In MainWindow's constructor before setupUi():


translator.load("path/to/qm/file/cs_CZ.qm");
qApp->installTranslator(&translator);


When you want to change language at runtime you just load the new language and retranslate the ui:


translator.load("path/to/qm/file/en_US.qm");
ui->retranslateUi(this);


This way, you install the translator once and just change it's loaded language whenever needed.

gordebak
11th November 2012, 16:36
I just wanted to load the appropriate translation according to the system locale in the first startup. I did it reading the documentation.

Thank you very much.