Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python and gettext

I'm building a Python application that utilizes a bunch of translated strings. The directory structure housing said strings looks like this:

/locales
    default.pot # reference English strings live here
    /es_ES
        /LC_MESSAGES
            default.po #Spanish strings
    /de_DE
        /LC_MESSAGES
            default.po #German strings

These default.po files were generated by a PHP application, but as far as I know, they conform to the general standard needed to work with gettext implementations.

When I attempt to utilize these strings in Python using gettext, the following goes down (this example was run from within the locales directory:

>>> import os; os.listdir('.')
['.svn', 'de_DE', 'default.pot', 'eng', 'es_ES', 'fr_FR', 'ja_JP', 'ko_KR', 'pl_PL', 'pt_BR', 'ru_RU']
>>> import os.path
>>> os.path.exists('./es_ES/LC_MESSAGES/default.po')
True
>>> import gettext
>>> ldir = 'es_ES/LC_MESSAGES/'
>>> t = gettext.translation('default',ldir)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/gettext.py", line 469, in translation
IOError: [Errno 2] No translation file found for domain: 'default'
>>>

I'm not sure what I'm doing wrong here (beyond inexperience with this library and the notion of 'domain' in its context).

Am I making a simple mistake? Or do I have a fundamental flaw in my understanding of how this crap works?

Thanks!

like image 756
brettkelly Avatar asked Mar 22 '12 18:03

brettkelly


People also ask

What is gettext used for?

In computing, gettext is an internationalization and localization (i18n and l10n) system commonly used for writing multilingual programs on Unix-like computer operating systems. One of the main benefits of gettext is that it separates programming from translating.

How do I use i18n in Python?

Pluralization With Python-i18n Package The python-i18n is a library that provides similar functionality as the Rails i18n library. Use the add_translation() function to store a translation, and use the t() function to translate it. YAML and JSON files can be used to permanently store these translations.


1 Answers

I'm very rusty on this, but based on past experience and http://docs.python.org/library/gettext, I can see two main things missing here:

  1. ldir should be set to the base directory containing locale data (/usr/share/locale is the classic system location). gettext will search for ldir/lang_COUNTRY/catalog.mo
  2. The .po files can't be read on their own, you need to convert them to .mo binary files. Typically you'd use msgfmt to do this ("brew install gettext" on the mac is the easiest way to get this).

A quick example:

$ find /tmp/locales -type f
/tmp/locales/de_DE/LC_MESSAGES/default.mo
/tmp/locales/de_DE/LC_MESSAGES/default.po
/tmp/locales/default.pot
/tmp/locales/en_IE/LC_MESSAGES/default.mo
/tmp/locales/en_IE/LC_MESSAGES/default.po

$ ~/Library/homebrew/Cellar/gettext/0.18.1.1/bin/msgfmt \
-o locales/en_IE/LC_MESSAGES/default.mo \
locales/en_IE/LC_MESSAGES/default.po

$ cat /tmp/app.py 
import gettext
t = gettext.translation('default', "/tmp/locales")
_ = t.ugettext

print _("Hello World")

$ locale
LANG="en_IE.UTF-8"
LC_COLLATE="en_IE.UTF-8"
LC_CTYPE="en_IE.UTF-8"
LC_MESSAGES="en_IE.UTF-8"
LC_MONETARY="en_IE.UTF-8"
LC_NUMERIC="en_IE.UTF-8"
LC_TIME="en_IE.UTF-8"
LC_ALL=

$ python app.py 
How's the craic?

$ LC_MESSAGES=de_DE python app.py
Guten Tag
like image 171
Michael Twomey Avatar answered Nov 01 '22 08:11

Michael Twomey