In my project we want to translate the user-interface but keep the technical logs in English. I agree those two kind of messages should remain separated but sometimes we have some error messages that we want to display and log at the same time.
Using QObject::tr you get the translated string and no way to retrieve the source string.
How could I manage log of original version + display of translated version without copy/pasting ?
I am open to any suggestion, including design constraints.
I solved this problem in one of my own projects by introducing a special helper class holding both the original string and the translated one. Here's the rough sketch of how it looks:
Then the code using this class works like this:
The main part within the approach is the use of QT_TR_NOOP macro. What it does is marking the string literal enclosed into it as the one requiring extraction during
qmakestep for further translation. UnlikeQObject::tr, this macro does not convert the non-translated text into the translated one. If you want to access the translated text, you need to calltrmanually later on - as I did in the above example within the constructor ofLocalizedString.Note that
QT_TR_NOOPis designed to be used within classes i.e. the context for translation would be the name of the class inside some method of which the macro is present. If you have free standing functions or if you want to specify the context yourself, use QT_TRANSLATE_NOOP macro instead - its second argument is the translation context.Upd.: one more tip: in my real implementation of
LocalizedStringit hasoperator<<which prints the original, non-localized string. It is convenient because this way you can just pass the object ofLocalizedStringclass toQDebugwithout calling itsoriginalStringmethod.