好吧,这可能有点棘手。
首先,Qt 小部件的本地化字符串可能在 Qt 的 .qm 文件中。请记住,一般情况下,您将需要所有 Qt 的 .qm 文件来翻译您的语言环境。这是因为 Qt 可能会在可用的平台上使用原生 MessageDialogs,在这种情况下,相关代码将位于 qtbase 中,因此您不仅需要 qtquickcontrols_XX.qm 文件,还需要 qtbase_XX.qm 文件用于选定的语言环境. 等等。
但是,在某些情况下,上述方法可能无济于事。您的语言环境的 Qt 本地化文件中可能只是缺少必需的字符串(这很常见),因此您需要自己为这些字符串制作 .ts 和 .qm 文件。在这种情况下,您将需要研究 Qt 源代码以了解您感兴趣的那些小部件的内部结构。
请注意,以下示例取决于可能在未来 Qt 版本中更改的实现细节。
例如,用于 Android 和 iOS 的 QML MessageDialog 的 Qt 实现使用QPlatformTheme,可以进一步子类化以与不同的平台一起使用。在 QPlatformTheme 中,按钮标签的文本由 defaultStandardButtonText() 方法返回,如下所示:
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();
}
因此,为了本地化是/否按钮,您需要在 .ts 文件中添加以下内容:
<context>
<name>QPlatformTheme</name>
<message>
<source>&Yes</source>
<translation>[...]</translation>
</message>
<message>
<source>&No</source>
<translation>[...]</translation>
</message>
</context>
Android 有自己的子类平台主题 ( QAndroidPlatformTheme ) 并覆盖了 defaultStandardButtonText(),因此对于 Android,您需要添加以下内容:
<context>
<name>QAndroidPlatformTheme</name>
<message>
<source>Yes</source>
<translation>[...]</translation>
</message>
<message>
<source>No</source>
<translation>[...]</translation>
</message>
</context>
iOS 不会在其自己的 QPlatformTheme 子类中覆盖此方法,因此不需要额外的操作。
您可以查看我的一个项目,该项目使用上述技术在 Android 和 iOS 上进行 QML MessageBox 本地化。