带运行时语言开关的本地化时钟
该示例演示了在CMake中使用 Qt 的翻译和本地化功能的最佳实践,尤其是在运行时更改应用程序的语言。 Qt Quick中使用 Qt 的翻译和本地化功能的最佳实践,尤其是在运行时更改应用程序的语言。它扩展了更简单的本地化时钟示例。
有关翻译 Qt 应用程序的更多信息,请参阅Qt Linguist Manual。
用户界面
该示例以您系统的语言显示当前时间和日期。用户界面文本还针对以下语言进行了本地化:英语、德语、法语、西班牙语、意大利语、日语、韩语、葡萄牙语、阿拉伯语和中文。用户还可以使用菜单按钮选择不同的语言和地区。
英文版应用程序:
运行时更改语言后的德语版本:
执行
应用程序由四个部分组成:
CMakeLists.txt
CMake 集成与本地化时钟示例的CMake 集成相同。详情请参考该页面。
main.cpp
应用程序的起点,负责加载 QML 模块。
int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; QObject::connect( &engine, &QQmlApplicationEngine::objectCreationFailed, &app, [](const QUrl &) { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.loadFromModule("qtexamples.localizedclockswitchlocale", "Main"); return app.exec(); }
翻译管理器(translator.h、translator.cpp)
TranslatorManager
是一个 QML 单例类,在运行时管理语言和地域切换。该类的核心方法是TranslatorManager::switchLanguage()
,它接受一个代表语言代码的字符串。
该方法用Q_INVOKABLE 标记,而 TranslatorManager 类则用QML_ELEMENT 和QML_SINGLETON 声明。这允许从 QML 直接访问该方法;另请参阅Main.qml。
voidTranslatorManager::switchLanguage(constQString语言) { QLocalelocale(lang); QLocale::setDefault(locale); qApp->removeTranslator(&m_translator);// not necessary from Qt 6.10 if(m_translator.load(locale, "clock"_L1, "_"_L1, ":/i18n/"_L1)&& qApp->installTranslator(&m_translator)) { m_engine.retranslate(); }else{ qWarning("Could not load translation to %s!", qPrintable(locale.name())); } }
运行时更新翻译
切换语言时,先前的翻译会被移除 (QCoreApplication::removeTranslator()) 并加载 (QTranslator::load()) 和安装 (QCoreApplication::installTranslator()) 所选语言的新翻译。QQmlEngine::retranslate() 会触发 QML 文档中字符串的重新翻译。
更新日期和时间格式
更改语言也会影响日期和时间的显示方式。例如,如果QLocale 上的本地语言 "en_US "表示 "en",而 "de_DE "表示 "de":
- 应用程序初始加载时的国家代码为 "US"。
- 由于美国的语言习惯,日期以MM/DD/YYYY格式显示,时间使用 12 小时时钟。
- 将语言更改为德语后,"de_DE "是德语的第一个首选区域设置,因此国家更改为德国。
- 德国的惯例将日期以DD.MM.YYYY格式显示,并使用 24 小时时钟。
切换语言时,应用程序会使用QLocale::setDefault() 根据所选语言更新其QLocale 。在表示时间时,函数Date.toLocaleTimeString() 会根据应用程序的地域对时间和日期进行本地化(参见Main.qml)。
Main.qml
主 QML 文件定义了应用程序的用户界面。用户界面显示时间、日期和秒计数器。有关基本翻译(窗口标题)和复数处理(秒数),请参阅本地化时钟示例。
从Menu
TranslatorManager.switchLanguage()
中选择一个项目时,会调用相应的语言代码。该调用还负责重新翻译可翻译文本(请参阅翻译管理器)。不过,当语言发生变化时,我们仍需调用updateClock()
,以新的本地化格式更新时间表示。
ListModel { id: languageModel ListElement { name: "English"; code: "en" } ListElement { name: "Deutsch"; code: "de" } ListElement { name: "العربية"; code: "ar" } ListElement { name: "한국어"; code: "ko" } ListElement { name: "中文"; code: "zh" } ListElement { name: "日本語"; code: "ja" } ListElement { name: "Français"; code: "fr" } ListElement { name: "Italiano"; code: "it" } ListElement { name: "Español"; code: "es" } ListElement { name: "Português"; code: "pt" } } Menu { id: languageMenu width: languageButton.width Repeater { model: languageModel delegate: MenuItem { required property string name required property string code text: name onTriggered: { TranslatorManager.switchLanguage(code) root.selectedLanguage = name root.updateClock() } } } }
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.