使 wxPython 应用程序成为多语言的

发布于 2024-07-25 21:26:18 字数 1177 浏览 4 评论 0原文

我有一个用 wxPython 编写的应用程序,我想将其设置为多语言。 我们的选择是

  1. 使用 gettext http://docs.python.org/library/gettext.html
  2. 将所有 UI 文本分离出来 messages.py 文件,并使用它来 ,

我非常倾向于第二种,我认为采用 gettext 方式没有任何好处 使用第二种方式,我可以将所有消息放在一个地方,而不是在代码中,所以如果我需要更改消息,则不需要更改代码,如果使用 gettext,我可能会混淆消息常量,因为我将只包装原始消息msg 而不是将其转换为 messages.py 中的常量

基本上而不是

wx.MessageBox(_("Hi stackoverflow!"))

我认为

wx.MessageBox(messages.GREET_SO)

更好,那么 gettext 方式有什么优点而第二种方式有缺点吗? 还有第三种方法吗?

编辑: 另外, gettext 语言文件似乎与代码联系太紧密,如果我想要两条英语相同但法语不同的消息会发生什么,例如假设法语对于英语的不同场景有更微妙的翻译,这是一种不错的

体验: 我已经采用了第二种方法,我必须说每个应用程序都应该尝试从代码中提取 UI 文本,它提供了重构的机会,查看 UI 正在渗透到模型中的位置以及 UI 文本可以改进的地方,相比之下 gettext 是机械式的,不为编码器提供任何输入,我认为维护起来会更困难。

在为文本创建名称(例如 PRINT_PROGRESS_MSG)时,有机会看到在许多地方,相同的消息的使用方式略有不同,并且可以合并为单个名称,稍后当我只需要更改一次消息时,这将有所帮助。

结论:我仍然不确定使用 gettext 有什么优势,并且正在使用我自己的消息文件。 但我选择的答案至少解释了为什么 gettext 是有益的。 IMO 的最终解决方案是从两种方式中获取最佳解决方案,即我自己的消息标识符由 gettext 包装,例如

wx.MessageBox(_("GREET_SO"))

I have a application written in wxPython which I want to make multilingual.
Our options are

  1. using gettext http://docs.python.org/library/gettext.html
  2. seprating out all UI text to
    a messages.py file, and using it to
    translate text

I am very much inclined towards 2nd and I see no benefit in going gettext way,
using 2nd way i can have all my messages at one place not in code, so If i need to change a message, code need not be changed, in case of gettext i may have confusing msg-constants as I will be just wrapping the orginal msg instead of converting it to a constant in messages.py

basically instead of

wx.MessageBox(_("Hi stackoverflow!"))

I think

wx.MessageBox(messages.GREET_SO)

is better, so is there any advantage in gettext way and disadvantage 2nd way? and is there a 3rd way?

edit:
also gettext languages files seems to be too tied to code, and what happens if i want two messages same in english but different in french e.g. suppose french has more subtle translation for different scnerarios for english one is ok

experience:
I have already gone 2nd way, and i must say every application should try to extract UI text from code, it gives a chance to refactor, see where UI is creeping into model and where UI text can be improved, gettext in comparison is mechanic, doesn't gives any input for coder, and i think would be more difficult to maintain.

and while creating a name for text e.g. PRINT_PROGRESS_MSG, gives a chance to see that at many places, same msg is being used slightly differently and can be merged into a single name, which later on will help when i need to change msg only once.

Conclusion: I am still not sure of any advantage to use gettext and am using my own messages file. but I have selected the answer which at least explained few points why gettext can be beneficial.
The final solution IMO is which takes the best from both ways i.e my own message identifier wrapped by gettext e.g

wx.MessageBox(_("GREET_SO"))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瑾兮 2024-08-01 21:26:18

Gettext 是可行的方法,在您的示例中,您还可以使用 gettext 来“避免在代码中存储翻译”:

wx.MessageBox(messages.GREET_SO)

可能与 gettext 相同:

wx.MessageBox(_("GREET_SO")) or wx.MessageBox(_("messages.GREET_SO"))

Gettext 几乎是多语言应用程序的标准,我很确定您将来会从使用它中受益。 例如,您可以使用 Poedit(或其他类似应用程序)将翻译分配给您的协作者或贡献者,然后将一条或多条消息标记为未正确翻译。 另外,如果缺少/额外的条目,poedit 会警告您。 不要欺骗自己,gettext 是唯一经过验证的可靠的维护翻译的方法。

Gettext is the way to go, in your example you can also use gettext to "avoid storing the translations in the code":

wx.MessageBox(messages.GREET_SO)

might be the same with gettext as:

wx.MessageBox(_("GREET_SO")) or wx.MessageBox(_("messages.GREET_SO"))

Gettext is pretty much the standard for multilingual applications, and I'm pretty sure you'll benefit from using it in the future. Example, you can use Poedit (or other similar app) to assign translations to your collaborators or contributors and later on flag one or several messages as not properly translated. Also if there are missing / extra entries poedit will warn you. Don't fool yourself, gettext is the only proven reliable way to maintain translations.

暖心男生 2024-08-01 21:26:18

gettext 有一些优点:

  1. 最大的优点之一是:使用 poedit 进行翻译时,您可以从翻译数据库中受益。 基本上,poedit 可以扫描您的硬盘并查找已翻译的文件,并在您翻译文件时提出建议。
  2. 当你把代码交给其他人翻译时,他们可能已经知道 gettext 的翻译方式,而你必须向他们解释你的翻译方式。
  3. 您在代码的上下文中拥有文本,因此当您看到翻译周围的代码时,翻译应该更容易
  4. 考虑如下文本: print _('%d files of %d files selected') % ( num, numTotal) 甚至更复杂的情况。 在这里,拥有代码真的很有帮助......

There are some advantages of gettext:

  1. One of the biggest advantages is: when using poedit to do the translations you can benefit from the translation database. Basically poedit ca scan your harddisk and find already translated files and will make suggestions when you translate your file.
  2. When you give the code to other people to translate they might already know the gettext way of translating, while you have to explain them your way of translating.
  3. You have the text in the context of the code, so it should be easier to translate, when you see the code around the translation
  4. Consider text like: print _('%d files of %d files selected') % (num, numTotal) and even more complicated situations. Here it really helps having the code around ...
偏爱你一生 2024-08-01 21:26:18

对于网络(这是 PHP,但想法是相同的),我总是在特定目录中创建多个语言文件。 en.php、fr.php 等等。 这些文件包含给定语言的所有输出文本的定义。 用户对语言的偏好决定了包含哪些文件,从而决定了输出以哪种语言显示。例如...

在 en.php 中:
TEXT_I_AM =“我是”

fr.php 中的 :
TEXT_I_AM = "我是你"

For the web (this is PHP but the idea's the same), I always create multiple language files in a specific directory. en.php, fr.php, et cetera. Those files contain definitions of all output text, in the given language. The user preference for language determines which of those files get included, thus, which language the output appears in. For example...

in en.php:
TEXT_I_AM = "I am"

in fr.php:
TEXT_I_AM = "Je suis"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文