QMessageBox::information(nullptr, tr("tips"), tr("Are yue sure?"), QMessageBox::Yes);
如上代码,使用了标准按钮 QMessageBox::Yes
。因此需要将 qt 翻译加载到程序中
加载 Qt 翻译
QLibraryInfo
可以获取 Qt 指定的翻译目录,QLibrary
支持目录包括如下:onstant | Value | Description |
---|---|---|
LibraryInfo::PrefixPath | 0 | The default prefix for all paths. |
ibraryInfo::DocumentationPath | 1 | The location for documentation upon install. |
ibraryInfo::HeadersPath | 2 | The location for all headers. |
ibraryInfo::LibrariesPath | 3 | The location of installed libraries. |
ibraryInfo::LibraryExecutablesPath | 4 | The location of installed executables required by libraries at runtime. |
ibraryInfo::BinariesPath | 5 | The location of installed Qt binaries (tools and applications). |
ibraryInfo::PluginsPath | 6 | The location of installed Qt plugins. |
ibraryInfo::ImportsPath | 7 | The location of installed QML extensions to import (QML 1.x). |
ibraryInfo::Qml2ImportsPath | 8 | The location of installed QML extensions to import (QML 2.x). |
ibraryInfo::ArchDataPath | 9 | The location of general architecture-dependent Qt data. |
ibraryInfo::DataPath | 10 | The location of general architecture-independent Qt data. |
ibraryInfo::TranslationsPath | 11 | The location of translation information for Qt strings. |
ibraryInfo::ExamplesPath | 12 | The location for examples upon install. |
ibraryInfo::TestsPath | 13 | The location of installed Qt testcases. |
ibraryInfo::SettingsPath | 100 | The location for Qt settings. Not applicable on Windows. |
通过 QLibraryInfo::location(QLibraryInfo::TranslationsPath)
可以获取到指定的翻译目录。
QLibraryInfo
支持的目录可通过 qt.conf
修改,支持值如下:Entry | Default Value |
---|---|
Prefix | QCoreApplication::applicationDirPath() |
Documentation | doc |
Headers | include |
Libraries | lib |
LibraryExecutables | libexec |
Binaries | bin |
Plugins | plugins |
Imports | imports |
Qml2Imports | qml |
ArchData | . |
Data | . |
Translations | translations |
Examples | examples |
Tests | tests |
Settings | . |
qt.conf
默认使用资源系统中的 :/qt/etc/qt.conf
,没有就依次加载当前目录 和 qt目录
[Paths]
Documentation=../../Docs/Qt-5.11.2
Examples=../../Examples/Qt-5.11.2
Prefix=..
当 Qt 翻译都加载正确后, QMessage button依然没有被翻译
Qt源码:
QString QPlatformTheme::defaultStandardButtonText(int button)
{
switch (button) {
case QPlatformDialogHelper::Ok:
return QCoreApplication::translate("QPlatformTheme", "OK");
case QPlatformDialogHelper::Save:
return QCoreApplication::translate("QPlatformTheme", "Save");
case QPlatformDialogHelper::SaveAll:
return QCoreApplication::translate("QPlatformTheme", "Save All");
case QPlatformDialogHelper::Open:
return QCoreApplication::translate("QPlatformTheme", "Open");
case QPlatformDialogHelper::Yes:
return QCoreApplication::translate("QPlatformTheme", "&Yes");
case QPlatformDialogHelper::YesToAll:
return QCoreApplication::translate("QPlatformTheme", "Yes to &All");
case QPlatformDialogHelper::No:
return QCoreApplication::translate("QPlatformTheme", "&No");
case QPlatformDialogHelper::NoToAll:
return QCoreApplication::translate("QPlatformTheme", "N&o to All");
case QPlatformDialogHelper::Abort:
return QCoreApplication::translate("QPlatformTheme", "Abort");
case QPlatformDialogHelper::Retry:
return QCoreApplication::translate("QPlatformTheme", "Retry");
case QPlatformDialogHelper::Ignore:
return QCoreApplication::translate("QPlatformTheme", "Ignore");
case QPlatformDialogHelper::Close:
return QCoreApplication::translate("QPlatformTheme", "Close");
case QPlatformDialogHelper::Cancel:
return QCoreApplication::translate("QPlatformTheme", "Cancel");
case QPlatformDialogHelper::Discard:
return QCoreApplication::translate("QPlatformTheme", "Discard");
case QPlatformDialogHelper::Help:
return QCoreApplication::translate("QPlatformTheme", "Help");
case QPlatformDialogHelper::Apply:
return QCoreApplication::translate("QPlatformTheme", "Apply");
case QPlatformDialogHelper::Reset:
return QCoreApplication::translate("QPlatformTheme", "Reset");
case QPlatformDialogHelper::RestoreDefaults:
return QCoreApplication::translate("QPlatformTheme", "Restore Defaults");
default:
break;
}
return QString();
}
我们发现 button 的文本上下文是 QPlatformTheme
, 而不是 qt_zh_CN.qm 中 QDialogButtonBox