Is it possible to change language on Qt at runtime

user2054339 picture user2054339 · Mar 12, 2013 · Viewed 11.4k times · Source

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?

Answer

Musa picture Musa · Feb 10, 2018

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.