Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to clear QTranslator

how to clear QTranslator

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 3 Posters 5.9k Views
  • 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 Offline
    L Offline
    literA2
    wrote on last edited by literA2
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • SGaistS SGaist

        Hi,

        How are you setting up your translators ?

        L Offline
        L Offline
        literA2
        wrote on last edited by
        #3

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

        VRoninV 1 Reply Last reply
        0
        • L literA2

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

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @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
          0
          • VRoninV VRonin

            @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

            L Offline
            L Offline
            literA2
            wrote on last edited by
            #5

            @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
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              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
              0
              • VRoninV VRonin

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

                L Offline
                L Offline
                literA2
                wrote on last edited by literA2
                #7

                @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
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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
                  0
                  • SGaistS SGaist

                    Are you re-creating the translator at some point ?

                    L Offline
                    L Offline
                    literA2
                    wrote on last edited by
                    #9

                    @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
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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
                      1
                      • SGaistS SGaist

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

                        L Offline
                        L Offline
                        literA2
                        wrote on last edited by
                        #11

                        @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
                        0
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #12

                          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
                          0
                          • VRoninV VRonin

                            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

                            L Offline
                            L Offline
                            literA2
                            wrote on last edited by
                            #13

                            @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
                            0
                            • VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by VRonin
                              #14

                              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
                              0
                              • VRoninV 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

                                L Offline
                                L Offline
                                literA2
                                wrote on last edited by literA2
                                #15

                                @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);
                                 }
                                
                                VRoninV 1 Reply Last reply
                                0
                                • L 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);
                                   }
                                  
                                  VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on last edited by VRonin
                                  #16

                                  @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
                                  0
                                  • VRoninV VRonin

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

                                    L Offline
                                    L Offline
                                    literA2
                                    wrote on last edited by
                                    #17

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

                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      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
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved