Results 1 to 5 of 5

Thread: qmake and localization

  1. #1
    Join Date
    May 2010
    Posts
    3
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default qmake and localization

    Hello everyone!

    This is my first post on the forums, so if I posted it in the wrong place, sorry. Moderators feel free to move it where it belongs.

    I have a small Qt app, and it has a localization for Czech. But I can't seem to get it to work. Till now I tried the following

    I put these lines in my app.pro file:

    Qt Code:
    1. TRANSLATIONS += cs_CZ.ts \
    2.  
    3. isEmpty(QMAKE_LRELEASE) {
    4. win32|os2:QMAKE_LRELEASE = $$[QT_INSTALL_BINS]\lrelease.exe
    5. else:QMAKE_LRELEASE = $$[QT_INSTALL_BINS]/lrelease
    6. unix {
    7. !exists($$QMAKE_LRELEASE) { QMAKE_LRELEASE = lrelease-qt4 }
    8. } else {
    9. !exists($$QMAKE_LRELEASE) { QMAKE_LRELEASE = lrelease }
    10. }
    11. }
    12.  
    13. updateqm.input = TRANSLATIONS
    14. updateqm.output = qm/${QMAKE_FILE_BASE}.qm
    15. updateqm.commands = $$QMAKE_LRELEASE -silent ${QMAKE_FILE_IN} -qm qm/${QMAKE_FILE_BASE}.qm
    16. updateqm.CONFIG += no_link target_predeps
    17. QMAKE_EXTRA_COMPILERS += updateqm
    18.  
    19. INSTALLS += translations
    20.  
    21. translations.path = /usr/share/app
    22. translations.files = qm/cs_CZ.qm
    To copy to clipboard, switch view to plain text mode 

    It compiles the translation and installs fine. But when I run the app translation doesn't work. Any ideas?
    Last edited by gordebak; 10th November 2012 at 13:34. Reason: updated contents

  2. #2
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: qmake and localization

    Introducing the translation files in the pro file is not enough. You have to write code in your application to change the locale.

    You can do this:
    In the MainWindow's constructor, before calling setupUi() function (or in the main function, before creating MainWindow object) write these lines:
    Qt Code:
    1. QTranslator translator;
    2. translator.load("path/to/qm/file/cs_CZ.qm");
    3. qApp->installTranslator(&translator); //Or in the case of main function: a.installTranslator(&translator);
    To copy to clipboard, switch view to plain text mode 

    You can get more information from the documentation of QTranslator class:
    http://doc.qt.digia.com/latest/qtranslator.html

  3. The following user says thank you to mousa_mk for this useful post:

    gordebak (11th November 2012)

  4. #3
    Join Date
    May 2010
    Posts
    3
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qmake and localization

    Thank you very much. It works.

    One last question, because I couldn't figure out from the documentation: Should I create a QTranslator for every translation, or can I load more than one translation into one?

    Sorry if it's obvious, I couldn't figure out.

    Edit: Sorry, I figured out. One for each. Thanks.

  5. #4
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: qmake and localization

    In that case you can move the definition of QTranslator object into your main window class (or everywhere else, just somewhere that'll keep it) and load it like before:

    at mainwindow.h file:
    Qt Code:
    1. private:
    2. QTranslator translator;
    To copy to clipboard, switch view to plain text mode 

    In MainWindow's constructor before setupUi():
    Qt Code:
    1. translator.load("path/to/qm/file/cs_CZ.qm");
    2. qApp->installTranslator(&translator);
    To copy to clipboard, switch view to plain text mode 

    When you want to change language at runtime you just load the new language and retranslate the ui:
    Qt Code:
    1. translator.load("path/to/qm/file/en_US.qm");
    2. ui->retranslateUi(this);
    To copy to clipboard, switch view to plain text mode 

    This way, you install the translator once and just change it's loaded language whenever needed.

  6. The following user says thank you to mousa_mk for this useful post:

    gordebak (11th November 2012)

  7. #5
    Join Date
    May 2010
    Posts
    3
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qmake and localization

    I just wanted to load the appropriate translation according to the system locale in the first startup. I did it reading the documentation.

    Thank you very much.

Similar Threads

  1. Localization - shortDayName - Raspberry Pi
    By scls19fr in forum Qt Programming
    Replies: 0
    Last Post: 25th October 2012, 18:18
  2. Localization of library
    By realdarkman71 in forum Newbie
    Replies: 0
    Last Post: 27th May 2012, 20:09
  3. Replies: 2
    Last Post: 23rd May 2011, 10:21
  4. Localization for dynamically generated text.
    By vaibhav in forum Qt Programming
    Replies: 10
    Last Post: 14th March 2011, 12:03
  5. Real Time Localization System
    By oswalidos in forum General Programming
    Replies: 3
    Last Post: 22nd September 2009, 20:50

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.