Home » Android » QML ignores qsTr() and doesn't localizes strings

QML ignores qsTr() and doesn't localizes strings

Posted by: admin November 1, 2017 Leave a comment

Questions:

I defined text as folows

Text {
    id: lessonsTxt
    //: Lesson label text
    text: qsTr("Lessons")
}

Created *.ts files with lupdate

Edited *.ts files

Released *.ts files with lrelease

Build, ran and that’s all.
I’ve tested (changed Language of device) results on Android device and Text was remaining the same.

Did I miss something?


Files tree:

.
├── GUI
│   ├── Views
│   │   ├── MainView.qml
│   │   ├── MainViewForm.ui.qml
│   │   └── StackView.qml
│   └── main.qml
├── Languages
│   ├── MyApp_en.qm
│   ├── MyApp_en.ts
│   ├── MyApp_ru.qm
│   └── MyApp_ru.ts
├── MyAppQt.pro
├── MyAppQt.pro.user
├── main.cpp
└── qml.qrc

MyAppQt.pro:

QT += quick
CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += main.cpp

RESOURCES += qml.qrc

TRANSLATIONS += Languages/MyApp_en.ts
TRANSLATIONS += Languages/MyApp_ru.ts

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

qml.qrc:

<RCC>
    <qresource prefix="/">
        <file>GUI/main.qml</file>
        <file>GUI/Views/MainView.qml</file>
        <file>GUI/Views/MainViewForm.ui.qml</file>
        <file>GUI/Views/StackView.qml</file>
        <file>Languages/MyApp_en.qm</file>
        <file>Languages/MyApp_en.ts</file>
        <file>Languages/MyApp_ru.qm</file>
        <file>Languages/MyApp_ru.ts</file>
    </qresource>
</RCC>

Languages/MyApp_en.ts:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="en_US">
<context>
    <name>MainViewForm.ui</name>
    <message>
        <location filename="../GUI/Views/MainViewForm.ui.qml" line="49"/>
        <source>Lessons</source>
        <extracomment>Lesson label text</extracomment>
        <translation>Lessons</translation>
    </message>
</context>
</TS>

Languages/MyApp_ru.ts:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="ru_RU">
<context>
    <name>MainViewForm.ui</name>
    <message>
        <location filename="../GUI/Views/MainViewForm.ui.qml" line="49"/>
        <source>Lessons</source>
        <extracomment>Lesson label text</extracomment>
        <translation>Уроки</translation>
    </message>
</context>
</TS>

GUI/Views/MainViewForm.ui.qml

import QtQuick 2.6
import QtQuick.Controls 1.2

Item {
    id: mainView

    property alias mainView: mainView

    Text {
        id: lessonsTxt
        //: Lesson label text
        text: qsTr("Lessons")
    }
}
Answers: