Results 1 to 4 of 4

Thread: QT application with dinamic language (QTranslator) does not work

  1. #1
    Join Date
    Apr 2013
    Posts
    61
    Qt products
    Qt4
    Platforms
    Windows

    Default QT application with dinamic language (QTranslator) does not work

    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):

    Qt Code:
    1. MyClass::MyClass()
    2. {
    3. ...
    4. selectLanguage("en");
    5.  
    6. m_pLabel = new QLabel(tr("User"));
    7. m_pLabel->show();
    8.  
    9. ...
    10. }
    11.  
    12. void MyClass::selectLanguage(QString language) {
    13.  
    14. QString sTranslationFile = QString("texts_%1").arg(language);
    15.  
    16. QTranslator translator;
    17. //translator.load(":/translations/" + sTranslationFile);
    18. translator.load(sTranslationFile, ":/translations/");
    19.  
    20. if (qApp->installTranslator(&translator))
    21. qDebug() << "Ok";
    22. }
    23.  
    24. void MyClass::changeEvent(QEvent *pEvent)
    25. {
    26. if(pEvent)
    27. {
    28. switch(pEvent->type())
    29. {
    30. case QEvent::LanguageChange:
    31. retranslate();
    32. break;
    33. }
    34. }
    35.  
    36. QMainWindow::changeEvent(pEvent);
    37. }
    38.  
    39. void MyClass::retranslate()
    40. {
    41. qDebug() << "retranslate";
    42.  
    43. if (m_pLabel)
    44. m_pLabel->setText(tr("User"));
    45. }
    To copy to clipboard, switch view to plain text mode 

    In 'myProject.pro':

    Qt Code:
    1. ...
    2. QMAKE_POST_LINK = lrelease.exe myProject.pro
    3. ...
    4. RESOURCES += \
    5. resources/resources.qrc
    6.  
    7. TRANSLATIONS += resources/translations/texts_en.ts \
    8. resources/translations/texts_es.ts
    9. ...
    10. DISTFILES += \
    11. ...
    12. resources/translations/texts_en.qm \
    13. resources/translations/texts_es.qm
    14. In 'resources.qrc':
    15.  
    16. ...
    17. translations/texts_en.qm
    18. translations/texts_es.qm
    To copy to clipboard, switch view to plain text mode 

    In 'texts_en.ts':

    Qt Code:
    1. <!DOCTYPE TS><TS>
    2. <context>
    3. <name>MyClass</name>
    4. <message>
    5. <source>User</source>
    6. <translation>UserEn</translation>
    7. </message>
    8. <message>
    9. <source>Group</source>
    10. <translation>GroupEn</translation>
    11. </message>
    12. </context>
    13. </TS>
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT application with dinamic language (QTranslator) does not work

    You claim that the translations are successfully installed, but your code does not check the return value of QTranslator::load().

    Anyway, the main problem is that MyClass::selectLanguage() allocates the QTranslator on the stack, which means that it is destroyed as soon as you exit the function. Qt's documentation is weak when it comes to specifying lifetime requirements, but it seems to me that the QTranslator passed to QCoreApplication::installTranslator() must live until it is removed or the QCoreApplication itself is destroyed, whichever comes first.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QT application with dinamic language (QTranslator) does not work

    The observation that installTranslator() accepts a pointer and not a "const QTranslator&" is a fairly good indicator that it does not take a copy. I agree that perhaps the doc should say that and also whether the QCoreApplication takes ownership of the QTranslator instance.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT application with dinamic language (QTranslator) does not work

    Quote Originally Posted by ChrisW67 View Post
    The observation that installTranslator() accepts a pointer and not a "const QTranslator&" is a fairly good indicator that it does not take a copy.
    And QTranslator being a QObject derived class, which means it cannot even be copied.

    Cheers,
    _

Similar Threads

  1. Static vs.dinamic linking (LGPL)
    By cpuinthebrain in forum Newbie
    Replies: 5
    Last Post: 28th June 2015, 08:07
  2. Replies: 2
    Last Post: 6th December 2011, 11:52
  3. Replot dinamic curve.
    By Zikoel in forum Qwt
    Replies: 2
    Last Post: 24th October 2011, 09:50
  4. Change language of the application
    By franco.amato in forum Newbie
    Replies: 1
    Last Post: 14th October 2010, 22:45
  5. QResource dinamic append resource on run app
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 27th November 2006, 19:30

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.