How to internationalize a python script? (i18n)

gialloporpora gialloporpora at gmail.com
Sun Jul 14 13:28:15 CEST 2013


Hello,
I am trying to internationalize a script. First I have tried with a 
little script to understand how it works, but unfortunately, it doesn't.

I have followed instruction in this page:
http://docs.python.org/2/library/i18n.html

I have created my script, marked strings with the _() function, 
installed it in the main namespace, but when I try to load the locale 
file it said methat locale is unavailable:

IOError: [Errno 2] No translation file found for domain: 'helloi18n'

C:\dropbox\Public\helloi18n>

I have created the pot file with pygettext, localized it with poedit and 
compiled the related .mo file.

As reported in this page:
http://docs.python.org/2/library/gettext.html
«Bind the domain to the locale directory localedir. More concretely, 
gettext will look for binary .mo files for the given domain using the 
path (on Unix): localedir/language/LC_MESSAGES/domain.mo, where 
languages is searched for in the environment variables LANGUAGE, LC_ALL, 
LC_MESSAGES, and LANG respectively.»

I have put my .mo file in locale\it\LC_MESSAGES naming it helloi18n.mo

here are all my files:
https://dl.dropboxusercontent.com/u/4400966/helloi18n.tar.gz

This is the code of the helloi18n.py file:

> # Simple script to use internationalization (i18n)
> import gettext
> import os
>
>
> LOCALE_DIR = os.path.join(os.path.abspath('.'), 'locale')
> print LOCALE_DIR
> print "---"
> a=gettext.find('helloi18n', LOCALE_DIR, 'it')
> print a
> gettext.install('helloi18n', localedir=LOCALE_DIR, unicode=1)
> gettext.textdomain ('helloi18n')
> gettext.translation('helloi18n', LOCALE_DIR, 'it')
>
>
> if __name__ == '__main__':
> 	print _('Hello world!')
> 	print (_('My first localized python script'))



Somebody could help me?


Sandro





More information about the Python-list mailing list