New in Qt 5.10: Dynamic Language Change in QML

My favourite new feature in Qt 5.10 is the inconspicuous function QmlEngine::retranslate(). Finally, seven years after QML’s birth, there is a Qt way to change the language of your application at runtime. There is no need for workarounds any more (see How to do dynamic translation in QML for the standard workaround).

I wrote a simple application demonstrating the new feature. If we click the British (German) flag on the right-hand side, the language of the labels on the left-hand side is changed accordingly.

How does the dynamic language change work?

The workaround required us to add a global QML property to each call of the qsTr() function:

    text: qsTr("Hello") + rootItem.emptyString

When the language changes, the notification signal of the global property rootItem.emptyString is emitted. Hence, every binding with this global property is reevaluated and every text string is translated.

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. In a future version, it will only reevaluate all bindings containing qsTr(). The new function automates what we had to emulate manually with the global property.

We can drop the global property in the bindings of translatable texts and simply write

    text: qsTr("Hello")

The function for switching the language looks as follows (with error handling removed).

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();
}

If another translator has been loaded before, this translator is removed. Then, the new translator is loaded and installed. Finally, the call to m_engine->retranslate() reevaluates all the property bindings containing calls to qsTr(). This makes for simpler and more robust QML code than before.

Leave a Reply

Your email address will not be published. Required fields are marked *