QtQuick 中的本地化自上而下 [英] Localization in QtQuick from top to bottom

查看:75
本文介绍了QtQuick 中的本地化自上而下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过几周断断续续的研究,我仍然没有找到关于如何在 QtQuick 中执行翻译/本地化的详尽指南(例如,使用 QML 语言,而不是 C++ 或 Python).

After several weeks of on-and-off research, I still haven't found a thorough guide on how to perform translation/localization in QtQuick (as in, using the QML language, not C++ or Python).

总的来说,我问的是在 QtQuick 中尽可能多地正确本地化项目的步骤是什么,使用最少或最好不使用 C++.

更具体地说,在我对 QtQuick 如何处理本地化的理解中,我需要填补很多漏洞.

More specifically, there are a good number of holes I need to fill in my understanding of how QtQuick handles localization.

到目前为止,我已经:

  • 将 QT_TR_NOOP() 附加到我所有的可翻译字符串中,以便在运行时进行翻译

  • Appended QT_TR_NOOP() to all of my translatable strings for translation at runtime

使用 lupdate_only{SOURCES += LanguageStrings.qml} 将包含所有字符串的文件添加到我的 .pro 文件中

Added my file containing all strings to my .pro file using lupdate_only{SOURCES += LanguageStrings.qml}

使用 QtLinguist 生成翻译文件

Generated translation files using QtLinguist

但是,我打算实现一个动态更改语言的选项,我见过的唯一一个不完全在 C++ 中的翻译示例基本上为每种语言创建了一个项目实例,而不是更改字符串在运行时.

However, I intend to implement an option for dynamically changing the language, and the only example I've seen regarding translation which wasn't entirely in C++ essentially created an instance of the project for each language, rather than changing the strings at runtime.

那么,如何在运行时更改语言?有我可以设置的变量吗?它是从系统区域设置中提取的吗?我还没有看到一个可靠的答案.

So, how do I change the language at runtime? Is there a variable I can set? Is it pulled from system locale? I haven't seen a solid answer on this.

有什么想法吗?

推荐答案

你可以用最少的 C++ 做到这一点(至少我认为这是最少的).我过去曾使用安装应用程序的系统的区域设置来完成此操作(直接在 main() 中):

You can do this with minimal C++ (at least I think this is minimal). I've done this in the past using the locale of the system the app is installed on like this (directly in main()):

QGuiApplication app(argc, argv);

QTranslator translator;

if(translator.load(":/translations/myapp_" + QLocale::system().name())) {
    app.installTranslator(&translator);
} else {
    qDebug() << "Unable to load translation";
} 

翻译需要在资源系统中才能使用.您当然可以在运行时根据来自 QML 的用户输入(例如在您的应用程序的设置中)触发上述内容.下面是一些示例代码(https://wiki.qt.io/How_to_do_dynamic_translation_in_QML).我不知道 QML-only 的方法来做到这一点.

The translations need to be in the resource system for the above to work. You can of course trigger the above based on user input from QML (e.g. in the settings of your app) at run-time. Here is some example code to do this (https://wiki.qt.io/How_to_do_dynamic_translation_in_QML). I am not aware of a QML-only way to do this.

我也尝试了其他有效的方法.您可以在 Loader 元素中拥有您的 UI,并在翻译器更改后简单地使用该元素的 setSource 函数.我很快整理了一个小例子,其中还包括一个如何在 Loader 外部更改 UI 元素的示例,如果需要的话 (https://github.com/Conntac/qtExamples).

I tried something else which works, too. You can have your UI in a Loader Element and simply use the setSource functions of that element after the translator changed. I quickly put together a small example, which also includes an example of how to change UI elements outside the Loader, if that is needed (https://github.com/Conntac/qtExamples).

这篇关于QtQuick 中的本地化自上而下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆