Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 much

    Update:

    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_QML

    However, removeTranslator still returns false, but this doesn't affect the translation, I am a bit confused


  • Lifetime Qt Champion

    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


  • Lifetime Qt Champion

    Is mTranslator modified at some point in your application ?
    Where is it located ?



  • @SGaist mTranslator is declared as the private member of this Class, QTranslator mTranslator I don't think it's being changed somewhere else. Thank you


  • Lifetime Qt Champion

    You call removeTranslator() twice so the second one will fail. Please check the return value of the first one.



  • @SGaist Thank you for your help, I resolved this problem myself. If you would like to check the update in my question



  • @Christian-Ehrlicher Thank you, I resolved this problem myself


  • Lifetime Qt Champion

    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 :-)



  • @QTLeearn

    However, removeTranslator still returns false, but this doesn't affect the translation, I am a bit confused

    That'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.



  • @QTLeearn said in removeTranslator fails?:

    I resolved the problem my self,

    Great. Please don't forget to mark your post as solved!


Log in to reply