Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: Switch QTranslator at runtime?

  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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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

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

    Default Re: Switch QTranslator at runtime?

    And what is the difference between the two?

  13. #13
    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?

    It’s not unconditional necessary to translate a argunent like arg(tr("Some argument")). I don’t know how can i translate a text that is set in a QTextEdit, can you help me?

    Tanks in advance,

    Whitefurrows

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

    Default Re: Switch QTranslator at runtime?

    Nobody says you have to have any arguments. I just said there is a possibility to do that, I didn't say you have to use it. If you have an append() call in your code, just recreate the contents using the same calls. If all those appends have a meaning, you can surely create a list of "commands" and refill the text edit with those commands (think of them as templates) translated to the appropriate language.

  15. #15
    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 how can i create a list of "commands" and refill the text edit with those commands. I have made a example that show you exact what i need, but is not so complex as the original. Please take a look at the example i have need a lot of time to make the example so easy for you. I hope you can help me.

    Thanks in advance,

    Whitefurrows
    Attached Files Attached Files

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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'm sorry, I just don't see the problem, you mean this?
    Qt Code:
    1. void MainWindow::showResult(int result)
    2. {
    3. QString html;
    4. html.append("<HTML><BODY>");
    5.  
    6. html.append("<table border=\"0\" width=\"100%\">");
    7. html.append("<tr><td align=\"center\">" + tr("The result is:") + "</td>");
    8. html.append("<td align=\"center\">" + QString().setNum(result) + "</td></tr>");
    9. html.append("</table>");
    10.  
    11. html.append("</HTML></BODY>");
    12.  
    13. ui.info->document()->setHtml(html);
    14. };
    To copy to clipboard, switch view to plain text mode 

    Just store the result somewhere and call this method again with the stored value when you need to retranslate the text edit.

    BTW. You can substitute
    Qt Code:
    1. QString().setNum(result)
    To copy to clipboard, switch view to plain text mode 
    with
    Qt Code:
    1. QString::number(result)
    To copy to clipboard, switch view to plain text mode 
    .

  17. #17
    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 your great help. i think i can do that, i store the result and call showResult again. What can i do in showInfo i set the text again but that wasn't translate. Can you help me?

    Thanks in advance,

    Whitefurrows

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

    Default Re: Switch QTranslator at runtime?

    You mean this?
    Qt Code:
    1. InfoView info(infos[i], this);
    To copy to clipboard, switch view to plain text mode 

    In this situation you have to call tr() here to do the translation and don't translate in setInfo(). You should only translate strings when they are displayed on screen (or shown to user in some other way). In setInfo() you should only mark strings for translation with QT_TR_NOOP().

  19. #19
    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, that's fine. A last little questin. Now is all working, the only thing what's not OK is that:

    Qt Code:
    1. QT_TR_NOOP("Any information") + QString("<br><br>") + QT_TR_NOOP("for more text look next page");
    To copy to clipboard, switch view to plain text mode 

    What can i do to translate? I don't want translate the html code.

    Thanks in advance,

    Whitefurrows

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

    Default Re: Switch QTranslator at runtime?

    How about:
    Qt Code:
    1. QString s = QString("%1<br><br>%2")
    2. .arg(tr("Any information"))
    3. .arg(tr("for more text look next page"));
    4. setText(s); // or something simmilar
    To copy to clipboard, switch view to plain text mode 

    If you want to avoid translating html code, you have to introduce html tags during actual translation (when tr() is called) or even later - not when using QT_TR_NOOP().

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.