开发者

Python Internationalization (gettext)

开发者 https://www.devze.com 2023-03-20 18:33 出处:网络
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 stri

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 com开发者_C百科e 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:

  1. Translated text is usually longer than original so you need a way to control text wrap (line breaks).
  2. 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).


I only use gettext functions through Django so I don't know if everything in your code is ok gettext-API-wise, but the method itself looks ok to me. Personally I often use such placeholders in nontranslated strings and fill them after getting the translated versions from gettext.

0

精彩评论

暂无评论...
验证码 换一张
取 消