⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

QT_TRANSLATE_NOOP inside static const QMap?

  • Hi there,

    i have a QMap with some QStrings i want to translate at runtime. So becuase these are static const i have to use QT_TRANSLATE_NOOP() i think - or i'am worng?

    I declare and initialize this QMap in one of my Header-Files after the Class-Part.

    #ifndef MYCLASS_H
    #define MYCLASS_H
    #include <QString>
    #include <QMap>
    class MyClass
    static const char * const flowers[] = {
        QT_TRANSLATE_NOOP("OrderForm2", "Medium Stem Pink Roses"),
        QT_TRANSLATE_NOOP("OrderForm2", "One Dozen Boxed Roses"),
        QT_TRANSLATE_NOOP("OrderForm2", "Calypso Orchid"),
        QT_TRANSLATE_NOOP("OrderForm2", "Dried Red Rose Bouquet"),
        QT_TRANSLATE_NOOP("OrderForm2", "Mixed Peonies Bouquet"),
    #endif // MYCLASS_H´

    When i run lupdate it found the strings and the i entered the translations for it in QtLinuist and release it. Then i run my Application if tried out the translation but they did not work.
    So where is the problem? Thanks in advice!

  • Lifetime Qt Champion

    Yes this marks them for extraction by the lupdate tool.

    You need a call to tr() to translate it.

    QString FriendlyConversation::greeting(int type)
        static const char *greeting_strings[] = {
        return tr(greeting_strings[type]); <<<<<<< returns via tr()


  • yes i tried to print it with (for my example above)

    for (int i = 0; flowers[i]; ++i)
            qDebug() << tr(flowers[i]);

    but i don't get the translated content printed out.

  • Lifetime Qt Champion

  • @mrjj said in QT_TRANSLATE_NOOP inside static const QMap?:

    How do you load the translation ?

    i'm exactly loading the translation this way. It work's very well with my other translations, for example the Menu-Entries or other QLabel on which i directly call tr(VALUE). But not the one i posted. Could it be problemativ because i declare and initialize it outside the class?
    But i found no other way to declare and initialize a static const variable.

    I did some research and found ouut that:
    When i call qDebug() << qApp->translate( "OrderForm2", flowers[0] );
    instead of qDebug() << tr(flowers[0]); i got the translated content.

    But i did it not work without calling qApp->translate() ?

  • Lifetime Qt Champion

    Well you are using it with a context ("OrderForm2")
    and the sample for that actually use app trans

    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", <<<< context

    So it could be related to that. ( context)

  • ok it seems so. I tried your other example from the official Qt-Source. But when i declare the greetings_Strings[]-Array outside the class the Strings will not be detectet for translation. I think these are only wokring inside a Class or Method :/

  • Lifetime Qt Champion

    Oh, are you saying the greeting_strings sample do not work?
    It is not in a class. Mind the context thing. It does matter. :)

  • @mrjj yes. outside a class it is only working with the context stuff and QT_TRANSLATE_NOOP. QT_TR_NOOP only works inside class. So thanks for helping out.

  • Lifetime Qt Champion

    Well when not in an object, the context is different, so
    I guess that's why to specify one, makes it work.
    Well just use what works, it all ends up being the same type
    of lookup in a table so TR or qApp->translate should not matter much.

  • @mrjj okay - thanks for help!