c++qtvisual-studio-2012translationqt-linguist

vc++ + QT translation of UI is not working


I have a project with QT in vc++ and I need to locate the string in the UI to different languages. I created a UI through the QTdesigner in the visual studio add in of visual studio 2012 and also I have installed the QT plugin to use the Qt features as well.

I have created a .pro file and added:

SOURCES      += main.cpp
TRANSLATIONS += languagefileqt_es.ts

After I generate a linguist file SOURCES emminensmultiportqt_es.ts and it detected correctly all the strings in my IU. After that, I generate the .qm file using the release function of Qtlinguist.

My resources file is:

<RCC>
    <qresource prefix="MyAppQT">
        <file>languagefileqt_es.qm</file>
    </qresource>
</RCC>

Then I have added this to my main.cpp:

QTranslator translator;
bool loaded = translator.load("languagefileqt_es");
qDebug() << "loaded " << loaded;
a.installTranslator(&translator);

And loaded returns true in all the cases. My problem is that the UI is not translated when the application is executed. It is weird because it has no effect.

Any clue about what I am missing or what could I check out?

Thanks a lot


Solution

  • I discovered what was the problem. thanks @Jens for try to help.

    I think I commit a mistake of not knowing how the translating mechanism was working. In my main.cpp I had:

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindowQT w;
    
    QTranslator translator;
    bool loaded = translator.load("languagefileqt_es");
    qDebug() << "loaded " << loaded;
    a.installTranslator(&translator);
    
    w.show();
    return a.exec(); 
    } 
    

    But I realized that if I execute

    qDebug() <<  QApplication::translate("MainWindowQTClass", "...BOARDING", 0);
    

    after the loading process it will return the string translated correctly. So I change the definition of my UI after the internationalization and it worked. Apparently, translation is done in a function called retranslateUi() which is called in the constructor.

    The correct main.cpp should be:

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    
    QTranslator translator;
    bool loaded = translator.load("languagefileqt_es");
    qDebug() << "loaded " << loaded;
    a.installTranslator(&translator);
    
    MainWindowQT w;
    w.show();
    return a.exec(); 
    }