Unsolved How to implement i18N whiling generating the GUI dynamically ?
-
We want to generating the GUI dynamically through reading XML files, and at the same time ,the GUI should support i18N , so , the linguist method CAN NOT be implemented .
How to resolve this problem ?
-
Hi
UI files are xml files.
http://doc.qt.io/qt-5/quiloader.html
So its can already do what you want.Also the linguist way can work as it's already working with UI files.
so if you use the tr() macro and do as docs says
when loading a translator , it will work.The translation with linguist is a lookup system and all that is needed is to re-set the text if u change language.
as seens in
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0));
pushButton->setText(QApplication::translate("MainWindow", "Go", 0));
} // retranslateUiSo even if you use your own xml format and generate widgets on the fly from it, then
the translator way, is still as valid as always. Same thing must happen. Re-apply the text.So im not sure i agree that "the linguist method CAN NOT be implemented " :)
-
I mean the GUI's character is stored in the xml files, NOT in the source code , and I designed all the GUI by C++,
so the linguist method CAN NOT be implemented.
-
@cyberpunker
QTranslator/tr()/... are all based on linguist which you do not want to use.Thus you will have to implement your custom translator, which reads the values from your XML file. Then instead of using Qt's
tr()
you useMyCustomTranslator::tr()
which looks up the translation in the XML file (or better in a cached structure). If it can't find the value it simply returns the value.
Whenever the loaded language in your custom translator changes, send anQEvent::LanguageChange
event to the QApplicationInstance. This propagates the event to all widgets.
Then listen in your widgets for the LanguageChange event and reapply the strings again. -
OK , I got it .
Also , what is " a cached structure" ? Any grammar indicate it ?
And at the same time , GUI need to change languages dynamically, and if the other parts of the application implements the linguist way , I think this method does NOT conflict with it , right ?
Thanks very much.
-
@cyberpunker said in How to implement i18N whiling generating the GUI dynamically ?:
Also , what is " a cached structure" ? Any grammar indicate it ?
i meant not to search the XML file everytime you request a translation. Instead load the XML values into memory (QHash, ....)
And at the same time , GUI need to change languages dynamically, and if the other parts of the application implements the linguist way , I think this method does NOT conflict with it , right ?
right.
Only the LanguageChanged event is in common. So if you use this it might also trigger Linguist (re-)translations of course.
You can overcome this by registering your custom event type and listen to this instead.