Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28

Thread: Dynamic translation (was: How to get QApplication pointer)

  1. #21
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to get QApplication pointer

    Then just keep all the strings in code Slovenian. Sorry, but I don't understand what in this concept is it so hard to understand. Once you have installed a German translator, QObject::tr("some Slovenian text") will return the text in German, provided that the particular string was translated.
    J-P Nurmi

  2. #22
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get QApplication pointer

    Ok, but I still do not get what do I have to implement in retranslate() function. I just set arbitrary tr() strings in it then if, for instance, german translator is automatically loaded?
    Qt 5.3 Opensource & Creator 3.1.2

  3. #23
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to get QApplication pointer

    It's YOU who installs the translator:
    Qt Code:
    1. // <no translator installed>
    2. qDebug() << tr("slovenian text"); // outputs "slovenian text" in slovenian
    3.  
    4. qApp->installTranslator(germanTranslator);
    5. qDebug() << tr("slovenian text"); // outputs "slovenian text" in german
    6.  
    7. qApp->installTranslator(italianTranslator);
    8. qDebug() << tr("slovenian text"); // outputs "slovenian text" in italian
    To copy to clipboard, switch view to plain text mode 

    Now, lets take a QLabel holding a string as an example. QLabel does not automatically translate anything because it doesn't make any sense. First of all, not all strings all translated and secondly, it would add unnecessary overhead. Every widget receives a language change event when language changes. This is the point when you have to ract. You must catch the event and reset all strings you want to translate in current language.

    Qt Code:
    1. // this will create a new label with string "slovenian text" in current language, whatever the current translator happens to be
    2. QLabel* label = new QLabel(tr("slovenian text"));
    3.  
    4. // later on, you switch the language by installing another translator
    5. // the label introduced above will not react anyhow, you will have to catch the event and do it yourself
    6. // this time, QObject::tr() returns the same string in different language, in language which was just installed
    7. void SomeWindow::changeEvent(QEvent* event)
    8. {
    9. if (event->type() == QEvent::LanguageChange)
    10. label->setText(tr("slovenian text"));
    11. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  4. The following user says thank you to jpn for this useful post:

    MarkoSan (22nd January 2008)

  5. #24
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to get QApplication pointer

    @admins: would it possible to rename this helpful thread to what exactly it reflect ?

    Sorry for being offtopic.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  6. #25
    Join Date
    Feb 2006
    Posts
    8
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Dynamic translation (was: How to get QApplication pointer)

    I use dynamic translation extensively in my application.

    The way I solved the problem is by inheriting QApplication to MyApp.

    In the header of MyApp I have a list of QTranslator members for each language/locale. In MyApp I also have a ChangeLang(LangEnum LANG) function.

    This will load the chosen language.

    In MyApp I have some strings that are application wide specific (eg: translation of the days of the week or the months) Open windows get an event as described in the helpfile at Trolltech.

    MyApp is accessed as
    Qt Code:
    1. MyApp* locApp= (MyApp*)qApp;
    2. locApp->LoadLanguage(ENG);//loads english
    3. locApp->LoadLanguage(NLD);//loads Dutch
    4. locApp->LoadLanguage(ZHO);//loads Mandarin
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void MyApp::ChangeLang(LangEnum LANG)
    2. {
    3. if(lang!=CurLang)
    4. {
    5. CurLang=lang;
    6. //Ensure that we have no translation loaded
    7. removeTranslator(&SPAtran);
    8. removeTranslator(&ENG_AUtran);
    9. removeTranslator(&DEUtran);
    10. removeTranslator(&FRAtran);
    11. removeTranslator(&NLDtran);
    12. removeTranslator(&ZHOtran);
    13. removeTranslator(&PORtran);
    14. removeTranslator(&ENG_UStran);
    15. removeTranslator(&ENGtran);
    16. switch(lang)
    17. {
    18. case ENGLISH:
    19. #ifdef VER_US
    20. installTranslator(&ENG_UStran);
    21. #elif defined(VER_AU)
    22. installTranslator(&ENG_AUtran);
    23. #elif defined(VER_UK)
    24. installTranslator(&ENGtran);
    25. #else
    26. installTranslator(&ENGtran);
    27. #endif
    28. break;
    29. case GERMAN:
    30. installTranslator(&DEUtran);
    31.  
    32. break;
    33. case DUTCH:
    34. installTranslator(&NLDtran);
    35. break;
    36. case SPANISH:
    37. installTranslator(&SPAtran);
    38. break;
    39. case FRENCH:
    40. installTranslator(&FRAtran);
    41. break;
    42. case MANDARIN:
    43. installTranslator(&ZHOtran);
    44. break;
    45. case ITALIAN:
    46. //Not defined
    47. break;
    48. case GREEK:
    49. //Not defined
    50. break;
    51. case PORTUGESE:
    52. installTranslator(&PORtran);
    53. break;
    54. case UNINITIALISED:
    55. default:
    56. break;
    57. }
    58. if(MANDARIN==lang)
    59. {
    60. CentralFont.setFamily( "mandarin" );
    61. }
    62. else
    63. {
    64. CentralFont.setFamily( "Freesans" );
    65. }
    66. MyCurSet.LoadPresentations();
    67. langInit();
    68. FillEditLists();
    69. emit newLang();
    70.  
    71. }
    72. }
    To copy to clipboard, switch view to plain text mode 
    The translations are removed before loading a new one. The documentation has nothing about reloading an already loaded translation, so out of memory paranoia I simply remove all that is not used and load the one I use.

    The header looks something like
    Qt Code:
    1. typedef enum _LangEnum
    2. {//This enum must match the enum in the SGCP document
    3. UNINITIALISED=-1,
    4. ENGLISH=0,
    5. GERMAN,
    6. DUTCH,
    7. SPANISH,
    8. FRENCH,
    9. MANDARIN,
    10. ITALIAN,
    11. GREEK,
    12. PORTUGESE
    13. } LangEnum;
    14. class MyApp : public QApplication
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. QFont CentralFont;
    20. LangEnum CurLang;
    21. QTranslator SPAtran;
    22. QTranslator ENGtran;
    23. QTranslator ENG_AUtran;
    24. QTranslator DEUtran;
    25. QTranslator FRAtran;
    26. QTranslator NLDtran;
    27. QTranslator ZHOtran;
    28. QTranslator PORtran;
    29. QTranslator ENG_UStran;
    30. void ChangeLang(LangEnum lang);
    31. signals:
    32. void newLang();
    33. }
    To copy to clipboard, switch view to plain text mode 

    As you can see this version also switches fonts for writing mandarin. Looks really neat! Our customers are very impressed! The planning of the competition also took a speedbump with this nice little extra.

  7. #26
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Dynamic translation (was: How to get QApplication pointer)

    Well, I've done it. Thanks to all that helped me. I now even can support dynamic number of languages. I have a list of languages in mysql database table. I load it at runtime an then the magic works!!!
    Qt 5.3 Opensource & Creator 3.1.2

  8. #27
    Join Date
    Apr 2008
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Dynamic translation (was: How to get QApplication pointer)

    Quote Originally Posted by MarkoSan View Post
    Well, I've done it. Thanks to all that helped me. I now even can support dynamic number of languages. I have a list of languages in mysql database table. I load it at runtime an then the magic works!!!
    How have you done it?
    At the moment I am working exactly on this problem!

    Thanks for an answer.

  9. #28
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Dynamic translation (was: How to get QApplication pointer)

    Quote Originally Posted by Nippler View Post
    How have you done it?
    At the moment I am working exactly on this problem!
    Did you read the wiki article: [WIKI]Dynamic translation[/WIKI]?
    J-P Nurmi

  10. The following user says thank you to jpn for this useful post:

    Nippler (16th May 2008)

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.