Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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"
    #define SETTINGS_GENERAL_NICKNAME_DEFAULT "NoName"

    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?
    Thanks.



  • @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

    Hi
    it will not see #defines sadly

    https://doc.qt.io/qt-5/i18n-source-translation.html
    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

    Hi,

    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:

    #define SETTINGS_GENERAL_NICKNAME_DEFAULT tr("NoName")
    
    QString nick = ttSettings->value(SETTINGS_GENERAL_NICKNAME, SETTINGS_GENERAL_NICKNAME_DEFAULT).toString();
    

    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;
    char const *const SETTINGS_GENERAL_NICKNAME_DEFAULT;
    // in cpp
    char const *const SETTINGS_GENERAL_NICKNAME = "general_/nickname";
    char const *const SETTINGS_GENERAL_NICKNAME_DEFAULT = QT_TR_NOOP("NoName");
    

    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.)


Log in to reply