Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automating custom QTranslator contexts

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?

like image 612
Igor Avatar asked Jan 19 '12 21:01

Igor


1 Answers

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.

like image 161
Serge VK Avatar answered Oct 22 '22 11:10

Serge VK