Python国际化(gettext) [英] Python Internationalization (gettext)

查看:197
本文介绍了Python国际化(gettext)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python进行国际化.我从一个简单的"Hello,World"脚本开始,现在我掌握了使用gettext的基本知识.我在文档中读到了将字符串串联在一起的方法,

I'm experimenting with internationalization in Python. I started with a simple "Hello, World" script and I now grasp the basics of using gettext. I read in the documentation that concatenating strings together like so:

msg = _('Start of message')
msg += _(' end of message')

不建议使用

,因为它会分割要翻译的字符串,并可能导致错误(无论如何,这是我从文档中了解的内容).

is not recommended as it splits the strings to be translated and can lead to errors (that's what I understood from the documentation anyway).

我想知道动态生成的字符串的最佳做法是什么,例如99瓶啤酒.众所周知,编写99瓶啤酒程序会很可爱,但是您是否嵌套了这样一个程序,使其可以国际化?通过阅读文档,我得出以下结论.这是相当冗长的-我想知道您是否对如何改进代码有任何评论(关于国际化,而不是产生歌曲!)

I wondered what the best practice was for dynamically generated strings - for example, 99 bottles of beer. As you all know you can get quite cute writing 99 bottles of beer programs, but do you nest structure such a program so it can be internationalized? From reading the docs I've come up with the following. It's reasonably verbose - I wondered if you have any comments on how to improve the code (with respect to internationalization, not generating the song!)

#self._ is gettext.ugettext
#self._s is gettext.ungettext
def bottles(self):
    for bottles in xrange(99, 1, -1):
        lyric =self._s('%(bottles)d bottle of beer on the wall, %(bottles)d bottles of beer.', '%(bottles)d bottles of beer on the wall, %(bottles)d bottles of beer.', bottles)
        lyric += "\n"
        lyric += self._s('Take one down and pass it around, no more bottles of beer on the wall.', 'Take one down and pass it around, %(bottles-1)d bottles of beer on the wall.', bottles - 1)
        lyric += '\n'
        print lyric % {'bottles' : bottles, 'bottles-1' : bottles -1}
    print self._('1 bottle of beer on the wall, 1 bottle of beer.')
    print self._('Take one down and pass it around, no more bottles of beer on the wall.\n')
    print self._('No more bottles of beer on the wall, no more bottles of beer.')
    print self._('Go to the store and buy some more, 99 bottles of beer on the wall.')

推荐答案

说实话,我对Python知之甚少,我只将Gettext与C ++一起使用.

To be honest I know very little about Python and I only used Gettext with C++.

似乎您在使用占位符和字符串格式来对外部字符串进行格式化.这绝对是好的.如果操作正确(某种程度上,稍后会再介绍),那么翻译人员可以带来多种复数形式-根据数量,瓶子在某些语言(包括波兰语)中会有不同的翻译-1 butelka ,2 butelki,5 butelek ...).

It seems that you are using placeholders and string formatting for externalized string. This is definitely good. If you did it correctly (as it seems to some extent, more on this later), the translators would be able to bring more than one plural form - depending on quantity, bottle would have different translation in some languages (including Polish - 1 butelka, 2 butelki, 5 butelek...).

现在,为什么我认为您可以做得更好.好吧,问题在于您正在串联字符串.在这种情况下,不要紧,而是在真实的句子中,即使对于很长的文本,也最好不要像您那样将其拆分,并且希望在句子中嵌入新的换行标记.这是由于两个原因:

Now, why I think you could have done a better job. Well, the problem is you are concatenating the string. In this case it should not matter but in real sentences, even for pretty long text it is better not to split it like you did, and it is desired to have new line marks embedded into sentence. That is because of two reasons:

  1. 翻译后的文本通常比原始文本长,因此您需要一种控制文本换行(换行符)的方法.
  2. 在翻译文本时通常需要对句子进行重新排序,以使其听起来更好(或者仅仅是因为目标语言的语法与英语完全不同而必须这样做).

这篇关于Python国际化(gettext)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆