Suggestions for using tr() with variables
-
You want to be able to dynamically translate a word taken from this xml file. So this could be any word, right? Then how do you want lupdate to populate the language file correctly? lupdate cannot guess the words that will come out of your xml.
-
In general, that is possible.
The only thing is, you have to manage the ts files by hand :-(
So if you create the ts files by hand, you can call tr with variables :-) -
[quote author="neFAST" date="1319202492"]You want to be able to dynamically translate a word taken from this xml file. So this could be any word, right?[/quote]
Yes and yes.
[quote author="neFAST" date="1319202492"]Then how do you want lupdate to populate the language file correctly? lupdate cannot guess the words that will come out of your xml.[/quote]
No, but I can - so that's why I thought that I could write those "fake" translations (unused translations, with some suitable context, possibly placed in some dead code sections) to make lupdate to know the words.
-
[quote author="Gerolf" date="1319202765"]In general, that is possible.
The only thing is, you have to manage the ts files by hand :-([/quote]Oh, no, that's something I would not like to do :(
[quote author="Gerolf" date="1319202765"]So if you create the ts files by hand, you can call tr with variables :-)[/quote]
There is no some comment fields for lupdate/linguist to tell the possibly content of variables? If not, then I try to make that fake translation method work...
-
Did anyone solve this?? I have a massive amount of strings to be translated for a big menu structure and would prefer them not to be hard coded - prefer to read them from an XML file - managing the ts file by hand would be a huge task prone to error.
-
[quote author="Dolphin" date="1338472810"]Did anyone solve this?? I have a massive amount of strings to be translated for a big menu structure and would prefer them not to be hard coded - prefer to read them from an XML file - managing the ts file by hand would be a huge task prone to error.[/quote]
Why don't you do the translations directly in your XML then?Otherwise, the only option I can see is that you write a kind of pre-compiler that parses your XML, and generates code for it that contains the strings to be translated using of the methods that can be picked up by lupdate.
-
Using the Menu XML is an option but it would be preferable not to give such a key file out to all and sundry (this system has to cope with lots of languages, braille, speech and hands free so a LOT of things can go wrong if the menu xml is messed up).
According to the documentation QT_TR_NOOP should work but I have not managed that yet.
Thanks for your thoughts, hope you had a good loooooooooong weekend.
-
QT_TR_NOOP can only be used with static texts. Text that have a value during the pass that lupdate does over your source code. At that point the XML is not loaded of course.
So, if you really want this, the custom compiler to create a source file from the XML is the only viable solution.
-
I have just now got this code:
@
QString text = QT_TR_NOOP("test, test!");QPushButton hello(QPushButton::tr(text.toStdString().c_str())); QPushButton hello2(QPushButton::tr("Hello from Nicola"));
@
giving me this TS file:
@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="en_US">
<context>
<name>QPushButton</name>
<message>
<source>Hello from Nicola</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>TranslationObject</name>
<message>
<source>test, test!</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>
@
Which is exactly what I need - woop! My code was not in a QObject, doh! ( Little things :-S )
Thank you :-)Edit: Please use @ tags to surround code sections; Andre
-
If your code is not a QObject, you can use QObject::tr. It is a static method after all...
-
Well, that is what I thought but my code did not work until I put it in a QObject (a frustrated experiment) but I have just moved it back out of the QObject (deleted the TS) and it works. Must have been something funky about the update of the TS). Either way, it works, yay!