Results 1 to 7 of 7

Thread: Using tr() in static consts and variables

  1. #1
    Join Date
    Feb 2011
    Posts
    10
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Using tr() in static consts and variables

    Hello guys, how are you?

    I'm newbie in Qt, brazilian and i'm working with Qt just 4 months.

    I have a doubt about if is possible use the tr() method with constants or variables.

    For example: I'm doing that...

    static const QString TitleSubgenreOther = tr("Outros"); //Meanings: 'others', but's in portuguese

    ui->category_list->item(2)->setText(TitleSubgenreOther);

    When i apply the english language with translate() method, those constants aren't translated. How can i do?

    Thanks and sorry my english.

  2. #2
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using tr() in static consts and variables

    What about using the disambiguation parameter, i.e.
    Qt Code:
    1. static const QString TitleSubgenreOther = QApplication::tr("Outros", "Global");
    To copy to clipboard, switch view to plain text mode 

  3. #3
    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: Using tr() in static consts and variables

    I think that the problem is because all static variables are initiated BEFORE QApplication instantiation.

  4. #4
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using tr() in static consts and variables

    Quote Originally Posted by Lesiok View Post
    I think that the problem is because all static variables are initiated BEFORE QApplication instantiation.
    Actually, even without static, it doesn't work. When I made m y suggestion above, I thought it could have worked since that added a new entry to the .ts file, but then it doesn't make any difference. Oh well...

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Using tr() in static consts and variables

    const values are typically built into the object file at compile time; their value cannot be changed at runtime, which is when translation takes place. Because translation is a dynamic, runtime operation, you can't use const values; declaring something to be const when it's value can change is a contradiction to begin with. You really don't want them to be const; you want them to be changeable.

  6. #6
    Join Date
    Feb 2011
    Posts
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Using tr() in static consts and variables

    Quote Originally Posted by SixDegrees View Post
    const values are typically built into the object file at compile time; their value cannot be changed at runtime, which is when translation takes place. Because translation is a dynamic, runtime operation, you can't use const values; declaring something to be const when it's value can change is a contradiction to begin with. You really don't want them to be const; you want them to be changeable.
    Whether or not the variable should be declared const is a design consideration that has no bearing on this problem as I understand it. It would take an *extremely* aggressive optimizer to get a const QString to be written directly into the object file; QString objects require several heap allocations, which by definition is not part of the object file. Simply removing the const will accomplish nothing as initialization still only occurs once.

    In this case, the problem seems to be one of lifetime. He initializes the variable, then translates to English, but since the variable is already initialized it doesn't notice the change. Keep the "static const", but move the tr() to where the string is used, and I believe everything will work as expected. You also apparently need QT_TR_NOOP to mark the string for translation.

    Qt Code:
    1. static const QString Portugese = qT_TR_NOOP("text");
    2. ...
    3. const QString English = tr(Portugese);
    To copy to clipboard, switch view to plain text mode 
    Last edited by conner686; 16th March 2011 at 22:49. Reason: Poor word choice & correction

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Using tr() in static consts and variables

    I don't know if it helps you, but you can always solve it by declaring a helper class instead of string constants, something like:
    Qt Code:
    1. class MyStrings{
    2. public:
    3. static QString TitleSubgenreOther(){
    4. return QObject::tr("Outros" );
    5. }
    6. static QString AnotherStringConstant(){
    7. return QObject::tr("Text to translate");
    8. }
    9. // ...
    10. };
    11.  
    12. ui->category_list->item(2)->setText( MyStrings::TitleSubgenreOther() );
    To copy to clipboard, switch view to plain text mode 
    Last edited by stampede; 16th March 2011 at 23:04. Reason: previous post was edited, remark removed

Similar Threads

  1. Build static QT + static SSL problem.
    By makzimi in forum Qt Programming
    Replies: 3
    Last Post: 23rd April 2012, 23:45
  2. accessing static member variables of one class in another class
    By jasonknight in forum General Programming
    Replies: 5
    Last Post: 6th September 2010, 14:53
  3. how to share variables
    By sksingh73 in forum Newbie
    Replies: 4
    Last Post: 2nd July 2010, 04:54
  4. new problem with simple C++ static variables
    By landonmkelsey in forum Qt Programming
    Replies: 2
    Last Post: 18th August 2008, 05:42
  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.