QtLinguist: defining a single context
From C ++, you can explicitly specify the translation context on a string basis using a static function QCoreApplication::translate(const char* context, const char* text)
instead of the traditional one QObject::tr(const char* text)
(see this doc: http://doc.qt.io/qt-5/qcoreapplication.html#translate for details )
eg.
menuItem->setText(QCoreApplication::translate("UniversalContext", "Exit"));
Similarly, you can specify the context in QML with a function qsTranslate(context, text)
, a la
Text { text: qsTranslate("UniversalContext", "hello") }
They will all be caught lupdate
as belonging to the same context (duplicate lines will only appear once in your file .ts
)
If you have a lot of lines and it becomes painful to specify the context on every call translate/qsTranslate
, it should be fairly easy for you to create a 1-arg macro (C ++) or a js function (qml) as a wrapper.
source to share