Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 580
webaloman Avatar asked Nov 13 '11 22:11

webaloman


1 Answers

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.

like image 128
Kombinator Avatar answered Nov 17 '22 10:11

Kombinator