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. Qt linguist not get text from c++ code.
QtWS25 Last Chance

Qt linguist not get text from c++ code.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
9 Posts 3 Posters 422 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
    S_max07
    wrote on last edited by
    #1

    Hi all,
    How I can get text in Qt linguist from c++ model field in qml, can I do something like qsTr(model.name);?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Why not just translate that in the model code ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        S_max07
        wrote on last edited by
        #3

        @SGaist said in Qt linguist not get text from c++ code.:

        Hi and welcome to devnet,
        Why not just translate that in the model code ?

        I need to add this variable in c++ model, and translate with Qt linguist with *.ts files, how I can add to .ts file my variable from C++ model? It's should be something like this:
        switch (role) {
        case TextRole:
        return tr(m_data.at(index.row()));

        after this we can lupdate all *.ts files, and it added new field from this function?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          How are you filling m_data ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            S_max07
            wrote on last edited by
            #5

            Actually, I have some .xml file with some name and phrase which I should to translate I getting this string phrase to some struct and use it in class with qmlRegisterType() going to qml, and have some getter with Q_PROPERTY(QString name READ name CONSTANT) and call it in qml text: name_of_register_type.name
            What I should to add to get this name in my generic .ts file for Qt Linguist?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              That's not the role of Linguist.

              Is that XML file something you change often ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • S Offline
                S Offline
                S_max07
                wrote on last edited by
                #7

                I don't change often this file it's something like config file with names and phrase for application.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  In that case you should either translate this XML file directly and load the appropriate one when switching your application language or generate a C++ file from it and then use Qt Linguist on that one.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • SeeLookS Offline
                    SeeLookS Offline
                    SeeLook
                    wrote on last edited by SeeLook
                    #9

                    Hi!

                    @S_max07 You might wrap a text in XML file with syntax recognized by lupdate, i.e.:

                    •<?xml version="1.0">
                    <someKey>translate("someContext",  "text to translate")</someKey>
                    

                    when that key is handled by C++, trim it and then translate:

                    QXmlStreamReader xml;
                    if (xml.name() == QLatin1String("someKey"))
                          auto key = QApplication::translate("someContext",  xml.readElementText()
                                                        .replace(QLatin1String("translate(\"someContext\", \""), QString())
                                                        .replace(QLatin1String("\")"), QString()).toLocal8Bit());
                    

                    and invoke lupdate which also looks up for *.xml extensions:

                    lupdate -extensions cpp,h,qml,xml -recursive src_dir xml_dir -ts some_file.ts
                    
                    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