Results 1 to 7 of 7

Thread: tr() and static fields

  1. #1
    Join Date
    Jan 2006
    Posts
    39
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default tr() and static fields

    Hi,

    I want to have all the translated strings in one place in my source code, so I've created a Strings class that extends QObject that consists of static QString objects.

    The header file contains declarations like:

    Qt Code:
    1. const static QString PENDING;
    To copy to clipboard, switch view to plain text mode 

    and then a .cpp file contains

    Qt Code:
    1. const QString Strings::PENDING(tr("Pending"));
    To copy to clipboard, switch view to plain text mode 

    I can run lupdate on the .cpp file, open the resulting .ts file in linguist and see that I need to translate "Pending". I do that, save everything, and run lrelease.

    Then in the main method of my app, before creating widgets I run:

    Qt Code:
    1. QString file ("/usr/local/swdevel/share/dbaker/lib/instr_fr");
    2. bool loaded = translator->load(file);
    3. TRACE("Loaded: %s\n", loaded ? "true" : "false");
    4. app.installTranslator(translator);
    To copy to clipboard, switch view to plain text mode 

    but when I try to display or just print out the value of the variable, I only get the English.

    Is there some problem with using tr in static members of a class?

    Thanks in advance.

    Derek

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: tr() and static fields

    Can you provide a compilable example?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: tr() and static fields

    Quote Originally Posted by drkbkr View Post
    but when I try to display or just print out the value of the variable, I only get the English.

    Is there some problem with using tr in static members of a class?
    The problem is that you do the translation when the variable is initialized, what happens before you install the translator.

    What you could try is:
    Qt Code:
    1. static const char PENDING[] = QT_TR_NOOP( "..." );
    2. ...
    3. label->setText( tr( PENDING ) );
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Posts
    39
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: tr() and static fields

    I'm attaching an example that shows a couple different tries at having translatable text in a class of static variables. The only text I can get to show up translated is the "maybe" string where the literal text to translate is right in the setText() call.

    The one I'd really like to get to work is the "No" string where the QString and tr call are in the strings class.

    Any help is greatly appreciated.

    Thanks.
    Derek
    Attached Files Attached Files

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: tr() and static fields

    Quote Originally Posted by drkbkr View Post
    The one I'd really like to get to work is the "No" string where the QString and tr call are in the strings class.
    Would such class (or namespace) be acceptable to you?
    Qt Code:
    1. class strings
    2. {
    3. public:
    4. enum StringConstant
    5. {
    6. Yes = 0,
    7. No = 1,
    8. AndSoOn = 2
    9. };
    10.  
    11. static QString get( StringConstant str );
    12. ...
    13. };
    14. ...
    15. label->setText( strings::get( strings::No ) );
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: tr() and static fields

    Another possibility is:
    Qt Code:
    1. class TranslatedString
    2. {
    3. public:
    4. TranslatedString( const * const str ) : _str( str ) {}
    5. operator QString() const { return QObject::tr( _str ); }
    6.  
    7. private:
    8. const * const _str;
    9. };
    10. ...
    11. TranslatedString strings::No = QT_TR_NOOP( "No" );
    12. ...
    13. label->setText( strings::No );
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: tr() and static fields

    So you can make it..


    Qt Code:
    1. #define _AbortAction_ \
    2. QCoreApplication::translate("coniglioe","comment",QApplication::UnicodeUTF8)
    3. #define _NextAbortAction_ \
    4. QCoreApplication::translate("conigliob","comment",QApplication::UnicodeUTF8)
    5. #define _PrevAction_ \
    6. QCoreApplication::translate("coniglioc","comment",QApplication::UnicodeUTF8)
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 10:12
  2. Qt4 MacOSX UB Xcode and a static Qt build?
    By kuroyume0161 in forum Installation and Deployment
    Replies: 15
    Last Post: 18th March 2007, 08:10
  3. Replies: 2
    Last Post: 16th March 2007, 09:04
  4. Accessing to a static variable from the same class
    By xgoan in forum General Programming
    Replies: 6
    Last Post: 5th March 2007, 10:50
  5. Replies: 4
    Last Post: 14th February 2006, 21:35

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.