Oumaya
Oumaya

Reputation: 655

qApp->installTranslator() and tr didn't translate?

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

Answers (1)

Tim Meyer
Tim Meyer

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:

  • Have tr() calls in your code
  • Add something like TRANSLATIONS += qt_fr.ts to your .pro file
  • Call lupdate -verbose <yourprofile>.pro in order to create an XML-structured .ts file which contains the texts which shall be translated
  • Call linguist qt_fr.ts in order to start Qt Linguist and easily translate strings
  • Generate the .qm file from Qt Linguist or using lupdate
  • Run the application

There is a tutorial on using translations here.

Upvotes: 3

Related Questions