tags:

views:

46

answers:

2

Please consider we have a menu which has text set "MyMenu" and I want to change the menu text by clicking a button in the same widget from "MyMenu" to "МойМеню". Could you bring a code snippet please for that operation?

+4  A: 

Hey,

Have a look at "Dynamic Translation", http://qt.nokia.com/doc/4.6/internationalization.html

void MyWidget::changeEvent(QEvent *event)
{
    if (e->type() == QEvent::LanguageChange)
    {
        titleLabel->setText(tr("Document Title"));
        ...
        okPushButton->setText(tr("&OK"));

        // You could also use : retranslateUi(QWidget*);
    } 
    else
    {
        QWidget::changeEvent(event);
    }
}

This will be helpful to you as well : http://doc.trolltech.com/4.6/qcoreapplication.html#installTranslator

Basically, when you call : qApp->installTranslator(MyAppTranslator) it will create a QEvent::LanguageChange.

So, provide a simple QComboBox with English/Russian, and when the selected language changes, call qApp->installTranslator(MyAppTranslator);. Then make sure your buttons are properly set up in changeEvent, and that's it !

Hope it helps a bit !

Andy M
A: 
int main(int argc, char *argv[])
{

    QApplication app(argc, argv);

    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));//this is the solution
    .............
}

And in the code you can dynamically change the strings if you set them from the begining by using tr() function [tr("your text")].

Narek