Results 1 to 9 of 9

Thread: QTranslator: getting the translation and original string

  1. #1
    Join Date
    May 2012
    Posts
    37
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default QTranslator: getting the translation and original string

    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?
    Last edited by Kwakkie; 8th April 2015 at 13:11.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTranslator: getting the translation and original string

    Emit signal with original message (probably English) and convert to actual language in Yours class.

  3. #3
    Join Date
    May 2012
    Posts
    37
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTranslator: getting the translation and original string

    Hi

    If you mean, do something like this:

    Qt Code:
    1. emit NewMessage("some string");
    To copy to clipboard, switch view to plain text mode 

    and in the slot have something like this:

    Qt Code:
    1. void MyQObject::OnNewMessage(QString messageString)
    2. {
    3. DisplayString(tr(messageString));
    4. LogString(messageString);
    5. }
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTranslator: getting the translation and original string

    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,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    Kwakkie (9th April 2015)

  6. #5
    Join Date
    May 2012
    Posts
    37
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTranslator: getting the translation and original string

    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 ;-)
    Last edited by Kwakkie; 9th April 2015 at 10:52.

  7. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTranslator: getting the translation and original string

    tr() needs const char * not literal string. So line 3 should be :
    Qt Code:
    1. DisplayString(tr(messageString.toLatin1().constData()));
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTranslator: getting the translation and original string

    Quote Originally Posted by Lesiok View Post
    tr() needs const char * not literal string. So line 3 should be :
    Qt Code:
    1. DisplayString(tr(messageString.toLatin1().constData()));
    To copy to clipboard, switch view to plain text mode 
    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,
    _

  9. #8
    Join Date
    May 2012
    Posts
    37
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTranslator: getting the translation and original string

    Quote Originally Posted by Lesiok View Post
    tr() needs const char * not literal string. So line 3 should be :
    Qt Code:
    1. DisplayString(tr(messageString.toLatin1().constData()));
    To copy to clipboard, switch view to plain text mode 
    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
    Last edited by Kwakkie; 9th April 2015 at 11:52.

  10. #9
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTranslator: getting the translation and original string

    Quote Originally Posted by anda_skoa View Post
    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 ....

Similar Threads

  1. QTranslator in Android
    By marnando in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 25th February 2014, 16:33
  2. QTranslator and polish signs
    By damon_1990 in forum Newbie
    Replies: 0
    Last Post: 11th December 2011, 22:04
  3. QTranslator??
    By nthung in forum Newbie
    Replies: 21
    Last Post: 15th October 2011, 03:33
  4. Help with QTranslator
    By aarelovich in forum Qt Programming
    Replies: 1
    Last Post: 24th July 2009, 22:38
  5. Want to use QTranslator
    By santhoshv84 in forum Qt Programming
    Replies: 1
    Last Post: 27th August 2008, 15:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.