【发布时间】:2011-10-07 07:36:00
【问题描述】:
我正在尝试使用 Python 进行国际化。我从一个简单的“Hello, World”脚本开始,现在我掌握了使用 gettext 的基础知识。我在文档中读到将字符串连接在一起,如下所示:
msg = _('Start of message')
msg += _(' end of message')
不推荐,因为它会拆分要翻译的字符串并可能导致错误(这就是我从文档中了解到的)。
我想知道动态生成字符串的最佳做法是什么 - 例如,99 瓶啤酒。众所周知,写 99 瓶啤酒的程序是很可爱的,但是你会嵌套这样的程序,让它可以国际化吗?通过阅读文档,我想出了以下内容。它相当冗长 - 我想知道您是否有任何关于如何改进代码的 cmets(关于国际化,而不是生成歌曲!)
#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 internationalization