Translate content from variable dynamically
-
Hi friends,
i need a way to translate the content of a variable which gets set at runtime.For example:
QString textToTranslate = getTextFromSomewhere(); QLabel *t = new QLabel(tr(textToTranslate));Now the label should show the translated text.
For example if textToTranslate is "HOUSE" it should show "House" for english or "Haus" for german, but if textToTranslate becomes "TREE" because the getTextFromSomewhere() returns it at another call it should become "Tree" for english and "Baum" for german...Any idea how to handle this "dynamic" translations?
Greetings
Marco -
@CloudL said in Translate content from variable dynamically:
Any idea how to handle this "dynamic" translations?
Qt is not google translate - so you have to add your words/sentences you want to translate to your translation file.
-
Hi friends,
i need a way to translate the content of a variable which gets set at runtime.For example:
QString textToTranslate = getTextFromSomewhere(); QLabel *t = new QLabel(tr(textToTranslate));Now the label should show the translated text.
For example if textToTranslate is "HOUSE" it should show "House" for english or "Haus" for german, but if textToTranslate becomes "TREE" because the getTextFromSomewhere() returns it at another call it should become "Tree" for english and "Baum" for german...Any idea how to handle this "dynamic" translations?
Greetings
Marco@CloudL once you loaded the new translation, you have to reset all strings you manually(in your code) set previously.
Only string inside your UI files or qml files (qsTr() marked, may eventually be reset "automatically" after the translation is loaded.
so any label created this way
QString textToTranslate = getTextFromSomewhere(); QLabel *t = new QLabel(tr(textToTranslate));has to either be recreated, or set via setText
QString textToTranslate = getTextFromSomewhere(); t->setText(tr(textToTranslate));