Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Suggestions for using tr() with variables
Forum Updated to NodeBB v4.3 + New Features

Suggestions for using tr() with variables

Scheduled Pinned Locked Moved General and Desktop
14 Posts 6 Posters 18.8k 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
    mkoskim
    wrote on last edited by
    #3

    [quote author="Eddy" date="1319199226"]
    Basically you use a qstring with %1 %2 .... placeholders which you can fill in with parameters using the arg member.
    [/quote]

    Hmmh, I don't get it. I thought that args are used to add variable content to translations (e.g. QString(tr("Count: %1")).arg(count)), but how can I add translations to dynamic content?

    I was probably not describing my aim very clearly. I try it again. I load an XML document, and I would like to translate attributes and their content:

    [code]
    // Load XML document
    QDomDocument doc;
    doc.setContent(QFile("file.xml"));

    ...
    // Get some node from document
    QDomElement node;

    // Translate node attribute names (and values) to GUI
    QDomNamedNodeMap attrs = node.attributes();

    for(int i = 0; i < attr.size(); i++)
    {
    cout << QObject::tr(attr.nodeName())
    << " = "
    << QObject::tr(attr.nodeValue());
    }
    [/code]

    For example, one element is "TitleItem", containing the title of the story, and type of it:

    [code]
    <TitleItem type="shortstory" ...> ... </TitleItem>
    [/code]

    I would like to add translations to those with Qt Linguist, so one way is to add "fake" translations to source, like below?

    [code]
    ...
    QObject::tr("type"); // Translation for type attribute name
    QObject::tr("shortstory"); // Translation for one type attribute value
    QObject::tr("longstory"); // Translation for another type attribute value
    ...
    [/code]

    Is there any better way to do this?

    http://mkoskim.wordpress.com
    http://mkoskim.drivehq.com
    http://mkoskim.deviantart.com

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JulienMaille
      wrote on last edited by
      #4

      You want to be able to dynamically translate a word taken from this xml file. So this could be any word, right? Then how do you want lupdate to populate the language file correctly? lupdate cannot guess the words that will come out of your xml.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #5

        In general, that is possible.
        The only thing is, you have to manage the ts files by hand :-(
        So if you create the ts files by hand, you can call tr with variables :-)

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mkoskim
          wrote on last edited by
          #6

          [quote author="neFAST" date="1319202492"]You want to be able to dynamically translate a word taken from this xml file. So this could be any word, right?[/quote]

          Yes and yes.

          [quote author="neFAST" date="1319202492"]Then how do you want lupdate to populate the language file correctly? lupdate cannot guess the words that will come out of your xml.[/quote]

          No, but I can - so that's why I thought that I could write those "fake" translations (unused translations, with some suitable context, possibly placed in some dead code sections) to make lupdate to know the words.

          http://mkoskim.wordpress.com
          http://mkoskim.drivehq.com
          http://mkoskim.deviantart.com

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mkoskim
            wrote on last edited by
            #7

            [quote author="Gerolf" date="1319202765"]In general, that is possible.
            The only thing is, you have to manage the ts files by hand :-([/quote]

            Oh, no, that's something I would not like to do :(

            [quote author="Gerolf" date="1319202765"]So if you create the ts files by hand, you can call tr with variables :-)[/quote]

            There is no some comment fields for lupdate/linguist to tell the possibly content of variables? If not, then I try to make that fake translation method work...

            http://mkoskim.wordpress.com
            http://mkoskim.drivehq.com
            http://mkoskim.deviantart.com

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dolphin
              wrote on last edited by
              #8

              Did anyone solve this?? I have a massive amount of strings to be translated for a big menu structure and would prefer them not to be hard coded - prefer to read them from an XML file - managing the ts file by hand would be a huge task prone to error.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #9

                [quote author="Dolphin" date="1338472810"]Did anyone solve this?? I have a massive amount of strings to be translated for a big menu structure and would prefer them not to be hard coded - prefer to read them from an XML file - managing the ts file by hand would be a huge task prone to error.[/quote]
                Why don't you do the translations directly in your XML then?

                Otherwise, the only option I can see is that you write a kind of pre-compiler that parses your XML, and generates code for it that contains the strings to be translated using of the methods that can be picked up by lupdate.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Dolphin
                  wrote on last edited by
                  #10

                  Using the Menu XML is an option but it would be preferable not to give such a key file out to all and sundry (this system has to cope with lots of languages, braille, speech and hands free so a LOT of things can go wrong if the menu xml is messed up).

                  According to the documentation QT_TR_NOOP should work but I have not managed that yet.

                  Thanks for your thoughts, hope you had a good loooooooooong weekend.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #11

                    QT_TR_NOOP can only be used with static texts. Text that have a value during the pass that lupdate does over your source code. At that point the XML is not loaded of course.

                    So, if you really want this, the custom compiler to create a source file from the XML is the only viable solution.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      Dolphin
                      wrote on last edited by
                      #12

                      I have just now got this code:

                      @
                      QString text = QT_TR_NOOP("test, test!");

                      QPushButton hello(QPushButton::tr(text.toStdString().c_str()));
                      QPushButton hello2(QPushButton::tr("Hello from Nicola"));
                      

                      @

                      giving me this TS file:
                      @
                      <?xml version="1.0" encoding="utf-8"?>
                      <!DOCTYPE TS>
                      <TS version="2.0" language="en_US">
                      <context>
                      <name>QPushButton</name>
                      <message>
                      <source>Hello from Nicola</source>
                      <translation type="unfinished"></translation>
                      </message>
                      </context>
                      <context>
                      <name>TranslationObject</name>
                      <message>
                      <source>test, test!</source>
                      <translation type="unfinished"></translation>
                      </message>
                      </context>
                      </TS>
                      @
                      Which is exactly what I need - woop! My code was not in a QObject, doh! ( Little things :-S )
                      Thank you :-)

                      Edit: Please use @ tags to surround code sections; Andre

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #13

                        If your code is not a QObject, you can use QObject::tr. It is a static method after all...

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          Dolphin
                          wrote on last edited by
                          #14

                          Well, that is what I thought but my code did not work until I put it in a QObject (a frustrated experiment) but I have just moved it back out of the QObject (deleted the TS) and it works. Must have been something funky about the update of the TS). Either way, it works, yay!

                          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