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. Qt application with dinamic language (QTranslator) does not work
Forum Updated to NodeBB v4.3 + New Features

Qt application with dinamic language (QTranslator) does not work

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 754 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.
  • Diego DonateD Offline
    Diego DonateD Offline
    Diego Donate
    wrote on last edited by A Former User
    #1

    I have a Qt application and I need to change language at runtime. I am using QTranslator and .qm files. The language is loaded properly but my widgets don't show the text.

    Y have 'MyClass..cpp' (inherits QMainWindow):

    MyClass::MyClass()
    {
       ...
       selectLanguage("en");
    
       m_pLabel = new QLabel(tr("User"));
       m_pLabel->show();
       ...
    }
    
    void MyClass::selectLanguage(QString language) {
    
       QString sTranslationFile = QString("texts_%1").arg(language);
    
       QTranslator translator;
       //translator.load(":/translations/" + sTranslationFile);
       translator.load(sTranslationFile, ":/translations/");
    
       if (qApp->installTranslator(&translator))
           qDebug() << "Ok";
     }
    
     void MyClass::changeEvent(QEvent *pEvent)
     {
         if(pEvent)
         {
             switch(pEvent->type())
             {
                   case QEvent::LanguageChange:
                      retranslate();
                      break;
               }
         }
    
         QMainWindow::changeEvent(pEvent);
     }
    
     void MyClass::retranslate()
     {
      qDebug() << "retranslate";
    
      if (m_pLabel)
         m_pLabel->setText(tr("User"));
     }
    

    In 'myProject.pro':

     ...
     QMAKE_POST_LINK = lrelease.exe myProject.pro
     ...
     RESOURCES += \
         resources/resources.qrc
    
     TRANSLATIONS += resources/translations/texts_en.ts \
                     resources/translations/texts_es.ts
     ...
     DISTFILES += \
          ...
          resources/translations/texts_en.qm \
               resources/translations/texts_es.qm
    

    In 'resources.qrc':

     ...
     translations/texts_en.qm
     translations/texts_es.qm
    

    In 'texts_en.ts':

     <!DOCTYPE TS><TS>
     <context>
         <name>MyClass</name>
         <message>
             <source>User</source>
             <translation>UserEn</translation>
         </message>
         <message>
             <source>Group</source>
             <translation>GroupEn</translation>
         </message>
     </context>
     </TS>
    

    Language is loaded ("Ok" appears) and "retranslate" is called, but the label does NOT show "UserEn", but "User"...

    I call 'selectLanguage("es")' after a while, but the same...

    I hope somebody could help me.

    Thanks in advance,

    Diego

    1 Reply Last reply
    0
    • O Offline
      O Offline
      oberluz
      wrote on last edited by
      #2

      @Diego-Donate said:

      translator.load(sTranslationFile, ":/translations/");

      How do you know the language loaded properly if you didn't test the return value of translator.load?

      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