How to internationalize a python script? (i18n)

gialloporpora gialloporpora at
Sun Jul 14 13:28:15 CEST 2013

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:

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'


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

As reported in this page:
«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/, 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

here are all my files:

This is the code of the 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?


More information about the Python-list mailing list