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"));
}
@ -
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);}@
-
@ AlterX ,
what do you mean by ui generated?
is it means the text of buttons, labels, menus etc?
-
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.