Translate Qt QML application on the fly

本秂侑毒 提交于 2021-01-28 06:00:55

问题


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() 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.



来源:https://stackoverflow.com/questions/47351298/translate-qt-qml-application-on-the-fly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!