removeTranslator fails?
-
Hi, I am implementing switching multiple translation files (.qm files), I would like to display text contents in qml files in different languages, qstrid-based. I know that before loading a new translation file I should remove the previous translator, so I implemented some codes like:
if( ! mTranslator.isEmpty() ) { qApp->removeTranslator(&mTranslator); qInfo() << "Translator is now empty?" << mTranslator.isEmpty(); qInfo() << "Translator removal is successful?" << qApp->removeTranslator(&mTranslator); } mTranslator.load("lang_de.qm", mLangDir); qApp->installTranslator(&mTranslator);
mTranslator is declared as
QTranslator mTranslator;
However,
removeTranslator
always returned false, does that mean the removal failed?
Also, after switching different files, translated contents didn't display properly. Did the failure of removal cause this issue? Any ideas on this issue? Thank you very muchUpdate:
I resolved the problem my self, the trick part is I should add a Q_PROPERTY emtyString, this is to ensure when the language changes, there's a signal from C++ side to notify the QML to refresh the display contents.
A more detailed description can be found here: https://wiki.qt.io/How_to_do_dynamic_translation_in_QMLHowever,
removeTranslator
still returns false, but this doesn't affect the translation, I am a bit confused -
Hi,
Might be a silly question but are you sure your translations are loaded successfully as well ?
-
Hi,
Might be a silly question but are you sure your translations are loaded successfully as well ?
@SGaist Yes, I implemented the following codes to verify:
successLoad = mTranslator.load("lang_de.qm", mLangDir); successInstall = qApp->installTranslator(&mTranslator); qInfo() << "Load trans file successful?" << successLoad; qInfo() << "Trans install successful?" << successInstall;
They always printed out true.
Basically, every time when a qm file is loaded initially, everything is fine. Butwhen I try to switch to another qm file,
removeTranslator
seems failed, and the corresponding texts don't display properly -
Is mTranslator modified at some point in your application ?
Where is it located ? -
You call removeTranslator() twice so the second one will fail. Please check the return value of the first one.
-
You call removeTranslator() twice so the second one will fail. Please check the return value of the first one.
@Christian-Ehrlicher Thank you, I resolved this problem myself
-
Haaaaa ! I somehow missed that you were doing that in a QML app...
Glad you found out and thanks for sharing !
Since you have it working now please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :-)
-
Hi, I am implementing switching multiple translation files (.qm files), I would like to display text contents in qml files in different languages, qstrid-based. I know that before loading a new translation file I should remove the previous translator, so I implemented some codes like:
if( ! mTranslator.isEmpty() ) { qApp->removeTranslator(&mTranslator); qInfo() << "Translator is now empty?" << mTranslator.isEmpty(); qInfo() << "Translator removal is successful?" << qApp->removeTranslator(&mTranslator); } mTranslator.load("lang_de.qm", mLangDir); qApp->installTranslator(&mTranslator);
mTranslator is declared as
QTranslator mTranslator;
However,
removeTranslator
always returned false, does that mean the removal failed?
Also, after switching different files, translated contents didn't display properly. Did the failure of removal cause this issue? Any ideas on this issue? Thank you very muchUpdate:
I resolved the problem my self, the trick part is I should add a Q_PROPERTY emtyString, this is to ensure when the language changes, there's a signal from C++ side to notify the QML to refresh the display contents.
A more detailed description can be found here: https://wiki.qt.io/How_to_do_dynamic_translation_in_QMLHowever,
removeTranslator
still returns false, but this doesn't affect the translation, I am a bit confusedHowever,
removeTranslator
still returns false, but this doesn't affect the translation, I am a bit confusedThat's because of what @Christian-Ehrlicher said:
You call removeTranslator() twice so the second one will fail. Please check the return value of the first one.
-
Hi, I am implementing switching multiple translation files (.qm files), I would like to display text contents in qml files in different languages, qstrid-based. I know that before loading a new translation file I should remove the previous translator, so I implemented some codes like:
if( ! mTranslator.isEmpty() ) { qApp->removeTranslator(&mTranslator); qInfo() << "Translator is now empty?" << mTranslator.isEmpty(); qInfo() << "Translator removal is successful?" << qApp->removeTranslator(&mTranslator); } mTranslator.load("lang_de.qm", mLangDir); qApp->installTranslator(&mTranslator);
mTranslator is declared as
QTranslator mTranslator;
However,
removeTranslator
always returned false, does that mean the removal failed?
Also, after switching different files, translated contents didn't display properly. Did the failure of removal cause this issue? Any ideas on this issue? Thank you very muchUpdate:
I resolved the problem my self, the trick part is I should add a Q_PROPERTY emtyString, this is to ensure when the language changes, there's a signal from C++ side to notify the QML to refresh the display contents.
A more detailed description can be found here: https://wiki.qt.io/How_to_do_dynamic_translation_in_QMLHowever,
removeTranslator
still returns false, but this doesn't affect the translation, I am a bit confused@QTLeearn said in removeTranslator fails?:
I resolved the problem my self,
Great. Please don't forget to mark your post as solved!