[Interest] qsTr in components

Kristoffersen, Even (NO14) Even.Kristoffersen at Honeywell.com
Mon May 23 14:31:09 CEST 2016


Hello all,

I've played around a bit with an approach inspired by http://wiki.qt.io/How_to_do_dynamic_translation_in_QML .

I've made a component for easier use:
TextTr.qml
------------------
Import QtQuick 2.0

Text {
    property string txt: ""
    text: qsTr(txt) + translator.refresh
}
------------------

Used like this:

TextTr {
    id: testString
    txt: QT_TR_NOOP("Testing")
}

However, I'm unable to get the TextTr element to actually display the translated string.

TextTr {
    id: testString
    txt: QT_TR_NOOP("Testing")

    Component.onCompleted:
    {
        console.log(qsTr(txt), text);
        translator.forceRefresh(); //translator will emit refreshChanged()
        console.log(qsTr(txt), text);
        txt = "Test";
        console.log(qsTr(txt), text);
        txt = "Testing";
        console.log(qsTr(txt), text);
    }
}

Loading a Norwegian translation yields the following output:
Tester Testing
Tester Testing
Test Testing
Tester Testing

That the value of text still is "Testing" when I set txt to "Test" seems to point towards a broken binding.


If I instead do it directly like this:
TextTr {
    id: testString
    text: qsTr("Testing") + translator.refresh
}
Then it works as intended, but then there's no point to the component and I have to have the "+ translator.refresh" part all over anyway.



Another weird result I've had while testing is with this: 

TextTr.qml
-----------------
import QtQuick 2.0
Text {
    property string txt: ""
    text: qsTr(txt) + translator.autoTranslate

    Connections {
        target: translator

        onAutoTranslateChanged: {
            console.log("Translation changed:", txt, "-->", qsTr(txt));
        }
    }
}

And on the screen where I change the translation:
Connections {
    target: translator
    onRefreshChanged: {
        console.log("Testing -->", qsTr("Testing"));
   }
}

Then the log in the TextTr element prints "Testing --> Testing" but the log in the screen prints "Testing --> Tester"


Anyone have any idea why qsTr() seems to break down when used in a component like this?
It seems like a caching or binding issue...


-Even



More information about the Interest mailing list