how to clear QTranslator



  • Is there a way to clear the QTranslator?

    I wanted to clear it once the selected language is English.

    And what could be the cause if the QCoreApplication::removeTranslator failed to run?

    Thanks.


  • Lifetime Qt Champion

    Hi,

    How are you setting up your translators ?



  • @SGaist said in how to clear QTranslator:

    Hi,

    How are you setting up your translators ?

    Thanks for the reply.

    From main.cpp, i created a translator object and installed it if it isn't English.

    Then from another class i have another translator object, upon its instantiation i load the currently installed translation file into translator. Once the language option changes, I checked if it is not empty and remove translator. Then load again another file if the language selected is not English and install it.

    I wanted to clear the translator after the removal so that only one translator will be loaded always.

    I encountered a failure to remove the translator if by default another language was preloaded. I checked the location of the file, they're actually the same but failed to remove it.



  • @literA2 said in how to clear QTranslator:

    From main.cpp, i created a translator object and installed it if it isn't English.

    Do you really need to do it? just run lupdate and lrelease to create the English translation file that will not alter any string inside tr().
    i.e. Use an untranslated .qm to make your program default to the original string



  • @VRonin so you still have .qm file for English, with the same key and value.

    But i have this problem removing the translator installed. I can't find any issues related to it.

    I'll give a try to create one for English. Thanks.



  • can you show us the code where you add and remove the translator?



  • @VRonin said in how to clear QTranslator:

    can you show us the code where you add and remove the translator?

    loadTranslator is being called also upon instantiation of the class to load the currently installed translator.

    void loadTranslator (QString language)
    {
      if (language === "German") {
        translator.load("translation_de_DE", location);
      }  else if (language === "French") {
        translator.load("translation_fr_FR", location);
      } else if (language === "Spanish") {
        translator.load("translation_es_ES", location);
      }
    }
    
    void changeLanguage(QString language)
    {
       if (!translator.isEmpty()) {
         qApp -> removeTranslator(&translator); // this returns false once there is an existing translator installed
         // if possible i wanted to clear here the translator
       }
    
       loadTranslator(language);
    
       if (!translator.isEmpty() && (language != "English")) {
         qApp -> installTranslator(&translator);
       }
     }

  • Lifetime Qt Champion

    Are you re-creating the translator at some point ?



  • @SGaist said in how to clear QTranslator:

    Are you re-creating the translator at some point ?

    yes, as i mentioned i have a translator from the main and created another translator from another class. In another class i loaded the file that is currently installed based on the stored language.


  • Lifetime Qt Champion

    Then aren't you trying to remove a translator that wasn't installed in the first place ?



  • @SGaist said in how to clear QTranslator:

    Then aren't you trying to remove a translator that wasn't installed in the first place ?

    After checking, it actually installed successfully.



  • The problem is if &translator used in installTranslator is the same as &translator used in removeTranslator. where does translator live. if it's a local variable you are doing it wrong



  • @VRonin said in how to clear QTranslator:

    The problem is if &translator used in installTranslator is the same as &translator used in removeTranslator. where does translator live. if it's a local variable you are doing it wrong

    Shouldn't that be the case? if you remove the previously installed translator, that is also what you need to pass for it to uninstall?

    The translator in other class is private.



  • What I mean is:

    Wrong:

    {
    QTranslator translator;
    translator.load("translation_de_DE", location);
     qApp -> installTranslator(&translator);
    }
    {
    QTranslator translator;
     qApp -> removeTranslator(&translator);
    }
    

    Right:

    QTranslator translator;
    {
    translator.load("translation_de_DE", location);
     qApp -> installTranslator(&translator);
    }
    {
     qApp -> removeTranslator(&translator);
    }
    

    We'd need to see where translator is declared and from where do you call those functions to help you better



  • @VRonin osrry, it isn't declared locally on the function. I have declared the it in the header file.

    //Settings.h
    class Settings : public QObject {
        QTranslator translator;
     }
    
    // Settings.cpp
    Settings::Settings(QObject *parent)
       : QObject (parent)
    {
        loadTranslator(Profile::Language);
     }


  • @literA2 then what is the life cycle of Settings instance(s)?
    is loadTranslator a global function?



  • @VRonin loadTranslator is only a private function. Settings lifecycle is as long as the app is running.


  • Lifetime Qt Champion

    You should also check that translator.load runs successfully


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.