Qt Localizing Windows



  • My project folder is currently setup like this:
    @
    -root
    ---[source files]
    ---Translations
    ------app_en.ts
    ------app_en.qm
    ------app_fr.ts
    ------app_fr.qm
    ------app_jp.ts
    ------app_jp.qm
    @

    My .pro file:
    @
    #-------------------------------------------------

    Project created by QtCreator 2014-03-18T15:15:40

    #-------------------------------------------------

    QT += core gui

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = TestApp
    TEMPLATE = app

    SOURCES += main.cpp
    mainwindow.cpp
    TRANSLATIONS=Translations/app_en.ts
    Translations/app_fr.ts
    Translations/app_jp.ts
    CODECFORSRC = UTF-8

    HEADERS += mainwindow.h

    FORMS += mainwindow.ui
    @

    In my main.cpp I try to force the translation to JP:
    @
    #include "mainwindow.h"
    #include <QApplication>
    #include <QTranslator>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QTranslator translator;
    translator.load("app_jp");
    a.installTranslator(&translator);
    MainWindow w;
    w.show();

    return a.exec(&#41;;
    

    }
    @

    But the main window still appears in English. Maybe I'm misunderstanding the documentation, but how do we force a certain local for the application?


  • Lifetime Qt Champion

    Hi,

    Are you sure your translation are loaded properly ? If I see things correctly, all your translation files are within your project sources. You application is run from the build directory so it can't find them. So you can either copy the files besides your executable, give the path where to find them to QTranslator or use the resource system.

    Hope it helps



  • Look at this project
    http://kde-apps.org/content/show.php/BackupTo?content=149622
    and download it. The it - en translations work very well. You can also embed in the exe the localizations file.



  • I've created a resource file entry for each one of my .qm files then added the resource to the projet. At the start up I load the resource and the path provided is:
    @translator.load(":/Translations/app_jp.qm");@
    and it works well I guess I needed to put in a full path to .qm file if I wasn't using resource file, but this way seems much simpler


  • Lifetime Qt Champion

    Indeed, that's a catch many people fall in currently, the source tree is not the build tree so using relative path will never have the expected effect


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.