Results 1 to 4 of 4

Thread: Change dynamically language just for some forms (not the app)

  1. #1
    Join Date
    Jan 2009
    Posts
    14
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question Change dynamically language just for some forms (not the app)

    Hi all,
    I have a question about change language dynamically, but not the application language just for same forms.
    The scenario is a qt application that has 2 forms:
    * One simulates a HMI that can change its language
    * Anothe one simulates other HMI that can change its language
    So if I change the language in one HMI (form) like this:
    Qt Code:
    1. if (translator.load(QLatin1String("idiomas_") + locale, resourceDir))
    2. qApp->installTranslator(&translator);
    3.  
    4. ui.retranslateUi(this);
    To copy to clipboard, switch view to plain text mode 

    Everything seems ok, but when I start using the other HMI (form) texts with the language in the othe form are showing. That's because I calling tr() while using the second form and in the first form I've done
    Qt Code:
    1. qApp->installTranslator(&translator);
    To copy to clipboard, switch view to plain text mode 

    Things that I tried without success:
    1) Not call qApp->installTranslator(), but nothing changes.
    2) Not call qApp->installTranslator() and not use tr() function, use
    Qt Code:
    1. translator.traslate()
    To copy to clipboard, switch view to plain text mode 
    instead. This works only with the text in the source but not in ui. Why? Because in funtion "ui.retranslateUi(this)" use the following for the texts:
    Qt Code:
    1. FrmERTMSClass->setWindowTitle(QApplication::translate("FrmERTMSClass", "FrmERTMS", 0, QApplication::UnicodeUTF8));
    To copy to clipboard, switch view to plain text mode 
    So you have to install the translator if you want to use the ui.retranslateUi() function because it call QApplication:translate().

    3) Install the new language, call ui.retranslateUi and the install the language that was before. But my problems are in the tr() functions of the first form.

    So, anyone can help me?
    Does anyone know how to change the language dynamically but only to a part of the application not to all the app?

    Thanks in advance.

  2. #2
    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: Change dynamically language just for some forms (not the app)

    Have separate translators for each dialog. Then by exchanging one translator for another one, you only translate the dialog handled by a particular instance of QTranslator.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2009
    Posts
    14
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Change dynamically language just for some forms (not the app)

    Thank you for your answer.
    So you say to have 1 .ts file for each form, so when I change the language in 1 form remove the translator form the qApp and install the new one.
    But if I have the same literal in both forms, example:
    Qt Code:
    1. tr("Name");
    To copy to clipboard, switch view to plain text mode 
    If I install the translator in the first form in the second one the strings in with tr("Name") will be translate with the last installed translator (that is not the second form language).

    So the solution is: To have separate translator for each dialog, as your said, and NOT using the tr() function. Instead of it use translator.translate() function?

  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: Change dynamically language just for some forms (not the app)

    Quote Originally Posted by holst View Post
    But if I have the same literal in both forms, example:
    Qt Code:
    1. tr("Name");
    To copy to clipboard, switch view to plain text mode 
    If I install the translator in the first form in the second one the strings in with tr("Name") will be translate with the last installed translator (that is not the second form language).
    No. Have a look at arguments of QObject::tr(). However even that is not required since tr() understands the concept of "translation context", each form will have a different context so no such problems you describe should occur (of course provided the ts file has the context information embedded).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Change UI Language of Qt Tools
    By shentian in forum Qt Tools
    Replies: 4
    Last Post: 10th July 2013, 08:45
  2. qml change local language
    By goli in forum Qt Programming
    Replies: 1
    Last Post: 29th May 2011, 12:20
  3. Change language of the application
    By franco.amato in forum Newbie
    Replies: 1
    Last Post: 14th October 2010, 22:45
  4. Change forms with QTreeWidget
    By olli in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2008, 16:58
  5. Replies: 8
    Last Post: 15th May 2007, 09:21

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.