问题
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!
回答1:
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:
- 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
- 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
来源:https://stackoverflow.com/questions/9828629/python-and-gettext