Qt World Summit: Submit your Presentation

Translation from an header file

  • Hi,
    I searched some ways to translate lines from an header file, but for my use case all I found it's not helpfull.
    My h file just contain a list of default settings values, this values are using to create an ini file.
    Here is a part of .h file:
    #ifndef SETTINGS_H
    #define SETTINGS_H

    #include <QSettings>

    #define SETTINGS_GENERAL_NICKNAME "general_/nickname"

    after that, "noname" is use by follow:
    QString nick = ttSettings->value(SETTINGS_GENERAL_NICKNAME, tr(SETTINGS_GENERAL_NICKNAME_DEFAULT)).toString();
    my question is how to translate "noname" from my header file?

  • @Oreonan said in Translation from an header file:

    QString nick = ttSettings->value(SETTINGS_GENERAL_NICKNAME, tr(SETTINGS_GENERAL_NICKNAME_DEFAULT)).toString();
    I forgot to precise that using tr() function isn't helpfull, I just tried this on line above but "noname" isn't present in .ts files

  • Lifetime Qt Champion

    it will not see #defines sadly

    Section "Using QT_TR_NOOP() and QT_TRANSLATE_NOOP() in C++"

    so maybe using QT_TR_NOOP on the define can work but not sure.

  • Hi and thanks for your answer,
    unfortunately it doesn't work. I've no error when building, but "noname" it's still not appearing in .ts file

  • Lifetime Qt Champion


    If I may, using translated keys for settings is usually not a good idea. If your user changes its locale for some reason, they're going to lose the settings.

  • This defines are mainly use for display. For example, when an user set his nickname to a blank line, we display the default nickname for other users.
    So we have to translate "noname" in user language

  • The problem is most likely between your source code and how the tool for extracting translations for your .ts file from it works. The tool does not use a regular C++ parser. This also means that it does not run the preprocessor. What I do expect is that it looks for things like tr("...") in the source code. What you have instead is tr(SETTINGS_GENERAL_NICKNAME_DEFAULT) which does not fit this pattern.

    You could try to to use tr already in your define:


    However, I would expect that tr() in it self does not work as the translation tool cannot extract the context for the tr-function. QT_TR_NOOP() could help instead.

    The problem with your initial code is not that it would not use the translation when run, it is only that the text is not extracted for translation. You can sprinkle a QT_TR_NOOP("NoName") or rather a QT_TRANSLATE_NOOP("MyClass", "NoName") somewhere in your source code. Maybe right above it's use (the compiler would throw this out). Or maybe as a global variable. Then the translation tool can pick it up.

    I don't see the exact reason why you choose a #define in the first place. It would only make sense if you could define them as a compiler parameter (with a corresponding guard). This does not make sense for translations, though. In modern C++ style you should aim for constants instead of #define whereever you can. This certainly applies here:

    // in header
    char const *const SETTINGS_GENERAL_NICKNAME;
    // in cpp
    char const *const SETTINGS_GENERAL_NICKNAME = "general_/nickname";

    The rest of your code could then remain unchanged. If you are on the newest standard you can shorten the above by using constinit. (I hope I did everything right in my example with the whole global const thing.)

  • Hi,
    thanks for your ansers. I added my string to .ts files using QT_TRANSLATE_NOOP("MainWindow", "NoName") in my .h file.
    Unfortunately I ahave again another problem. I have another file (common.cpp) with a lot of functions used in all files of project, in this file I have a function to return nickname of an user. If this user has set his nickname to a blank string, it's automatically set to "NoName" with this line:
    nickname = QString("%1").arg(SETTINGS_GENERAL_NICKNAME_DEFAULT);
    I tried the following:
    nickname = QString("%1").arg(QObject::tr(SETTINGS_GENERAL_NICKNAME_DEFAULT));
    But I still get "NonMae" instead of translated string.
    How I missed?

  • Lifetime Qt Champion

    @Oreonan said in Translation from an header file:

    How I missed?

    You simply used a macro without reading the documentation on how to use it.

  • I understand that I need to use QCoreApplication::translate, but I don't understand how.

  • Lifetime Qt Champion

    @Oreonan said in Translation from an header file:

    but I don't understand how.

    What exactly is not understandable with this example?

    static const char *greeting_strings[] = {
        QT_TRANSLATE_NOOP("FriendlyConversation", "Hello"),
        QT_TRANSLATE_NOOP("FriendlyConversation", "Goodbye")
    QString global_greeting(int type)
        return QCoreApplication::translate("FriendlyConversation",

  • Oh, yes, OK, just read correctly :)
    It's working perfectly, thanks.

Log in to reply