Is it possible to change language on Qt at runtime
Asked Answered
W

4

6

in my app I need internationalization. Say I have created several .ts file for different languages e.g., German, French, etc. - together with translated phrases.

Now, say the user wants to change the language at run time. Will it not be possible using Qt approach?

Whittemore answered 12/3, 2013 at 6:58 Comment(1)
See qt-project.org/forums/viewthread/11708Deenadeenya
G
4

You should have a LocaleHandler object in your application and listen to the systemLanguageChanged() signal.

This signal will be called anytime the user changes the language through the settings menu.

When this happens you should update your installed dictionary to the new one with something like the below code:

void MyAppSettings::updateLanguage() {
    QString translations = QString("MyApp%1.qm").arg(QLocale().name());
    Application::instance()->removeTranslator(&mTranslator);
    if (mTranslator.load(translations, "app/native/qm")) {
        qDebug() << "LOAD FINISHED";
        Application::instance()->installTranslator(&mTranslator);
    } else {
        qDebug() << "COULD NOT INSTALL TRANSLATIONS " << translations;
    }
}

This will remove the current dictionary and replace it with the new one for the chosen system language.

Unfortunately this is not enough, as it will not update any existing screens you have for your app. To update existing strings in QML you should add Retranslate.onLanguageChanged to your translated string.

ex:

Label {
    text: qsTrId("header1")  + Retranslate.onLanguageChanged
}

This will update the string for the above lavel every time the translator changes. For more information see: http://developer.blackberry.com/cascades/reference/bb_cascades_qmlretranslate.html

Gwynethgwynne answered 13/3, 2013 at 19:40 Comment(1)
For Qt >=5.10 use the simpler answer from MusaValet
P
9

The function QQmlEngine::retranslate introduced in Qt 5.10 simply reevaluates all property bindings. This includes all the bindings with a call to qsTr() on the right-hand side.

void Settings::switchToLanguage(const QString &language)
{
    if (!m_translator.isEmpty())
        QCoreApplication::removeTranslator(&m_translator);
    m_translator.load(QStringLiteral(":/language_") + language));
    QCoreApplication::installTranslator(&m_translator));
    m_engine->retranslate();
}

For more detail you can check this post too.

Prototype answered 10/2, 2018 at 9:3 Comment(4)
what is the m_engine object ?Quadruped
It's your 'QQmlApplicationEngine'. the application's qml engin that load 'main.qml' file.Prototype
@Quadruped you can get m_engine by QQmlEngine::contextForObject(this)->engine(); Paletot
What will happen if I don't remove the translator? QCoreApplication::removeTranslator()Paletot
G
4

You should have a LocaleHandler object in your application and listen to the systemLanguageChanged() signal.

This signal will be called anytime the user changes the language through the settings menu.

When this happens you should update your installed dictionary to the new one with something like the below code:

void MyAppSettings::updateLanguage() {
    QString translations = QString("MyApp%1.qm").arg(QLocale().name());
    Application::instance()->removeTranslator(&mTranslator);
    if (mTranslator.load(translations, "app/native/qm")) {
        qDebug() << "LOAD FINISHED";
        Application::instance()->installTranslator(&mTranslator);
    } else {
        qDebug() << "COULD NOT INSTALL TRANSLATIONS " << translations;
    }
}

This will remove the current dictionary and replace it with the new one for the chosen system language.

Unfortunately this is not enough, as it will not update any existing screens you have for your app. To update existing strings in QML you should add Retranslate.onLanguageChanged to your translated string.

ex:

Label {
    text: qsTrId("header1")  + Retranslate.onLanguageChanged
}

This will update the string for the above lavel every time the translator changes. For more information see: http://developer.blackberry.com/cascades/reference/bb_cascades_qmlretranslate.html

Gwynethgwynne answered 13/3, 2013 at 19:40 Comment(1)
For Qt >=5.10 use the simpler answer from MusaValet
T
0

You will need to use a LocaleHandler and listen for the onLanguageChanged signal. When received, re-translate the text e.g. myLabel->setText(tr("My label")).

Tobey answered 12/3, 2013 at 8:34 Comment(2)
or if using ui files UiClass->retranslateUi();Tolson
@KamilKlimek as BB10 uses cascades for UI is your statement true? E.g. BB10 apps must use bb::cascades::Application and not QApplication.Tobey
P
0

It is possible with QCoreApplication::installTranslator

Pinite answered 12/3, 2013 at 14:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.