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. qsTr vs QT_TR_NOOP with dynamic translations

qsTr vs QT_TR_NOOP with dynamic translations

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.5k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    Marek
    wrote on last edited by
    #1

    Hi all,

    I need to have translations in my QML app. A few years ago I was doing C++ apps with translations so I'm familiar with lupdate, lrelease and installTranslator in main.cpp
    I also remember that I needed to restart app after changing language to have everything translated.
    The question is: If I have some models in C++ with texts in it and whole user interface in QML will app translate everything when I change language and execute installTranslator ?
    I mean what will be the difference in C++ files and QML ?
    What is difference between qsTr and QT_TR_NOOP in terms of dynamic translations. I have read that QT_TR_NOOP only marks the text for the lupdate to see this. In old C++ apps I was using QT_TR_NOOP when I could not use trUtf8 (like in Singleton files, for variable initialization)

    Best.
    Marek

    J.HilkJ 1 Reply Last reply
    0
    • M Marek

      Hi all,

      I need to have translations in my QML app. A few years ago I was doing C++ apps with translations so I'm familiar with lupdate, lrelease and installTranslator in main.cpp
      I also remember that I needed to restart app after changing language to have everything translated.
      The question is: If I have some models in C++ with texts in it and whole user interface in QML will app translate everything when I change language and execute installTranslator ?
      I mean what will be the difference in C++ files and QML ?
      What is difference between qsTr and QT_TR_NOOP in terms of dynamic translations. I have read that QT_TR_NOOP only marks the text for the lupdate to see this. In old C++ apps I was using QT_TR_NOOP when I could not use trUtf8 (like in Singleton files, for variable initialization)

      Best.
      Marek

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Marek said in qsTr vs QT_TR_NOOP with dynamic translations:

      Hi all,

      hi

      I need to have translations in my QML app. A few years ago I was doing C++ apps with translations so I'm familiar with lupdate, lrelease and installTranslator in main.cpp
      I also remember that I needed to restart app after changing language to have everything translated.

      I'm unsure if that was a requirement back than(how many years?), but as far as I remember back, runtimes language changes have been possible

      The question is: If I have some models in C++ with texts in it and whole user interface in QML will app translate everything when I change language and execute installTranslator ?

      Installing a QTranslator to you application will trigger a changeEvent (QEvent::LanguageChange) on QML-side this will also trigger a reevaluation of all strings marked with qsTr - at least since 5.9 - and you'll have to do nothing on that side.

      On c++ side, if you're using a ui-file you'll have to call ui->retranslateUi(this); if you assigned text by hand, e.g myLabel->setText(tr("Hello World")); you'll have to call that function again because retranslateUi will not effect this code, even if myLabel should live in side a ui file.

      If you feed your QML files QStrings from a c++ backend you'll have to trigger a reload as well.

      What is difference between qsTr and QT_TR_NOOP in terms of dynamic translations. I have read that QT_TR_NOOP only marks the text for the lupdate to see this. In old C++ apps I was using QT_TR_NOOP when I could not use trUtf8 (like in Singleton files, for variable initialization)

      QT_TR_NOOP is only needed if you have a QString that is not created inside a function, for example as a member of your class, and you still want to translate it.

      Greetings


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      M 1 Reply Last reply
      2
      • J.HilkJ J.Hilk

        @Marek said in qsTr vs QT_TR_NOOP with dynamic translations:

        Hi all,

        hi

        I need to have translations in my QML app. A few years ago I was doing C++ apps with translations so I'm familiar with lupdate, lrelease and installTranslator in main.cpp
        I also remember that I needed to restart app after changing language to have everything translated.

        I'm unsure if that was a requirement back than(how many years?), but as far as I remember back, runtimes language changes have been possible

        The question is: If I have some models in C++ with texts in it and whole user interface in QML will app translate everything when I change language and execute installTranslator ?

        Installing a QTranslator to you application will trigger a changeEvent (QEvent::LanguageChange) on QML-side this will also trigger a reevaluation of all strings marked with qsTr - at least since 5.9 - and you'll have to do nothing on that side.

        On c++ side, if you're using a ui-file you'll have to call ui->retranslateUi(this); if you assigned text by hand, e.g myLabel->setText(tr("Hello World")); you'll have to call that function again because retranslateUi will not effect this code, even if myLabel should live in side a ui file.

        If you feed your QML files QStrings from a c++ backend you'll have to trigger a reload as well.

        What is difference between qsTr and QT_TR_NOOP in terms of dynamic translations. I have read that QT_TR_NOOP only marks the text for the lupdate to see this. In old C++ apps I was using QT_TR_NOOP when I could not use trUtf8 (like in Singleton files, for variable initialization)

        QT_TR_NOOP is only needed if you have a QString that is not created inside a function, for example as a member of your class, and you still want to translate it.

        Greetings

        M Offline
        M Offline
        Marek
        wrote on last edited by
        #3

        @J.Hilk
        Thanks for clarifications.
        I started coding with Qt 4.6 but my experience with translations was probably with some early versions of Qt 5. Now when You mentioned it I remember ui->retranslateUi(this) if I wanted for changes to be visible.
        Now I don't have any QWidget and ui files, all UI are in QML, however I have models in C++ where I have some variables, these I assume I will have to assign (or reload) eg

        myStructPointer->variable=tr("some text)
        

        QML should take car of itself providing that I have qsTr() for every string.

        Best,
        Marek

        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