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. "Language Reverts to English When Missing .qm File"

"Language Reverts to English When Missing .qm File"

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 747 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    shreya_agrawal
    wrote on last edited by shreya_agrawal
    #1

    So, I have 3 language options in my application, English and 2 others, say Language A and Language B. Suppose that the .qm file of B is missing. I select A and the UI translates to A. Then, I select B, I get an error message box, but the language shifts to English. I want it to remain A. I don't get what I am doing wrong. Here is the function where I install and remove the translator:

    void LanguageSupport::onLanguageChanged(Language lang)
    {
        if(lang == LanguageSupport::English) {
            qApp->removeTranslator(&translator);
        }
    
        else{
            QString filePath = getLanguageFilePath(lang);
            if(!translator.load(filePath)) {
                QMessageBox::critical(nullptr, "error", "Failed to load language translation file");
                return;
            }
            qApp->installTranslator(&translator);
        }
    }
    

    Note: I am not using this translator in any other file. Its a private member of the LanguageSupport class.

    Christian EhrlicherC 1 Reply Last reply
    0
    • S shreya_agrawal

      So, I have 3 language options in my application, English and 2 others, say Language A and Language B. Suppose that the .qm file of B is missing. I select A and the UI translates to A. Then, I select B, I get an error message box, but the language shifts to English. I want it to remain A. I don't get what I am doing wrong. Here is the function where I install and remove the translator:

      void LanguageSupport::onLanguageChanged(Language lang)
      {
          if(lang == LanguageSupport::English) {
              qApp->removeTranslator(&translator);
          }
      
          else{
              QString filePath = getLanguageFilePath(lang);
              if(!translator.load(filePath)) {
                  QMessageBox::critical(nullptr, "error", "Failed to load language translation file");
                  return;
              }
              qApp->installTranslator(&translator);
          }
      }
      

      Note: I am not using this translator in any other file. Its a private member of the LanguageSupport class.

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @shreya_agrawal said in "Language Reverts to English When Missing .qm File":

      I don't get what I am doing wrong.

      You remove the old translator so how should it be used afterwards? You have to load the old ts file.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • S Offline
        S Offline
        shreya_agrawal
        wrote on last edited by
        #3

        Thank you for your reply!
        The translator is only removed when I select English language. But when I select Language A and then Language B, the translator is not being removed.

        J.HilkJ 1 Reply Last reply
        0
        • S shreya_agrawal

          Thank you for your reply!
          The translator is only removed when I select English language. But when I select Language A and then Language B, the translator is not being removed.

          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @shreya_agrawal seems like you're using the same stack allocated translator for everything:

          removeTranslator(&translator)
          
          !translator.load(filePath)
          

          probably the reason.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          S 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @shreya_agrawal seems like you're using the same stack allocated translator for everything:

            removeTranslator(&translator)
            
            !translator.load(filePath)
            

            probably the reason.

            S Offline
            S Offline
            shreya_agrawal
            wrote on last edited by
            #5

            @J-Hilk
            If I don't use the same translator, then how would it know which translator to remove when switching back to English?

            J.HilkJ 1 Reply Last reply
            0
            • S shreya_agrawal

              @J-Hilk
              If I don't use the same translator, then how would it know which translator to remove when switching back to English?

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #6

              @shreya_agrawal keep a map of QTranslator pointers and the LanguageSupport enum as key?


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              S 1 Reply Last reply
              1
              • J.HilkJ J.Hilk

                @shreya_agrawal keep a map of QTranslator pointers and the LanguageSupport enum as key?

                S Offline
                S Offline
                shreya_agrawal
                wrote on last edited by
                #7

                @J-Hilk
                I tried implementing this approach, but still facing the same issue.

                    // Load translators for all languages
                    for (auto it = languageFiles.constBegin(); it != languageFiles.constEnd(); ++it) {
                        QSharedPointer<QTranslator> translator = QSharedPointer<QTranslator>::create();
                        if (translator->load(it.value())) {
                            translators[it.key()] = translator;
                        } else {
                            qDebug() << "Failed to load translator for language:" << it.key();
                        }
                    }
                
                void LanguageSupport::onLanguageChanged(Language lang)
                {
                    // Remove all translators
                    for (auto it = translators.constBegin(); it != translators.constEnd(); ++it) {
                        qApp->removeTranslator(it.value().data());
                    }
                
                    if(lang == LanguageSupport::English) {
                        return;
                    }
                
                    auto translator = translators.value(lang);
                    if (translator) {
                        qApp->installTranslator(translator.data());
                    } else {
                        QMessageBox::critical(nullptr, "Error", "Failed to load language translation file");
                    }
                }
                

                Am I doing something wrong in this?

                J.HilkJ 1 Reply Last reply
                0
                • S shreya_agrawal

                  @J-Hilk
                  I tried implementing this approach, but still facing the same issue.

                      // Load translators for all languages
                      for (auto it = languageFiles.constBegin(); it != languageFiles.constEnd(); ++it) {
                          QSharedPointer<QTranslator> translator = QSharedPointer<QTranslator>::create();
                          if (translator->load(it.value())) {
                              translators[it.key()] = translator;
                          } else {
                              qDebug() << "Failed to load translator for language:" << it.key();
                          }
                      }
                  
                  void LanguageSupport::onLanguageChanged(Language lang)
                  {
                      // Remove all translators
                      for (auto it = translators.constBegin(); it != translators.constEnd(); ++it) {
                          qApp->removeTranslator(it.value().data());
                      }
                  
                      if(lang == LanguageSupport::English) {
                          return;
                      }
                  
                      auto translator = translators.value(lang);
                      if (translator) {
                          qApp->installTranslator(translator.data());
                      } else {
                          QMessageBox::critical(nullptr, "Error", "Failed to load language translation file");
                      }
                  }
                  

                  Am I doing something wrong in this?

                  J.HilkJ Online
                  J.HilkJ Online
                  J.Hilk
                  Moderators
                  wrote on last edited by J.Hilk
                  #8

                  @shreya_agrawal maybe this ?

                  // Remove all translators
                      for (auto it = translators.constBegin(); it != translators.constEnd(); ++it) {
                          qApp->removeTranslator(it.value().data());
                      }
                  

                  seems like you now always remove all translators and do not reinstall any when the loading fails :D


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    shreya_agrawal
                    wrote on last edited by
                    #9

                    Ah! I got your point. Now, I am removing all translators only when English is selected and then reinstalling them for the respective languages and it works as intended. Thanks !

                    1 Reply Last reply
                    1
                    • S shreya_agrawal has marked this topic as solved on

                    • Login

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