Translating between NL and EN
-
Hi all!
I am using PySide 6 and have created an application. I want to be able to change language during runtime. The .ui I created in dutch (which is the default) language. Now when you switch to English (which I managed to do) and want to switch back it doesn't work.This is the code I am using, it runs when a user changes a combobox:
def changeLanguage(self): language = str(self.ui.cmbLanguage.currentText()) if(language == "NL"): self.translator.load("translations/nl.qm") elif(language == "EN"): self.translator.load("translations/en.qm") QApplication.instance().installTranslator(self.translator) self.retranslateUI() #self.ui.retranslateUi(self) def retranslateUI(self): for i in range(self.ui.cmbLevering.count()): self.ui.cmbLevering.setItemText(i, self.tr(self.ui.cmbLevering.itemText(i)))
I feel like the dutch translations are redundant. The program is originally written in dutch. Though the tr() function needs the string to translate. And after translation this string is in English. When trying to translate back the QTranslator is referring to the English string but can't find that as a source text in my dutch (nl.ts) translation file.
My nl.ts file:
<TS version="2.1" language="nl_NL" sourcelanguage="en">
My en.ts file:
<TS version="2.1" language="en_GB" sourcelanguage="nl">
To explain it a little bit easier. The source text in the nl.ts file is dutch itself. So it can't translate from English to Dutch.
I am not familiar with the proper way to use translation in Qt applications. Can someone explain me what a better way would be? Is the nl.ts file necessary? I have read it might not be necessary, but could be handy (and I agree). Therefore I would still like to have the separate files for nl.ts and en.ts.
Hope someone can help me. Thanks in advance!
Raymond
-
@RayKn said in Translating between NL and EN:
I feel like the dutch translations are redundant. The program is originally written in dutch. Though the tr() function needs the string to translate. And after translation this string is in English. When trying to translate back the QTranslator is referring to the English string but can't find that as a source text in my dutch (nl.ts) translation file.
Every installTranslator() call adds an additional translator. The documentation says
Multiple translation files can be installed. Translations are searched for in the reverse order in which they were installed, so the most recently installed translation file is searched first and the first translation file installed is searched last. The search stops as soon as a translation containing a matching string is found.
So, if your .nl file doesn't contain a full translation, it will always fall back to the .en file that was installed first.
The easiest way to fix this is to explicitly remove the previous translation via QTranslator::removeTranslator().
Alternatively, just don't allow dynamic retranslations. Instead, retrieve the desired language from the settings at application startup, and ask people to restart your app when they want to change their UI language. This is often 'good enough', and way easier to implement and test ....
-
Thank you for your response @kkoehne
I feel like I have full translations in place. See screenshot below:
What do I do at initialization? Do I install the dutch translator even though the program is already set at dutch (also in QT Creator Designer the language is set to dutch).
I really feel like I am missing something.
Raymond
-
@RayKn said in Translating between NL and EN:
What do I do at initialization? Do I install the dutch translator even though the program is already set at dutch (also in QT Creator Designer the language is set to dutch).
I really feel like I am missing something.The Qt translation system at runtime doesn't know anything about specific langauges. It just checks the installed QTranslators one by one whether they provide a translation for the source string / context combo, falling back to the original source string if nothing is found.
If you don't need a nl translation because all the source strings are already in correct language , then you can as well remove the .nl file. Then all you have to do to switch from English to Dutch is to remove the translation file by calling QCoreApplication::removeTranslator() with the QTranslator object as argument.
Sometimes it's useful though to keep a (partial) translation, for instance to be able to fix typos, or to handle plural forms correctly. In this case, you might want to have a partial .ts file for NL (-pluralsonly argument).
That said, if it's true that the nl file is contains a correct translation, then your setup should actually work (even if slightly inorthodox). I suggest at least checking the return value of your installTranslator calls...
Kai