Results 1 to 6 of 6

Thread: translate on fly

  1. #1
    Join Date
    Feb 2009
    Posts
    53
    Thanks
    5

    Default translate on fly

    Hi I have some problem. I want to translate my programm on fly. I have this code
    Qt Code:
    1. void MainWindow::changeLanguage(QAction *action)
    2. {
    3.  
    4. if(action == engLAction){
    5. QTranslator translator;
    6. translator.load(QString("programm_en"), "." );
    7. qApp->installTranslator(&translator);
    8. }
    9. else if(action == czeLAction){
    10. QTranslator translator;
    11. translator.load(QString("programm_cs"), "." );
    12. qApp->installTranslator(&translator);
    13. }
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 
    before make I use "lupdate programm.pro" and "lrelease programm.pro" then I translate documents in *.ts and use "lrelease programm.pro" again. Then I use make.
    But when I try to change language on fly nothing happens Could someone help me, where I make mistake? thanks to all

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: translate on fly

    your QTranslators get destroyed when they go out of scope. You are (un)lucky this code does not crash.

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: translate on fly

    Hi,

    Define the QTranslator as a member variable and load the proper language. Then, when "changeLanguage" is called, you have to set all the widget texts:
    widget1.setText(tr("Hello")); //This will return your translation depending on language loaded
    Òscar Llarch i Galán

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

    Noxxik (23rd February 2009)

  5. #4
    Join Date
    Feb 2009
    Posts
    53
    Thanks
    5

    Default Re: translate on fly

    Quote Originally Posted by ^NyAw^ View Post
    Hi,

    Define the QTranslator as a member variable and load the proper language. Then, when "changeLanguage" is called, you have to set all the widget texts:
    thanks now it is OK, but I thought, that the translation will use *.ts or *.qm files, which I generate with lupdate, lrelease.
    So I think, that i dont use this *.ts and *.qm files, isnt it?

  6. #5
    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: translate on fly

    See [WIKI]Dynamic translation[/WIKI].
    J-P Nurmi

  7. #6
    Join Date
    May 2008
    Location
    duesseldorf, germany
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: translate on fly

    Quote Originally Posted by Noxxik View Post
    So I think, that i dont use this *.ts and *.qm files, isnt it?
    You're still requiring the qm files to be loaded and do your stuff (with global member, of course) - but the translator can only work dynamically when you're following the guides from Wiki.
    That's simply because usually you setup static text once (e.g. in constructor) - where the tr-function handles the translation.
    Once your text is assigned, installing a new translator will only affect any text being processed by tr-function again

Similar Threads

  1. QTransform scaling and translate feature.
    By AmolShinde_8 in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2008, 10:31
  2. Can I translate data with GUI element.
    By mourad in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2008, 09:41
  3. Translate QFileDialog
    By ^NyAw^ in forum Qt Programming
    Replies: 4
    Last Post: 7th April 2008, 15:20
  4. Translate the SimpleChat example into PyQt
    By GuS in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2007, 20:16
  5. Switch QTranslator at runtime?
    By whitefurrows in forum Qt Programming
    Replies: 22
    Last Post: 6th November 2006, 11:32

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.