Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved how to clear QTranslator

    General and Desktop
    3
    18
    4759
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • L
      literA2 last edited by literA2

      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.

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        How are you setting up your translators ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        L 1 Reply Last reply Reply Quote 0
        • L
          literA2 @SGaist last edited by

          @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.

          VRonin 1 Reply Last reply Reply Quote 0
          • VRonin
            VRonin @literA2 last edited by

            @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

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            L 1 Reply Last reply Reply Quote 0
            • L
              literA2 @VRonin last edited by

              @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.

              1 Reply Last reply Reply Quote 0
              • VRonin
                VRonin last edited by

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

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                L 1 Reply Last reply Reply Quote 0
                • L
                  literA2 @VRonin last edited by literA2

                  @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);
                     }
                   }
                  
                  1 Reply Last reply Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    Are you re-creating the translator at some point ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    L 1 Reply Last reply Reply Quote 0
                    • L
                      literA2 @SGaist last edited by

                      @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.

                      1 Reply Last reply Reply Quote 0
                      • SGaist
                        SGaist Lifetime Qt Champion last edited by

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

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        L 1 Reply Last reply Reply Quote 1
                        • L
                          literA2 @SGaist last edited by

                          @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.

                          1 Reply Last reply Reply Quote 0
                          • VRonin
                            VRonin last edited by

                            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

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            L 1 Reply Last reply Reply Quote 0
                            • L
                              literA2 @VRonin last edited by

                              @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.

                              1 Reply Last reply Reply Quote 0
                              • VRonin
                                VRonin last edited by VRonin

                                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

                                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                ~Napoleon Bonaparte

                                On a crusade to banish setIndexWidget() from the holy land of Qt

                                L 1 Reply Last reply Reply Quote 0
                                • L
                                  literA2 @VRonin last edited by literA2

                                  @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);
                                   }
                                  
                                  VRonin 1 Reply Last reply Reply Quote 0
                                  • VRonin
                                    VRonin @literA2 last edited by VRonin

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

                                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                    ~Napoleon Bonaparte

                                    On a crusade to banish setIndexWidget() from the holy land of Qt

                                    L 1 Reply Last reply Reply Quote 0
                                    • L
                                      literA2 @VRonin last edited by

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

                                      1 Reply Last reply Reply Quote 0
                                      • SGaist
                                        SGaist Lifetime Qt Champion last edited by

                                        You should also check that translator.load runs successfully

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply Reply Quote 0
                                        • First post
                                          Last post