- Qt 6.11
- Qt Core
- <QtTranslation>
- Qt 6.11.1 Reference Documentation

# <QtTranslation>

Marking strings for translation. More...

Header: | `#include <QtTranslation>`

## Functions

QString | qtTrId(const char *id, int n = -1)

## Macros

| QT_TRANSLATE_NOOP3(context, sourceText, disambiguation)
| QT_TRANSLATE_NOOP(context, sourceText)
| QT_TRANSLATE_N_NOOP3(context, sourceText, comment)
| QT_TRANSLATE_N_NOOP(context, sourceText)
| QT_TRID_NOOP(id)
`(since 6.3)` | QT_TRID_N_NOOP(id)
| QT_TR_NOOP(sourceText)
| QT_TR_N_NOOP(sourceText)

## Detailed Description

See also Writing Source Code for Translation.

## Function Documentation

### QString qtTrId(const char *id, int n = -1)

The qtTrId function finds and returns a translated string.

Returns a translated string identified by id. If no matching string is found, the id itself is returned. This should not happen under normal conditions.

If n >= 0, all occurrences of `%n` in the resulting string are replaced with a decimal representation of n. In addition, depending on n's value, the translation text may vary.

Meta data and comments can be passed as documented for QObject::tr(). In addition, it is possible to supply a source string template like that:

`//% <C string>`

or

`\begincomment% <C string> \endcomment`

Example:

     //% "%n fooish bar(s) found.\n"
     //% "Do you want to continue?"
     QString text = qtTrId("qtn_foo_bar", n);

Creating QM files suitable for use with this function requires passing the `-idbased` option to the `lrelease` tool.

Warning: This method is reentrant only if all translators are installed before calling this method. Installing or removing translators while performing translations is not supported. Doing so will probably result in crashes or other undesirable behavior.

Note: This function is reentrant.

See also QObject::tr(), QCoreApplication::translate(), and Internationalization with Qt.

## Macro Documentation

### QT_TRANSLATE_NOOP3(context, sourceText, disambiguation)

Marks the UTF-8 encoded string literal sourceText for delayed translation in the given context with the given disambiguation. The context is typically a class and also needs to be specified as a string literal. The string literal disambiguation should be a short semantic tag to tell apart otherwise identical strings.

The macro tells lupdate to collect the string, and expands to an anonymous struct of the two string literals passed as sourceText and disambiguation.

Example:

     static struct { const char *source; const char *comment; } greeting_strings[] =
     {
         QT_TRANSLATE_NOOP3("FriendlyConversation", "Hello",
                            "A really friendly hello"),
         QT_TRANSLATE_NOOP3("FriendlyConversation", "Goodbye",
                            "A really friendly goodbye")
     };

     QString FriendlyConversation::greeting(int type)
     {
         return tr(greeting_strings[type].source,
                   greeting_strings[type].comment);
     }

     QString global_greeting(int type)
     {
         return qApp->translate("FriendlyConversation",
                                greeting_strings[type].source,
                                greeting_strings[type].comment);
     }

See also QT_TR_NOOP(), QT_TRANSLATE_NOOP(), and Internationalization with Qt.

### QT_TRANSLATE_NOOP(context, sourceText)

Marks the UTF-8 encoded string literal sourceText for delayed translation in the given context. The context is typically a class name and also needs to be specified as a string literal.

The macro tells lupdate to collect the string, and expands to sourceText itself.

Example:

     static const char *greeting_strings[] = {
         QT_TRANSLATE_NOOP("FriendlyConversation", "Hello"),
         QT_TRANSLATE_NOOP("FriendlyConversation", "Goodbye")
     };

     QString FriendlyConversation::greeting(int type)
     {
         return tr(greeting_strings[type]);
     }

     QString global_greeting(int type)
     {
         return qApp->translate("FriendlyConversation",
                                greeting_strings[type]);
     }

See also QT_TR_NOOP(), QT_TRANSLATE_NOOP3(), and Internationalization with Qt.

### QT_TRANSLATE_N_NOOP3(context, sourceText, comment)

Marks the UTF-8 encoded string literal sourceText for numerator dependent delayed translation in the given context with the given comment. The context is typically a class and also needs to be specified as a string literal. The string literal comment should be a short semantic tag to tell apart otherwise identical strings.

The macro tells lupdate to collect the string, and expands to an anonymous struct of the two string literals passed as sourceText and comment.

Example:

     static struct { const char * const source; const char * const comment; } status_strings[] = {
         QT_TRANSLATE_N_NOOP3("Message Status", "Hello, you have %n message(s)",
                              "A login message status"),
         QT_TRANSLATE_N_NOOP3("Message status", "You have %n new message(s)",
                              "A new message query status")
     };

     QString FriendlyConversation::greeting(int type, int count)
     {
         return tr(status_strings[type].source,
                   status_strings[type].comment, count);
     }

     QString global_greeting(int type, int count)
     {
         return qApp->translate("Message Status",
                                status_strings[type].source,
                                status_strings[type].comment,
                                count);
     }

See also QT_TR_NOOP(), QT_TRANSLATE_NOOP(), QT_TRANSLATE_NOOP3(), and Internationalization with Qt.

### QT_TRANSLATE_N_NOOP(context, sourceText)

Marks the UTF-8 encoded string literal sourceText for numerator dependent delayed translation in the given context. The context is typically a class name and also needs to be specified as a string literal.

The macro tells lupdate to collect the string, and expands to sourceText itself.

Example:

     static const char * const greeting_strings[] = {
         QT_TRANSLATE_N_NOOP("Welcome Msg", "Hello, you have %n message(s)"),
         QT_TRANSLATE_N_NOOP("Welcome Msg", "Hi, you have %n message(s)")
     };

     QString global_greeting(int type, int msgcnt)
     {
         return translate("Welcome Msg", greeting_strings[type], nullptr, msgcnt);
     }

See also QT_TRANSLATE_NOOP(), QT_TRANSLATE_N_NOOP3(), and Internationalization with Qt.

### QT_TRID_NOOP(id)

The QT_TRID_NOOP macro marks an id for dynamic translation.

The only purpose of this macro is to provide an anchor for attaching meta data like to qtTrId().

The macro expands to id.

Example:

     static const char * const ids[] = {
         //% "This is the first text."
         QT_TRID_NOOP("qtn_1st_text"),
         //% "This is the second text."
         QT_TRID_NOOP("qtn_2nd_text"),
         0
     };

     void TheClass::addLabels()
     {
         for (int i = 0; ids[i]; ++i)
             new QLabel(qtTrId(ids[i]), this);
     }

See also qtTrId() and Internationalization with Qt.

### `[since 6.3]` QT_TRID_N_NOOP(id)

The QT_TRID_N_NOOP macro marks an id for numerator dependent dynamic translation.

The only purpose of this macro is to provide an anchor for attaching meta data like to qtTrId().

The macro expands to id.

Example:

     static const char * const ids[] = {
         //% "%n foo(s) found."
         QT_TRID_N_NOOP("qtn_foo"),
         //% "%n bar(s) found."
         QT_TRID_N_NOOP("qtn_bar"),
         0
     };

     QString result(int type, int n)
     {
         return qtTrId(ids[type], n);
     }

This macro was introduced in Qt 6.3.

See also qtTrId() and Internationalization with Qt.

### QT_TR_NOOP(sourceText)

Marks the UTF-8 encoded string literal sourceText for delayed translation in the current context (class).

The macro tells lupdate to collect the string, and expands to sourceText itself.

Example:

     QString FriendlyConversation::greeting(int type)
     {
         static const char *greeting_strings[] = {
             QT_TR_NOOP("Hello"),
             QT_TR_NOOP("Goodbye")
         };
         return tr(greeting_strings[type]);
     }

The macro QT_TR_NOOP_UTF8() is identical and obsolete; this applies to all other _UTF8 macros as well.

See also QT_TRANSLATE_NOOP() and Internationalization with Qt.

### QT_TR_N_NOOP(sourceText)

Marks the UTF-8 encoded string literal sourceText for numerator dependent delayed translation in the current context (class).

The macro tells lupdate to collect the string, and expands to sourceText itself.

The macro expands to sourceText.

Example:

     const char * const StatusClass::status_strings[] = {
         QT_TR_N_NOOP("There are %n new message(s)"),
         QT_TR_N_NOOP("There are %n total message(s)")
     };

     QString StatusClass::status(int type, int count)
     {
         return tr(status_strings[type], nullptr, count);
     }

See also QT_TR_NOOP and Internationalization with Qt.