[Tutor] Simple way for i18n ?

francois schnell francois.schnell at gmail.com
Thu Mar 23 23:58:58 CET 2006


Thanks both for your help,

I managed to find one book which talk about i18n for Python: "Python in a
Nutshell" (few pages for i18n)

I've used sys.prefix on my ubuntu box to find the default directory and it
works fine this way:
http://www.flickr.com/photos/frenchy/116742621/

I've also done like you said:
gettext.install(domain, localedir)
and now it works fine also with a locale directory   :)

Then what I've got now is:
- I log on the french version of Ubuntu I've got the french app  version
- I log on the UK version of Ubuntu I've got the original app version in
English

Now I'd like to be able to change language without loging out, change
language, log in.

Martelli says in his book that to set the default language for the app I
just need to do:

>>> os.environ.setdefault('LANG', 'fr_FR')

and Python doesn't complain (but doesn't work) but if I then do:

>>> print locale.getdefaultlocale()

Python says : ('en_GB', 'utf-8')  # meaning that really couldn't work ?

How can I have my app in French even if I'm still in the GB version of
Ubuntu (change the language for the app) ?

I've also tried the "translation" way instead of the "install" way:

if I do:
gettext.install("myapp", localedir) #it translates in French when I'm in the
French Ubuntu
but if I do instead: gettext.translation("myapp", localedir,
languages="fr_FR") #with the same localedir which worked before
=>
Python complains:
"    gettext.translation("myapp", localedir, languages="fr_FR")
  File "/usr/lib/python2.4/gettext.py", line 480, in translation
    raise IOError(ENOENT, 'No translation file found for domain', domain)
IOError: [Errno 2] No translation file found for domain: 'myapp' "

I find it strange that "install" finds it but not "translation" (for the
same localedir) ?

Thanks in advance If you can help.

francois










On 23/03/06, Michael Lange <klappnase at freenet.de> wrote:
>
> On Wed, 22 Mar 2006 17:41:14 +0100
> "francois schnell" <francois.schnell at gmail.com> wrote:
>
> > Hello all,
> >
> > I wish to translate a Python script from English to French. I've read
> the
> > offical documentation (python.org doc) but I must admit that I'm lost
> now
> > ...
> > I've found some simple explanations here but I can't make it work:
> > http://karrigell.sourceforge.net/en/internationalization.htm
> >
> > Here's where I'm stuck:
> >
> > Let's imagine my app is: myapp.py
> > --
> > import gettext
> > _ = gettext.gettext
> >
> > print _("hello friends")
> > print _("Bye Bye")
> > ---
> >
> > Here are my folders on a windows box:
> >
> > C:\myappfolder\
> > -----------------------\Translations\francais\LC_MESSAGES
> >
> > My myapp.py is in myappfolder
> >
> > In this folder I've used pygettext.py to produce a messages.pot file =>
> I
> > add the translation in it => I have a messages.po file.
> > I then used msgfmt.py to produce messages.mo file.
> >
> > I then copied messages.po and messages.mo in LC_MESSAGES folder
> > C:\myappfolder\
> > -----------------------\Translations\francais\LC_MESSAGES
> >
> > I now come back to myapp.py and add two lines:
> >
> > ---
> > import gettext
> > _ = gettext.gettext
> >
> > t=gettext.translation
> ("messages","c:\myappfolder\Translations","francais")
> > t.install()
> >
> > print _("hello friends")
> > print _("Bye Bye")
> > ---
> >
> > When I do that Python anwers:
> >
> > >>>
> > Traceback (most recent call last):
> >   File "C:\myappfolder\myapp.py", line 4, in ?
> >     t=gettext.translation
> > ("messages","c:\myappfolder\Translations","francais")
> >   File "C:\Python24\lib\gettext.py", line 456, in translation
> >     raise IOError(ENOENT, 'No translation file found for domain',
> domain)
> > IOError: [Errno 2] No translation file found for domain: 'messages'
> > >>>
> >
> Hi Francois,
>
> not sure if it is different on windows, on linux I simply do:
>
>   import gettext
>   gettext.install(domain, localedir)
>
> to install _() into my application's global namespace,
> where localedir in your case was "c:\myappfolder\Translations".
> The path that contains the french translation should be
> "..\fr\LC_MESSAGES" instead of "..\francais\LC_MESSAGES"
> I think (at least that is true on linux).
>
> I hope this helps
>
> Michael
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060323/34d06b6e/attachment.htm 


More information about the Tutor mailing list