Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML qsTr() translation doesn't translate if text is passed in from other files
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Trian
    wrote on last edited by
    #1

    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;
     }
    

    }

    1 Reply Last reply
    0
    • Arvindhan PonnusamyA Offline
      Arvindhan PonnusamyA Offline
      Arvindhan Ponnusamy
      wrote on last edited by Arvindhan Ponnusamy
      #2

      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.

      T 1 Reply Last reply
      1
      • Arvindhan PonnusamyA Arvindhan Ponnusamy

        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.

        T Offline
        T Offline
        Trian
        wrote on last edited by Trian
        #3

        @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.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Raureth
          wrote on last edited by Raureth
          #4

          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")
          
          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved