PDA

View Full Version : QTranslator: getting the translation and original string



Kwakkie
8th April 2015, 13:07
Hi all

Our application has a QTranslator installed and loads different language files depending on a user setting. This all works fine and languages display correctly.

Now, say I created my own messagebox class. It will listen to a signal that can come from all over the application. E.g. to display a messages, I can simply do
emit NewMessage(tr("some string"));

In the messagebox class, I will display the message in a widget using the language of choice, no problem. But I also want to log the message to a file using the original untranslated string. How can I do this? How can I get the original string?

Lesiok
8th April 2015, 18:26
Emit signal with original message (probably English) and convert to actual language in Yours class.

Kwakkie
9th April 2015, 07:47
Hi

If you mean, do something like this:


emit NewMessage("some string");

and in the slot have something like this:



void MyQObject::OnNewMessage(QString messageString)
{
DisplayString(tr(messageString));
LogString(messageString);
}


That won't work. The string will never get translated as tr() needs a literal string.

anda_skoa
9th April 2015, 07:55
My recommendation would be to do the logging separately.

This enables you to include contextual information, e.g. function name, location, etc., as well as use data or wording not suitable for display to the user.

If you really must access source string and translation, my approach would be to derive from QTranslator and make it store the source string of the last call to translate() and have a getter function for retrieving it.

Cheers,
_

Kwakkie
9th April 2015, 09:20
Logging it separately will duplicate strings though. For this logging, all I want to know is what message the users gets at what time (logger will save the time)

Deriving from QTranslate seems a good idea. I'll try it out.

Added after 7 minutes:

Just to let you know it works this way, thanks. Been a while since I used the "mutable" keyword ;-)

Lesiok
9th April 2015, 11:32
tr() needs const char * not literal string. So line 3 should be :
DisplayString(tr(messageString.toLatin1().constDat a()));

anda_skoa
9th April 2015, 11:43
tr() needs const char * not literal string. So line 3 should be :
DisplayString(tr(messageString.toLatin1().constDat a()));

You would still need to pass the original string literal through tr() (or the respective NOOP macro) somewhere, because it not only performs translation at runtime, it also serves as the extraction marker for lupdate.

Cheers,
_

Kwakkie
9th April 2015, 11:45
tr() needs const char * not literal string. So line 3 should be :
DisplayString(tr(messageString.toLatin1().constDat a()));

Again, have you tried this? This does not work as tr has no reference, the string will not end up in the .ts file. You need to pass the literal.

http://en.wikipedia.org/wiki/String_literal

Lesiok
9th April 2015, 14:45
You would still need to pass the original string literal through tr() (or the respective NOOP macro) somewhere, because it not only performs translation at runtime, it also serves as the extraction marker for lupdate.

Cheers,
_

Man learns all his life ....