Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML qsTr() translation doesn't translate if text is passed in from other files



  • I have a utility qml file which contains a translate function as follow: one function work, but another function does not. It appears that it only works if literal string is defined in the same file. Any way to get around this?

    Item {
    //does not work
    function doTranslation1( string) {
    var translateStr = string
    translateStr = qsTr(string)
    return translateStr;
    }

     //work
     function doTranslation2( string) {
         var translateStr = string
         if(string == "hello")
              translateStr = qsTr("hello")
         return translateStr;
     }
    

    }



  • Try the below format in case using attributes for translation.
    Item {
    property string greeting: QT_TR_NOOP("hello")

    Text { text: qsTr(greeting) }
    

    }
    Use can use :
    qsTR(QT_TR_NOOP(stringdata))

    This should help in resolving the issue.



  • @Arvindhan-Ponnusamy said in QML qsTr() translation doesn't translate if text is passed in from other files:

    qsTR(QT_TR_NOOP(stringdata)) does not work.
    It is odd that literal text has to be defined in the same file at compile time.
    I tried to create a variable that contains a deep copy of the string parameter, but it still did not work.



  • Maybe you can use qsTranslate() instead of qsTr() to define the context ?
    At least that was useful for me since the value to translate was passed by another QML file

    translateStr = qsTr("TrContext", "hello")
    

Log in to reply