Skip to content Skip to sidebar Skip to footer

Python Internationalization (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 c

Solution 1:

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

Solution 2:

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.


Post a Comment for "Python Internationalization (gettext)"