问题
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
回答1:
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()
andQCoreApplication::removeTranslator()
. Afterwards you can callQQmlEngine::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 toQQmlEngine::retranslate()
.
Note that at the time of writing, only beta releases of Qt 5.10 are available.
来源:https://stackoverflow.com/questions/47351298/translate-qt-qml-application-on-the-fly