QtLinguist: defining a single context

How can I define one context in the QtLinguist.ts file instead of having one context per file?

I need this because I have the same strings displayed in different files for which I want the same translation (and no duplicates)

+3


source to share


1 answer


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.

+3


source







All Articles