Qt lupdate and QTranslator group source strings into exclusive contexts. This means that a translation defined in one context will not be accessible within a different context.
The default context inside C++ is the name of a class that has overridden QObject::tr()
. The default context inside declarative QML is the current filename without extension. To override the translation context, one would use qApp->translate( "context", "source" )
or qsTranslate( "context", "source" )
in C++ or QML.
I want to be able to use a single common translation context across a large project and I am finding that specifying the translation context with every single translation function is very tedious. Is there any existing or future Qt translation framework extensions that would simplify this task? I am looking for something that would be as simple as tr( "source" )
and qsTr( "source" )
, but use a system-wide or project-wide default context. Any ideas?
You could use the Q_DECLARE_TR_FUNCTIONS() macro applied to a class definition that acts solely as a context:
class CONTEXT_CLASS {
Q_DECLARE_TR_FUNCTIONS(CONTEXT_CLASS)
};
where CONTEXT_CLASS could be as short as you'd like, let's say X (hoping that doesn't conflict with anything else in your code). That would make your tr() statements
X::tr("source");
Don't try to #define something to shorten X::tr, as that won't get picked up by the translation tool.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With