View Full Version : lupdate and dynamic/loaded strings (how to get into TS file)

23rd December 2010, 10:38
I have many strings/tokens in my code that come from external sources (DBs, files, network connections). All of these need to be translated as would a normal string. The documentation on lupdate is however kind of sparse/non-extant in this regards.

What kind of file can I produce to get lupdate to see all the strings that it needs to translate? I suppose I could just generate a dummy CPP file. Yet surely other projects have this need and there is a standard way of doing this.

Note that mostly the strings are not dynamic and I *can* generate a list of them. In some cases I have some truly dynamic strings, but that's another problem.

23rd December 2010, 12:52
If you can generate a cpp file containing all the strings, then you can use QtGlobal::QT_TR_NOOP and QtGlobal::QT_TRANSLATE_NOOP. They just mark the strings to be translated.

23rd December 2010, 12:58
Generating a CPP file does appear to be the most popular way of doing this.

Thanks for pointing out QT_TRANSLATE_NOOP, that lets me specify an arbitrary context.

9th August 2012, 18:39
I have the exact same use case as mortoray. Since this thread is two years old, is there now a better way to accomplish this, or is the recommendation still to generate a dummy cpp file for lupdate to process? If so, would such a file only consist of the following?

QT_TRANSLATE_NOOP("MyContext", "MyString1")
QT_TRANSLATE_NOOP("MyContext", "MyString2")
QT_TRANSLATE_NOOP("MyContext", "MyString3")
// ...

Or would it perhaps be possible to simply wrap the translatable text in the external resource file in QT_TRANSLATE_NOOP() and have lupdate process that file instead (thereby eliminating the need to generate a new cpp file every time the translatable text is changed in the external resource file)?

9th August 2012, 22:08
I found this old Qt doc that discusses this issue a bit: http://doc.qt.nokia.com/qq/qq03-swedish-chef.html#when.tr.is.not.enough.

It looks like a dummy cpp file that looks like this would work:

#if 0
qApp->translate( "MyContext", "MyString1" );
qApp->translate( "MyContext", "MyString2" );
qApp->translate( "MyContext", "MyString3" );

Has anyone successfully used this approach?