Results 1 to 20 of 23

Thread: Switch QTranslator at runtime?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Switch QTranslator at runtime?

    How can i change QTranslator dynamic? I try this:

    Qt Code:
    1. int main(int argc, char** argv)
    2. {
    3. QApplication app( argc, argv );
    4.  
    5. //...
    6.  
    7. if (language=="de"){
    8. QTranslator* translator = new QTranslator();
    9. translator->load(":/translation/language + ".qm");
    10. app.installTranslator(translator);
    11. }
    12.  
    13. //...
    14. }
    15.  
    16. ___________________________________________________________________________
    17.  
    18.  
    19. void ApplicationSetup::switchLanguage()
    20. {
    21. static QTranslator* translator=0;
    22.  
    23. if (translator)
    24. {
    25. qApp->removeTranslator(translator);
    26. delete translator;
    27. }
    28.  
    29. translator = new QTranslator(0);
    30.  
    31. if (language =="en")
    32. {
    33. translator->load(":/translation/language + ".qm");
    34. qApp->installTranslator( translator );
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

    The language don’t change at runtime. I use ui-files and classes code by hand. All strings look like this tr("Please translate me").

    Can somebody tell me how can i translate all strings in all open widgets at runtime?

    Thanks in advance,

    Whitefurrows

  2. #2
    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: Switch QTranslator at runtime?

    QtCentre Wiki: Dynamic translation
    J-P Nurmi

  3. #3
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Switch QTranslator at runtime?

    Hi,

    thank you for the example, that’s good, but i have one more question.

    My QMainWindow is build without Designer and i can’t use ui.retranslateUi(this);. How can i translate the strings in my QMainWindow?

    Is it right that i must use the following code in all Widgets, wich build with the designer to translate the strings?

    Qt Code:
    1. void MyWindow::changeEvent(QEvent* event)
    2. {
    3. if (event ->type() == QEvent::LanguageChange)
    4. {
    5. // retranslate designer form (single inheritance approach)
    6. ui.retranslateUi(this);
    7.  
    8. // retranslate other widgets which weren't added in designer
    9. retranslate();
    10. }
    11.  
    12. // remember to call base class implementation
    13. QMainWindow::changeEvent(event);
    14. }
    To copy to clipboard, switch view to plain text mode 

    How can i tell the designer, don’t translate all strings? I want translate only definite strings.

    Another problem is, the designer translate html in the strings. How can i tell the disigner translate the strings without html?

    Thanks in advance,

    Whitefurrows

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

    Default Re: Switch QTranslator at runtime?

    If you don't use Designer, you have to implement your own method for retranslating strings (in the above example it is called retranslate()) and you have to call it on every language change.

  5. #5
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Switch QTranslator at runtime?

    Hi,

    thank you, but i have a problem.

    I do this e.g.:
    Qt Code:
    1. void MyMainWindow::createActions()
    2. {
    3. actionOpen = new QAction(QIcon(":/myPic.png"), tr("&Open File"), this);
    4. }
    To copy to clipboard, switch view to plain text mode 

    and i must do this:
    Qt Code:
    1. void MyMainWindow::retranslate()
    2. {
    3. actionOpen->setText(tr("&Datei öffnen"));
    4. }
    To copy to clipboard, switch view to plain text mode 
    I think that’s dirty and a lot to do, but it’s OK.

    What can i do in a function like that:
    Qt Code:
    1. void AnyWidget::anyFunction(){
    2. QString html;
    3.  
    4. html.append("<table border=\"0\" width=\"100%\">");
    5. html.append("<tr><td>" + tr("Text to Translate") + "</td>");
    6. html.append("<td>" + tr("Text to Translate") + "</td></tr>");
    7. html.append("</table>");
    8.  
    9. myTextEdit->document()->setHtml(html);
    10. }
    To copy to clipboard, switch view to plain text mode 
    I can’t translate in retranslate()

    Thanks in advance,

    Whitefurrows

  6. #6
    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: Switch QTranslator at runtime?

    Quote Originally Posted by whitefurrows View Post
    I do this e.g.:
    Qt Code:
    1. void MyMainWindow::createActions()
    2. {
    3. actionOpen = new QAction(QIcon(":/myPic.png"), tr("&Open File"), this);
    4. }
    To copy to clipboard, switch view to plain text mode 

    and i must do this:
    Qt Code:
    1. void MyMainWindow::retranslate()
    2. {
    3. actionOpen->setText(tr("&Datei öffnen"));
    4. }
    To copy to clipboard, switch view to plain text mode 
    I think that’s dirty and a lot to do, but it’s OK.
    Hmm, you are not supposed to hardcode all the languages in the code. Use only one string in code and then use translation files to translate it to any language you want.
    Qt Code:
    1. void MyMainWindow::createActions()
    2. {
    3. actionOpen = new QAction(QIcon(":/myPic.png"), tr("&Open File"), this);
    4. }
    5. void MyMainWindow::retranslate()
    6. {
    7. actionOpen->setText(tr("&Open File")); // same string here, put "&Datei öffnen" into the translation file
    8. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. #7
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Switch QTranslator at runtime?

    Hi,

    ok i think i have understood, but what can i do with a function like that:
    Qt Code:
    1. void AnyWidget::anyFunction(){
    2. QString html;
    3.  
    4. html.append("<table border=\"0\" width=\"100%\">");
    5. html.append("<tr><td>" + tr("Text to Translate") + "</td>");
    6. html.append("<td>" + tr("Text to Translate") + "</td></tr>");
    7. html.append("</table>");
    8.  
    9. myTextEdit->document()->setHtml(html);
    10. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance,

    Whitefurrows

  8. #8
    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: Switch QTranslator at runtime?

    There is no other way around than somehow identifying the translated parts (using regexp maybe?) and then setting the translated text again after passing it to tr().
    J-P Nurmi

  9. #9
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Switch QTranslator at runtime?

    Hi,

    sorry i don't know what are you mean. Can you give me a example please.

    Thanks in advance,

    Whitefurrows

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

    Default Re: Switch QTranslator at runtime?

    I think that in that situation you should tr() the whole html code and provide translations for it in the translation file.

    Qt Code:
    1. html->append(tr("<HTML><BODY>Text to be translated goes here<BR>\\n"
    2. "and you can even have arguments for it like this: %1</BODY></HTML>")
    3. .arg(tr("Some argument")));
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Switch QTranslator at runtime?

    Hi,

    thank you, but this is not my problem. I have attach a example and hope you can help me.

    Thanks in advance,

    Whitefurrows
    Attached Files Attached Files

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
  •  
Qt is a trademark of The Qt Company.