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. Translation is not working through JavaScript function
Qt 6.11 is out! See what's new in the release blog

Translation is not working through JavaScript function

Scheduled Pinned Locked Moved Solved QML and Qt Quick
11 Posts 3 Posters 2.8k 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.
  • S Offline
    S Offline
    Sanjeev Gudisagar
    wrote on last edited by
    #1

    I am doing language translation in qml, i have three text objects, if i directly assign the values and do the translations it works fine, but if i change the values of a text fields through JavaScript function then apply the translations it is not working.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #2

      It could be issue with property binding. Binding has to re-evaluate for the translation to work. Do you have sample program for us to check ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      https://www.pthinks.com

      1 Reply Last reply
      0
      • ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        Hi,
        You should show your function and how you are doing it in qml so someone can help..
        You can use QtLinguist for translations.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sanjeev Gudisagar
          wrote on last edited by Sanjeev Gudisagar
          #4

          @dheerendra This is my sample code, Once the button is clicked i am calling JavaScript function, i am changing all the text values after that i am translating, the translation is not working

          Column {
                  spacing: 10
                  anchors.centerIn: parent
                  Text {
                      id : t1
                      text :cm.buttonText + trans.lang
                  }
                  Text {
                      id : t2
                      text :cm.buttonT1 + trans.lang
                  }
                  Text {
                      id: t3
                      text :cm.buttonT2 + trans.lang
                  }
                  Button{
                      text : "German"
                      onClicked: {
                          MyScript.changeValue();
                          trans.selectLanguage("de");
                      }
                  }
              }
          
          1 Reply Last reply
          1
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
            wrote on last edited by
            #5

            trans - Is it object coming from C++ side ? If yes, is this method is really called ?Can you confirm ? Can you show me your setLanguage method ?

            Dheerendra
            @Community Service
            Certified Qt Specialist
            https://www.pthinks.com

            1 Reply Last reply
            1
            • S Offline
              S Offline
              Sanjeev Gudisagar
              wrote on last edited by
              #6

              I have the following code in the function. this function is called i verified it with debugs.

                  if(language == "de") {
                      qDebug() << Q_FUNC_INFO << " Germany Language " << endl;
                      translator->load("");
                      translator->load("://de.qm");
                      QCoreApplication::instance()->installTranslator(translator);
                      engine->retranslate();
                  }
              
              1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
                wrote on last edited by dheerendra
                #7

                This method looks correct from translation perspective. I'm assuming that appropriate translations exist in de.qm files. what changes are you doing in changeValue() method ? Can you show the function ?

                Dheerendra
                @Community Service
                Certified Qt Specialist
                https://www.pthinks.com

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sanjeev Gudisagar
                  wrote on last edited by
                  #8

                  @dheerendra said in Translation is not working through JavaScript function:

                  changeValue()
                  Whatever i am changing in changeValue() function is not getting translated.

                  function changeValue() {
                      t2.text = cm.buttonT4
                      t3.text = cm.buttonT5
                      t1.text = cm.buttonT3
                  }
                  
                  1 Reply Last reply
                  0
                  • dheerendraD Offline
                    dheerendraD Offline
                    dheerendra
                    Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
                    wrote on last edited by
                    #9

                    hmm. You are doing the property bindings in javascript methods. This will not work. Can you try the following ?

                    t2.text = Qt.binding(function() { return cm.buttonT4;})
                    

                    Dheerendra
                    @Community Service
                    Certified Qt Specialist
                    https://www.pthinks.com

                    1 Reply Last reply
                    1
                    • S Offline
                      S Offline
                      Sanjeev Gudisagar
                      wrote on last edited by
                      #10

                      After replacing this like above its working now... Thank you @dheerendra

                      1 Reply Last reply
                      0
                      • dheerendraD Offline
                        dheerendraD Offline
                        dheerendra
                        Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
                        wrote on last edited by
                        #11

                        Cool. If you want to property binding trigger, you should call Qt.binding explicitly. Since issue is resolved you can move the case to "SOLVED" state.

                        Dheerendra
                        @Community Service
                        Certified Qt Specialist
                        https://www.pthinks.com

                        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