python gettext_Python国际化(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).

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.')

解决方案

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

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:

Translated text is usually longer than original so you need a way to control text wrap (line breaks).

It is often required to reorder the sentence when translating the text so it sounds better (or it is simply required because target language's grammar is totally different than English).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值