QML Translation

I am trying to use translation in QML. I opened a new project of QtQuick project, I chose QtQuick Componenets for Symbian as QtQuick Application Type. Qt Creator created an application source tree with all the 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, after implementing QTranslator, looks 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 used a linguist to translate the POSIX expression โ€œHello world!โ€. for something else, just check that it is a translation. Then I created an inter_en.qm file with a linguist.

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

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

Only the QmlApplicationViewer and QML file do not translate correctly. Any quests ?????

thanks

UPDATE

I found out the following: MainPage.qml is imported as a reusable component in main.qml. If I use qsTr () in main.qml, the text will be correctly translated into main.qml. However, the text in MainPage.qml does not translate correctly. I think due to importing it as a reusable component. Any comments? Experience?

UPDATE2 - SOLUTION

Translation files should be created with the feeling:

 lupdate mainpage.qml -ts myapp_sk.ts is wrong lupdate MainPage.qml -ts myapp_sk.ts is correct 
+4
source share
2 answers

I also use QML files as reusable components, and I have no problem translating at all. So I think the following may help you.

I also assume that you do not want to call lupdate manually for each file. Thus, you should use the following lines in the .pro file to automatically search for all translatable phrases in QML and JS files (fix 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

0
source

When a problem is not a translation as such, but changes the language at runtime, this can help you. If you download a new QTranslator using the application โ†’ installTranslator (translator); it (QApplication) will fire a change event. In your Qt class, you should 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); } } 

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") } 

It is very suitable for MS Windows platform.

0
source

Source: https://habr.com/ru/post/1381041/