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. [Solved] How to translate text in a listmodel? (qsTr, QT_TR_NOOP)
Qt 6.11 is out! See what's new in the release blog

[Solved] How to translate text in a listmodel? (qsTr, QT_TR_NOOP)

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 9.7k 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.
  • F Offline
    F Offline
    FranzB
    wrote on last edited by
    #1

    Hallo again,

    When I tried to create translations for my QML UI, an error occurred when I started it: "ListElement: Cannot use script as property value". I have already searched the web for the problem and it turns out that qsTr() cannot be used within a ListModel. As I really need those strings translated, I searched for other options and some bug report answers suggest that QT_TR_NOOP() will do the job. When I replace all the qsTr() with QT_TR_NOOP(), the application is able to start again. However, nothing is translated. Why?

    The translation works well for the C++ part of the application. What do I have to do to get it working for the QML part?

    I have created the language files via the usual queue lupdate qml.qml -ts qml.ts -> Linguist -> lrelease qml.ts. The translators are loaded right after app start:
    @
    QTranslator translatorApp;
    translatorApp.load("language/de");
    app.installTranslator(&translatorApp);
    QTranslator translatorUI;
    translatorUI.load("language/ui_de");
    app.installTranslator(&translatorUI);
    @
    The lines should load both app and UI translations into German. The "app" one works well, the other one doesn't. The files exist, load() returns true.

    I assume there is an easy way to do it (because it seems like a very standard task for an internationalization-capable framework), but I have not been able to find it. Can anyone point me to a viable solution please?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      task_struct
      wrote on last edited by
      #2

      Hi.

      Have a look at "this tutorial":http://developer.qt.nokia.com/wiki/Qt_Quick_Carousel#70b4903abcb62ace84264ad0443ae759

      "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

      • Linu...
      1 Reply Last reply
      0
      • F Offline
        F Offline
        FranzB
        wrote on last edited by
        #3

        Sweet! Works like a charm. I just made one javascript function for each of my list models and put model.append() there for every element I want to create. Then at application start I make a call via invokeMethod to the javascript function and - voila - everything is translated without bugging me for the qsTr() calls.

        I would have never thought of searching for "carousel" nor did that article pop up when I searched, so a very big thanks for that pointer!

        1 Reply Last reply
        0
        • T Offline
          T Offline
          task_struct
          wrote on last edited by
          #4

          You can search by tags. They describe pages better ;)

          "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

          • Linu...
          1 Reply Last reply
          0
          • N Offline
            N Offline
            navrocky
            wrote on last edited by
            #5

            You can use plain list of JS object instead of ListModel.

            Example:
            @
            id: root

            // define plain JS object list
            property var model: [
            { title: qsTr("Airplane"), descr: qsTr("Some descr 1") },
            { title: qsTr("Car"), descr: qsTr("Some descr 2") },
            { title: qsTr("Credit Card"), descr: qsTr("Some descr 3") }
            ]

            ListView {
            model: root.model
            delegate: Rectangle {
            height: 20

                // cross operability with ListModel and plain JS object list
                property var item: model.modelData ? model.modelData : model
                
                Text { text: item.title + " " + item.descr }
            }
            

            }
            @

            1 Reply Last reply
            1

            • Login

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