Nominate our 2022 Qt Champions!

Translator Comments

  • Hi All,

    I am trying to put Translator Comments in the generated .ts file from the code through the Qt lupdate tool. I general the comments can be put in to the file by the following process

    However in the file i am getting the following in the ts file,

        <source>Status: Acknowledged</source>
        <extracomment>Translator Comments----------Translator Comments</extracomment>
        <translation type="unfinished"></translation>

    in the code i have made the following change
    /*: Translator Comments */
    label->setText(QApplication::translate(" " , "Status: Acknowledged"));

    So what am i missing here? Any suggestions?

  • Moderators

    @UTC_09_069 said in Translator Comments:

    label->setText(QApplication::translate(" " , "Status: Acknowledged"));

    Instead of QApplication::translate() you should use QObject::tr():

    label->setText( tr("text-to-translate" , "comment") );

  • Here is the output once i run the above code,

    <source>Status: Acknowledged</source>
    <extracomment>Translator Comments

    Translator Comments</extracomment>
    <translation type="unfinished"></translation>

    As you can see the comment that is coming is basically the disambiguation however the what we need is the extracomment and also want to avoid the context name QApplication ...

  • Moderators

    ok got you now.
    See translator comments.

    //: this should be an extra comment

    The context comes from the class which the translate method is called on. But you can also change the context with the translator comments by adding meta-data?

    //~ Context MY_NEW_CONTEXT

  • @UTC_09_069 What I think you're looking for is using the "translator comments" by means of using the metadata syntax for translations (see documentation stated in previous post).

    //~ <field-name> <field-value>

    The key phrase from documentation is "For storage in TS files, the field name together with the prefix "extra-" will form an XML element name.". So for this source code snippet:

        //~ comment This is a comment for the translator
        Qstring txt = QString(tr("same label to translate"));

    you'll get the following in the .ts file:

           <location filename="mainwindow.cpp" line="9"/>
           <source>same label to translate</source>
           <translation type="unfinished"></translation>
           <extra-comment>This is a comment for the translator</extra-comment>

    Happy coding!

    PS: don't forget to mark your post as solved when you finally find the solution

Log in to reply