简体   繁体   中英

Translate Qt QML application on the fly

I'm trying to translate a qt QML application that is filled with

tr("string");

All over the place, If I set the translator before the app starts it works perfectly, but I'm having troubles doing it on the fly. The only solution seems to be the empty string hack but I dont want to search for every "tr" and add the empty string

Up to Qt 5.9 , the easiest solution is to add an empty string to each translatable string: How to do dynamic translation in QML

From Qt 5.10 , it is possible to retranslate all strings by calling: QQmlEngine::retranslate

More information can be found in the QtQuick Internationalization documentation:

You can change the language that Qt translation functions use by adding and removing translators with QCoreApplication::installTranslator() and QCoreApplication::removeTranslator() . Afterwards you can call QQmlEngine::retranslate() to trigger a refresh of all bindings that use translations. As a result, your user interface will switch, dynamically, to the newly selected language.

Alternatively, you can also forward a QEvent::LanguageChange event to your application's QQmlEngine instance or connect your own signal to QQmlEngine::retranslate() .

Note that at the time of writing, only beta releases of Qt 5.10 are available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM