PyQt:翻译标准按钮
如何轻松地从 QMessageBox 翻译标准按钮(是、否)?我无法在这些参数上使用 self.tr,所以我想以其他简单的方式来实现它。我必须使用整个翻译系统吗?
How can I easily translate standard buttons (Yes, No) from QMessageBox? I can't use self.tr on those arguments, so I would like to achieve it in some other simple way. Do I have to use whole translation system?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我是这样做的:
首先,您需要将
qt_LOCALE.qm
文件复制到您的应用程序目录。我的是:其次,您需要为您的应用程序加载翻译器。
Here is how I did :
First you need to copy the
qt_LOCALE.qm
file to your application directory. Mine was :Secondly, you need to load a translator for your application.
最快的方法是这样的:
请参阅 QMessageBox 文档以了解所有可能的标准按钮。
The quickest way is something like this:
See the QMessageBox documentation for all possible standard buttons.
您确定必须自己翻译吗?在其他工具包中,标准按钮会自动翻译为用户正在使用的语言。
Are you sure you have to translate them yourself? In other toolkits, standard buttons are automatically translated to the language that the user is using.
如果您想将“是/否”翻译成当前语言,通常设置系统语言就足够了。否则请尝试 Natim 的建议。
如果您想要自定义文本:像 Question() 等静态方法不允许自定义按钮文本。不幸的是,因为“是”/“否”对话框被认为是不好的风格(它们不是描述性的,人们必须阅读整条消息,并且很容易误解什么意味着“是”和什么意味着“否”并意外地否定它们)。对于自定义文本,您必须直接使用 QMessageBox。我建议编写您自己的静态方法来接受可选的按钮文本。
If you want to have Yes/No translated into the current language, usually setting the system language should be enough. Otherwise try with Natim's suggestion.
If you want custom texts: The static methods like question() etc. don't allow custom button texts. That's unfortunate, as "Yes"/"No" dialogs are considered bad style (they are not descriptive, one has to read the whole message, and its easy to misunderstand what means Yes and what means No and accidentally negating them). For custom texts, you have to use QMessageBox directly. I'd suggest to write your own static methods accepting optional button texts.