Dynamic Language Translation in Qt



  • Hi All,

    I have written one application in that application i have to do reboot the system then only after language translation applied.
    What i want to change the language without reboot i mean at run time i have to change the all text in translated localized form.
    In my sample application in main i have written following code.

    @
    QTranslator * translator;
    translator = new QTranslator;
    translator->load( "QM file path where the language file exist ");
    app.installTranslator(translator);
    @

    From another file or form how can i know the language has changed and how to handle it ?
    Can someone explain me how to do with this code ? or sample code or link will helped me a lot.

    Thanks,
    Neel

    [Edit] Use @ for code



  • You need to react on the language change event within your widgets. The change event is posted to all widgets if there is a change to the installed Qt translator.

    "Dynamic translations":http://doc.qt.nokia.com/4.7/internationalization.html#dynamic-translation

    example:
    @ void MyWidget::MyWidget(QWidget* parent)
    : QWidget(parent)
    {
    retranslate();
    }

    void MyWidget::changeEvent(QEvent* event)
    {
    if (event->type() == QEvent::LanguageChange)
    {
    // retranslate designer form
    ui.retranslateUi(this);

           // retranslate other widgets which weren't added in designer
           retranslate();
       }
       
       // remember to call base class implementation
       QWidget::changeEvent(event);
    

    }

    void MyWidget::retranslate()
    {
    mylabel->setText(tr("Text"));
    }
    @



  • Sorry for my intrusion...I've just located an entire desktop app, but I'm able to see translations just for my texts and not for ui generated texts.
    shall I have to use dynamic translation to change ui texts?!



  • Some time ago I did it as follows;

    First create actions for different languages and connect their triggered() SIGNALs with language specific SLOTs.

    @
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    connect(ui->actionDanish, SIGNAL(triggered()), this, SLOT(changeLanguageToDanish()));
    connect(ui->actionEnglish, SIGNAL(triggered()), this, SLOT(changeLanguageToEnglish()));
    QApplication::instance()->installTranslator(&translator);

    }@

    Then from that SLOTs call to language change function.
    @
    void MainWindow::changeLanguageToEnglish()
    {
    changeLanguageTo(English_file_location);
    }

    void MainWindow::changeLanguageToDanish()
    {
    changeLanguageTo(Danish_file_location);
    }@

    Load the language file

    @void MainWindow::changeLanguageTo(const QString &language_file_location)
    {
    translator.load(language_file_location);
    }@

    Call language change event

    @void MainWindow::changeEvent(QEvent *e)
    {
    if(e->type() == QEvent::LanguageChange)
    {
    ui->retranslateUi(this);
    }
    QMainWindow::changeEvent(e);

    }@



  • So I have to do something to translate ui....I've tried to implement changeEvent for one ui file and now it is correctly located.
    My question is: why my tr(text) in the code is automatically translated at runtime and ui generated tr did not?



  • @ AlterX ,

    what do you mean by ui generated?

    is it means the text of buttons, labels, menus etc?



  • Yes,
    it creates ui_xxxx.h with all localized code QApplication::translate(), but even they are present like my own code, ui form doesn't get translated at runtime like my code does!



  • generally I set the text of button as _[Click Me] _.
    Then by using 'Qt Linguist' set the text in different languages for [Click me] .

    And I'm not doing any other thing except my previous reply. ChangeEvent do the rest.



  • Ok so changeEvent is necessary to translate uis...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.