Results 1 to 6 of 6

Thread: qt problem with tr() in static member of a class

  1. #1
    Join Date
    Jul 2009
    Posts
    26
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows

    Default qt problem with tr() in static member of a class

    0 down vote favorite


    Hi all,

    I have problem concerning translations in qt. All translations in my porject work fine, but one, which is in a static variable of a class. Corresponding part of code looks as follows

    The header file is similar to this:
    Qt Code:
    1. typedef struct {
    2. int type;
    3. QString problematicString;
    4. } info;
    5.  
    6. MyClass::QObject_Descendant
    7. {
    8. Q_OBJECT;
    9.  
    10. //some functions like constructor, destructor... etc.
    11. ....
    12.  
    13. static info myClassInfo;//class that makes problems
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 
    and in implementation file I initialize the variable as follows:
    Qt Code:
    1. info MyClass::myClassInfo={
    2. 1,
    3. tr("something to be translated")
    4. };
    To copy to clipboard, switch view to plain text mode 
    And whatever I do (having declared array of *char[] with QT_TR_NOOP, then tr() it in initialization of the member) I cannot get myClassInfo.problematicString translated. The weirdest thing is that the text "something to be translated" appears in *.ts file.

    If someone has any hints, please share them with me. Thanks in advance.
    Chris.

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qt problem with tr() in static member of a class

    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Jul 2009
    Posts
    26
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows

    Default Re: qt problem with tr() in static member of a class

    Thank You for a reply. However it doesn't work for me.

    I did as follows:

    Qt Code:
    1. info MyClass::myClassInfo={
    2. 1,
    3. QCoreApplication::translate("mykey", "something to be translated")
    4. };
    To copy to clipboard, switch view to plain text mode 
    and in *.ts file I changed:
    Qt Code:
    1. <context>
    2. <name>mykey</name>
    3. <message>
    4. <source>something to be translated</source>
    5. <translation>My Polish translation</translation>
    6. </message>
    7. </context>
    To copy to clipboard, switch view to plain text mode 

    I removed "type="unfinished"" from the <translation> tag. Then executed lrelease and I installed the translator with this application. It is installed for sure, since other texts (but those not in static member) are translated correctly.

    If You have any other ideas, please share them. I tried a lot of things and now have no solution for this issue.

    Thank You once more. All the best!

  4. #4
    Join Date
    Jul 2009
    Posts
    26
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows

    Default Re: qt problem with tr() in static member of a class

    On the other forum (StackOverflow) I got the answer from Caleb Huitt - cjhuitt which seems to be the solution/explanatioin to the problem. It goes as follows:

    "Static variables are instantiated (and thus, constructor code run) before your int main function is run. The translation code is set up in the QApplication constructor (I believe), which isn't run until your int main function has been entered. Thus, you are trying to get the translation of a string before the code to support it has been initialized.

    To avoid this, you could either accept that the given string isn't translated and explicitly translate it every time it is used, or use the Construct on First Use idiom instead of a static member variable."

    Thank You for Your time and hope this post also helps someone else.

  5. #5
    Join Date
    Jul 2010
    Posts
    53
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qt problem with tr() in static member of a class

    Qt Code:
    1. //at global scope
    2. const char *yourText = QT_TRANSLATE_NOOP("MyClass", "something to be translated");
    3. ...
    4. info MyClass::myClassInfo={
    5. 1,
    6. MyClass::tr(yourText);
    7. };
    To copy to clipboard, switch view to plain text mode 

    hint: tr() uses className() to determine context therefore MyClass must have Q_OBJECT macro.
    use lupdate and Qt Linguist instead manual editind of *.ts

  6. #6
    Join Date
    Jul 2009
    Posts
    26
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows

    Default Re: qt problem with tr() in static member of a class

    Hey. Thanks for it. But still, it doesn't work. There is no translated text appearing. If the code is:

    Qt Code:
    1. const char *trText = QT_TRANSLATE_NOOP("MyClass", "something to be translated");
    2. ...
    3. info MyClass::myClassInfo={//static vaiable initialization
    4. 1,
    5. MyClass::tr(trText);
    6. };
    To copy to clipboard, switch view to plain text mode 

    But if I do "myClass.problematicString=MyClass::tr(trText) ;" in the class constructor, all is fine. So probably this still concerns the problem mentioined on StackOverflow - strings are created before translator is run (even though it is in main function straight after QApplication definition).

    Thanks,
    Chris.

Similar Threads

  1. 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
  2. Using a static member in a class
    By feraudyh in forum Newbie
    Replies: 4
    Last Post: 29th April 2010, 10:58
  3. Using a QMutex as a static class member
    By emostar in forum Qt Programming
    Replies: 2
    Last Post: 15th June 2009, 13:48
  4. Replies: 22
    Last Post: 8th October 2008, 13:54
  5. Replies: 3
    Last Post: 19th February 2008, 13:10

Tags for this Thread

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.