Reputation: 73
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?
Upvotes: 7
Views: 3196
Reputation: 2590
There's something easier than that. Use qtTrId/qsTrId (Qt/QML) instead of tr/qsTr and add the -idbased parameter to your lrelease calls. ID based translation has no context at all.
Upvotes: 0
Reputation: 89
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.
Upvotes: 3