webaloman
webaloman

Reputation: 221

QML translation

I try to use translation in QML. I opened a new project QtQuick project, I chose QtQuick Componenets for Symbian as a QtQuick Application Type. Qt Creator created a application source tree with all standard files (main.cpp, main.qml, mainpage.qml...)

MainPage.qml is very simple:

import QtQuick 1.1
import com.nokia.symbian 1.1

Page {
    id: mainPage
    Text {
        anchors.centerIn: parent
        text: qsTr('Hello world!')
        color: platformStyle.colorNormalLight
        font.pixelSize: 20
    }
}

My main.cpp file looks after implementation of QTranslator like this:

#include "qmlapplicationviewer.h"
#include <QTranslator>
#include <QPushButton>
#include <QDebug>

Q_DECL_EXPORT int main(int argc, char *argv[])
{
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    QTranslator* translator = new QTranslator;

    qDebug()<<"Translating: "<<translator->load(QString("qml/International/inter_en"));

    app->installTranslator(translator);

    //QPushButton hello(QPushButton::tr("Hello world!"));
    //   hello.resize(100, 30);

    //   hello.show();

    QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());

    viewer->setMainQmlFile(QLatin1String("qml/International/main.qml"));
    viewer->showExpanded();

    return app->exec();
}

Then I run lupdate mainpage.qml -ts inter_en.ts , I have used linguist to translate the POSIX expression "Hello world!" to something else just test that it is translating. Then I created inter_en.qm file with linguist.

But when I run the application on simulator I dont get "Hello world!" translated, although the translator is loaded successfully (I get "Translating: true" in qDebug).

Translator is working correctly I am sure, because when I deremark part of the code with QPushButton , (again I repeat the lupdate and linguist things for this purpose) , then the "Hello world!" expression in QPushButton is translated correctly.

It is only the QmlApplicationViewer and QML file that is not performing translation correctly. Any quesses?????

Thanks

UPDATE

I found out the following: MainPage.qml is imported as reusable component into main.qml. If I use qsTr() in main.qml then the text is translated correctly in main.qml. However text in MainPage.qml is not tranlated correcty. I guess due to importing it as reusable component. Any comments? Experiences?

UPDATE2 - SOLUTION

Translation files need to be created case sensitively:

lupdate mainpage.qml -ts myapp_sk.ts is wrong

lupdate MainPage.qml -ts myapp_sk.ts is correct

Upvotes: 6

Views: 3693

Answers (2)

Kombinator
Kombinator

Reputation: 105

When the issue is not the translation per se, but changing language during runtime this may help you. If you load a new QTranslator with app->installTranslator(translator); it (QApplication) will fire a change event. In your Qt class you have to catch it with

 /*!
    on the fly translation 
 */
 void MyQmlView::changeEvent(QEvent *event)
 {
    if (event->type() == QEvent::LanguageChange) 
    {
        // triggers qml function/slots 
        emit  retranslate();
     }
     else 
     {
         QWidget::changeEvent(event);
      }
  } 

Somewher after loading your "main.qml":

    m_pQmlView->rootContext()->setContextProperty( "_view", this );

QML side:

Component.onCompleted: {

  /**********************  Connections  ***************************/

// connect Qt signal MyView::retranslate() with QML function (slot) retranslate        
 _view.retranslate.connect(retranslate)
}

// slot! 
function retranslate () {
     lblHelloWord.text  = qsTr("Hello Word")
}

This works very good for MS Windows Platform.

Upvotes: 1

Pěna
Pěna

Reputation: 61

I also use QML files as reusable components and I have no problem with translations at all. So I guess the following can help you as well.

I also guess you don't want to call lupdate manually for every file. So you should use the following lines in .pro file to make it look for all translatable phrases in QML and JS files automatically (correct your paths)...

lupdate_hack{
    SOURCES += qml/*.qml \
        qml/*.js
}
TRANSLATIONS = \
    langs/WakeOnLAN_cs.ts \
    langs/WakeOnLAN_pl.ts \
    langs/WakeOnLAN_es.ts \
    langs/WakeOnLAN_fr.ts \
    langs/WakeOnLAN_it.ts \
    langs/WakeOnLAN_hu.ts \
    langs/WakeOnLAN_fa.ts \
    langs/WakeOnLAN_de.ts \
    langs/WakeOnLAN_pt.ts
CODECFORTR = UTF-8

It doesn't come from my head, so here is the source (there is also a note about dynamic translation): https://forum.qt.io/topic/30076/is-there-a-way-to-use-linguist-in-global-context

Upvotes: 0

Related Questions