PDA

View Full Version : Localization for dynamically generated text.



vaibhav
14th March 2011, 08:58
Hi,
I have a XML file which has the text for all the Widgets that i am using in my application.
I read this XML file at runtime and set the text for my widgets.

Now i want to create localization for these texts which are in that XML files.
lupdate doesn't recognize the texts used to create ts files for this case.

Literals String works fine like pushbutton->setText(tr("Hi"));

But something like pushbutton->setText(strText);
where strText is QString which contains the text from that XML file .
lupdate doesn't recognize for latter text to create ts file .

Any help would be appreciated.
Thanks .

wysota
14th March 2011, 09:12
You need to wrap all the possible literal strings into QT_TR_NOOP calls apart from using tr for the translation:

QString FriendlyConversation::greeting(int type) {
static const char *greeting_strings[] = {
QT_TR_NOOP("Hello"),
QT_TR_NOOP("Goodbye")
};
return tr(greeting_strings[type]);
}

Other than that I would suggest to apply an xsl transformation on your original xml file to make it conformant with Qt's .ts files. Then you just don't need lupdate.

vaibhav
14th March 2011, 09:15
Thanks .
Could you elaborate more on second point about XSL.

wysota
14th March 2011, 09:17
Google for XSLT.

MarekR22
14th March 2011, 09:21
How do you imagine to have localized text from custom data? If text from file is none deterministic then it is impossible (extremely hard) to provide proper translations! For example see google translate.
If you have well defined texts then IMHO you trying to do something strange what should be handled fully by Qt localization engine.
Write what you are trying to do not how you try to achieve this.

vaibhav
14th March 2011, 10:22
I have some 50 files from which i read these texts.These are not the .cpp or .h files but the files which stores the text for some widgets.
The way QT lupdate reads all the literals withen the file(.cpp or .h) wraped under tr() and creates .ts for it.
In similar way i want some approch by which my texts from those 50 file be the part of ts file so that i can provide the translation for the same.

wysota
14th March 2011, 10:25
So use XSLT or store your texts in a format compliant with .ts file structure.

vaibhav
14th March 2011, 10:37
Sorry But thses are not the XML files but some format read by my application.It not only stores the text but the other widget information like height with color etc.
I cant change these files either.

wysota
14th March 2011, 11:07
So convert them. How many times do I have to repeat myself? Or build a custom backend for QTranslator around your files.

vaibhav
14th March 2011, 11:33
I may sound little foolish but how to build a custom backend for QTranslator around my files.

wysota
14th March 2011, 12:03
You reimplement QTranslator::translate().