Reputation: 655
I'm trying to change my application language dynamically :
void MainWindow::ChangeEvent(QEvent *event, QString *language)
{
if (event->type() == QEvent::LanguageChange) {
RetranslateInterface(language);
//ui.retranslateUi(this);
}
QWidget::changeEvent(event);
}
void MainWindow::RetranslateInterface(QString *language) {
QString lang = language->remove(2, language->length());
lang = lang.toLower();
qDebug()<<"lang"<<lang;
lang = "qt_" + lang + ".qm";
qDebug()<<"lang"<<lang;
if ( Translator )
qApp->removeTranslator( Translator );
qDebug()<<"Translator->load( lang)"<< Translator->load( QApplication::applicationDirPath()+"/"+ lang);
qApp->installTranslator( Translator );
//qDebug()<<"tr >>>>"<<qApp->translate("MainWindow","my english text");
setWindowTitle(tr("my english text"));
}
void MainWindow::Slot_ChLangue(QAction* Trigaction)
{
QString Selectedlanguage = Trigaction->text();
qDebug()<<"selected language"<<Selectedlanguage;
if (!Selectedlanguage.isEmpty()) {
QEvent *translate = new QEvent(QEvent::LanguageChange);
ChangeEvent(translate, &Selectedlanguage);
}
}
"qt_*.qm" was loaded but setWindowTitle(tr("my english text"));
didn't set the appropriate text
I tried this in the main function it didn't work either:
QTranslator translator;
if(QFile::exists(QApplication::applicationDirPath()+"/qt_fr.qm"))
qDebug()<<"loaded"<<translator.load(QApplication::applicationDirPath()+"/qt_fr.qm");
app.installTranslator(&translator);
qDebug()<<"out en français"<<app.tr("my english text");
I get always "my english text" in english.
Any help will be appreciated.
Upvotes: 0
Views: 3654
Reputation: 12600
Regarding your latest comment:
There is no predefined Dictionary, you need to translate the strings yourself (or have someone else translate them).
In order to do this, you need the following steps:
TRANSLATIONS += qt_fr.ts
to your .pro filelupdate -verbose <yourprofile>.pro
in order to create an XML-structured .ts file which contains the texts which shall be translatedlinguist qt_fr.ts
in order to start Qt Linguist and easily translate stringslupdate
There is a tutorial on using translations here.
Upvotes: 3