Qt World Summit: Submit your Presentation

Translate with tr() string defined with #define

  • Hello,
    I have a string defined with #define str, and I need to translate it, but not everywhere.

    The #define is in a separate file LtDefs.h :

    #define LT_FAMILY_NAME "Lt\xE2\x84\xA2" //TM utf8 code

    and in certains classes, I need to translate it. I've tried to use

    tr(LT_FAMILY_NAME ) but it doesnt add the string to translation list.

    The class has the Q_OBJECT macro, but not the LtDefs.h

    Is there a way to translate a string defined with #define ? I've seen the QT_TR_NOOP macro, but I dont really understand it

  • Lifetime Qt Champion

    @Mwoua said in Translate with tr() string defined with #define:


    As far as I remember it would NOT pick up defines for translation.
    It seems to be looking for tr("") and no other form except ""
    Make sense that lupdate cannot see variables / defines as its not via the compiler.

    one way is to use a function

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

  • My problem is that the #define is in a header included in several place in the code, so I can't replace it by a function easily

  • Lifetime Qt Champion

    Well there is no way to get it to grab the #define.

    Also, it will not get translated anyway, as the tr() macro is key to make it work.

    So even if you add it manually to the translation file.
    It will not work when you load a new language.

    It does not have to be a function.
    This also works

     const char* LT_FAMILY_NAME [] = {

    qDebug() << LT_FAMILY_NAME;

    Should work instead of the #define with no altering code.

  • My header is included in several place, so it gives me a variable redefinition. I dont think there is an easy way to get this to work, so I've an ugly workaround

  • Lifetime Qt Champion


    • a variable redefinition.

    You can fix that with extern.

    in .h

    extern const char* LT_FAMILY_NAME[] ;

    and in .cpp (any)
    const char* LT_FAMILY_NAME [] = {

  • Thats a good idea, I'll try it when I figure out how to write MC (a part of what im trying to translate) in superscript in title bar

Log in to reply