Reputation: 45
The Qt translation documentation states that you can use the QT_TRANSLATE_NOOP
macro for strings that are going to be translated via both tr()
and QApplication::translate()
, e.g.:
static const char* HelloStr = QT_TRANSLATE_NOOP("MyClass", "Hello World");
void MyClass::helloWorld()
{
qDebug() << tr(HelloStr);
qDebug() << QApplication::translate("MyClass", HelloStr);
}
As far as I understood, I just have to make sure that the context given to QT_TRANSLATE_NOOP
is the same as the class name that calls the tr()
function. But when running this code, I get the following output:
Hello World
Hallo Welt
Only the second version, using QApplication::translate()
does work. Does anybody know why the first version doesn't work?
Upvotes: 3
Views: 3382
Reputation: 2576
Maybe you don't know how to reduce your problem to a minimal reproducible example. This is important because doing so you may find something wrong in your code yourself, or facilitate that others can diagnose it comfortably.
For instance, you don't provide a complete definition of MyClass
. Being tr()
a member of QObject
, I suppose that MyClass
inherits directly or indirectly from QObject
, but maybe you forgot something in the process. Another possibility is that you used Q_DECLARE_TR_FUNCTIONS(MyClass)
and MyClass
so it does not need to be a subclass of QObject
?
Anyway, I've tested both options and it worked for me with no error. Here is my minimal example. You need to run lrelease
to create "i18n_es.qm" that should be copied to the output directory.
test.pro
QT -= gui
CONFIG += c++11 console
SOURCES += main.cpp
TRANSLATIONS += i18n_es.ts
main.cpp
#include <QCoreApplication>
#include <QTranslator>
#include <QDebug>
static const char* HelloStr = QT_TRANSLATE_NOOP("MyClass", "Hello World");
class MyClass
{
Q_DECLARE_TR_FUNCTIONS(MyClass)
public:
void helloWorld() {
qDebug() << tr(HelloStr);
qDebug() << QCoreApplication::translate("MyClass", HelloStr);
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTranslator translator;
qDebug() << "loaded translations ?" << translator.load("i18n_es");
qDebug() << "installed translator ?" << QCoreApplication::installTranslator(&translator);
MyClass test;
test.helloWorld();
return a.exec();
}
i18n_es.ts
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="es_ES">
<context>
<name>MyClass</name>
<message>
<location filename="main.cpp" line="5"/>
<source>Hello World</source>
<translation>Hola mundo</translation>
</message>
</context>
</TS>
output
loaded translations ? true
installed translator ? true
"Hola mundo"
"Hola mundo"
Upvotes: 3
Reputation: 2576
The documentation says about both macros that:
They merely mark the text for extraction by the lupdate tool. The macros expand to just the text (without the context).
So, the macros do not directly interfere with tr() or QApplication::translate(). The macros are intended to mark the strings for lupdate to extract them when creating the source .ts files that will be used by lrelease to create .qm translation files.
Upvotes: 0