Translation issue: use another file for a certain form
-
Hello guys,
I am developing a program where at the beginning I should use a translation file. However, I have to change the translation file for a certain class (Game). In other words, I want to separate all gui translations into a file and this game translation in another file.
Steps :
1- separted the game from the program
2- build the translation file of the game, translated and made the release.
3- Problem --->> Translation is not shown. I need help. In the main constructor I placed the following code@
QApplication a(argc, argv);KeyBoardMap *w = new Game(); QTranslator translator; translator.load("ab_ab.qm"); a.installTranslator(&translator);
@
Is this correct?
I should then merge this game class into the other software, can I load a translation file from a form.cpp and not from main.cpp?
thanks
-
does your ab_ab.qm file exist in the same folder where your executable is?
bq. I should then merge this game class into the other software, can I load a translation file from a form.cpp and not from main.cpp?
Sure you can, but be sure to create your QTranslator instance on the heap... and use qApp() instead of a
-
Yes the path is correct, here is a simple example where the complete path is removed.
how to use qApp? It does not accept
qApp.installTranslator ?
do you mean the form that loads the game class should be an Application class?
-
bq. Yes the path is correct, here is a simple example where the complete path is removed.
Do you actually use QObject::tr() or translate() in your code for strings which needs to be translated?
bq. how to use qApp? It does not accept
@
qApp->installTranslator
@It is only needed if you will load your translation elsewhere when in main function...
-
And I have forgot to say:
bq. Note that the translator must be created before the application's widgets.
-
Thank you, now it works
@
QTranslator* translator = new QTranslator;
translator->load("/.../.../.../abc.qm");
qApp->installTranslator(translator);
@But it works from inside the form. Will this be global, I mean will other forms use the same translation file ? should I load the other translation file again after this form quits?
-
It is application global, but you must be sure what QTranslator instance exist till you unload translation or loads another one... Make it global or singleton.
-
Hi SherifOmran,
you can use multiple QTranslator instances in one process, that is no problem. Just the translations must be unique. If you use tr() within a QObject, the class name is also part of the search logic, which makes it easier. If you use QObject::translate(...) you add a context as parameter which has the same meaning as the class in a QObject derived class (which is used as context there).
So using multiple qm files for different classes / libraries is no problem. At least, my apps always load 2 (my local one and one for Qt).