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 -
Hi
it will not see #defines sadlyhttps://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,
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.
-
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 istr(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 aQT_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.) -
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?
Thanks. -
@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.
-
@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", greeting_strings[type]); }