1

This is my QML code:

Text {
     id: helloText
     //: Hello World text
     //% "Hello World"
     //~ Context No specific context
     text: qsTrId("hello-world-id");
     anchors.top: parent.top
}

Excerpt from the Qt documentation here:

The "Engineering English" text that you see in the user interface for development builds is indicated with a //% comment. If you do not include this, the text ID will be shown in the user interface.

When I run the application, I'm seeing hello-world-id rather than Hello World, as described in the documentation. Am I missing something here?

Thanks in advance.

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
ramtheconqueror
  • 1,907
  • 1
  • 22
  • 35

2 Answers2

2

First of all you have to create .ts file with translated strings. For example, if I want to create translation for Russian locale, I do:

lupdate main.qml -ts your_project_ru_RU.ts

If you have several QML files you can specify it in .pro file:

lupdate_only {
    SOURCES = main.qml \
              foo.qml
}

and so write:

lupdate your_project.pro -ts your_project_ru_RU.ts

this command will create untranslated file for Russian locale that you have to open with QT Linguist to translate.

You can specify .ts files in .pro file:

TRANSLATIONS += your_project_ru_RU.ts

After all you have to compile the file (File/Compile in Qt Linguist). That will create your_project_ru_RU.qm.

Finally, you must load .qm file, for example in main.cpp:

QTranslator translator;    
translator.load("your_project_" + QLocale::system().name());
app.installTranslator(&translator);

UPDATED:

If you need some semblance of Qt i18n engine, without providing qm files:

foo.qml

import QtQuick 2.4
import QtQuick.Window 2.2
import "i18n.js" as I18n

Window {
    Text {
        text: I18n.myTr("text-id");
        anchors.centerIn: parent
    }
}

i18n.js

var texts = {
    "text-id": "Hello, World!",
    "another-text-id": "Goodbye, World!"
}

function myTr(textid) {
    return texts[textid] ? texts[textid] : "";
}
folibis
  • 12,048
  • 6
  • 54
  • 97
  • Thanks for the answer. This is for implementing a full localisation. My question is `How to see the Engineering Text rather than string id`? For ex: if you use `button->setText(tr("Hello World"));` it will display `Hello World` on button. But using `button->setText(qtrId("hello-world-id"));` is showing `hello-world-id` rather than `Hello World` which is the Engineering English (added with `//% "Hello World"`. Thanks. – ramtheconqueror Jul 06 '15 at 14:47
  • Ok, I see you misunderstand it a bit. From the Qt help: `Localizing with text IDs follows much the same process as for plain text`. All comments you provided above are for simplification in Qt Lunguist. `//%` is text pattern but not the text itself. You still must create `ts` file as I described above, translate it and include to your app. The sense of the comment is that you cannot provide pattern in `qtrId()` as you can do it , for example in `qsTr("Hello, %1")`. You pass an `id` only. – folibis Jul 06 '15 at 23:06
0
  1. created a .ts file via lupdate:

    lupdate main.qml -ts your_project_ru_RU.ts

  2. open .ts file in Qt linguist and add your translations
  3. call -idbased option in the lrelease tool:

    lrelease -idbased your_project_ru_RU.ts

Visit http://doc.qt.io/qt-5/qml-qtqml-qt.html#qsTrId-method

Medici Lorenzo
  • 189
  • 2
  • 10