469,645 Members | 1,653 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

gettext again

Hi,
I'm failing to make it work but can't find out what's wrong. Here's what
I do :

================
test.py

import gettext
gettext.install('')

msg = _("Message without accented characters")

print msg
================

Then I do :

xgettext test.py

mv message.po message pot

msginit --> output message.fr_FR@euro

I edit my fr_FR@euro with this text :
msgid "This is a message without accented characters."
msgstr "Message avec caractères accentués : éèàùëêôîaâç"

Then msgfmt fr_FR@euro

Then python test.py shows "Message without accented characters"

How come ? What's wrong with what I am doing ?

Thanks for your help.
Aug 7 '05 #1
13 3668
On Sun, 07 Aug 2005 11:09:14 +0200, cantabile wrote:
Hi,
I'm failing to make it work but can't find out what's wrong. Here's what
I do : [....] How come ? What's wrong with what I am doing ?

Start with this little howto about gettext.
http://childsplay.sourceforge.net/translate-howto.html

And then do this in your test.py:
http://www.python.org/doc/2.4.1/lib/node330.html

You should read the part about gettext in the Python Library Reference
it's really good :-)

Good luck,
Stas Z
Aug 7 '05 #2
stasz a écrit :
On Sun, 07 Aug 2005 11:09:14 +0200, cantabile wrote:

Hi,
I'm failing to make it work but can't find out what's wrong. Here's what
I do :


[....]
How come ? What's wrong with what I am doing ?


Start with this little howto about gettext.
http://childsplay.sourceforge.net/translate-howto.html

And then do this in your test.py:
http://www.python.org/doc/2.4.1/lib/node330.html

You should read the part about gettext in the Python Library Reference
it's really good :-)

Good luck,
Stas Z


Well, I must be dumb, because I did exactly that and it still doesn't
work...

BTW, I have no pygettext module. I asked here and somebody said it was
deprecated and now included in xgettext.
And I've read the Python doc about gettext about ten times, but it seems
quite outdated since it calls pygettext (as staded above)...

I've read the info pages of gettext too (quite a long work).

Here's my test1.py file again :

===========================
import gettext, os, locale

locale.setlocale(locale.LC_ALL)
gettext.install('test1.py', '/usr/share/locale')

msg = _("This is a message without accented characters.")

print msg
===========================

What can I do now ?
Aug 7 '05 #3
On Sun, 07 Aug 2005 21:33:21 +0200, cantabile wrote:
stasz a écrit :
On Sun, 07 Aug 2005 11:09:14 +0200, cantabile wrote:

Hi,
I'm failing to make it work but can't find out what's wrong. Here's what
I do :
[....]
How come ? What's wrong with what I am doing ?


Start with this little howto about gettext.
http://childsplay.sourceforge.net/translate-howto.html

And then do this in your test.py:
http://www.python.org/doc/2.4.1/lib/node330.html

You should read the part about gettext in the Python Library Reference
it's really good :-)

Good luck,
Stas Z


Well, I must be dumb, because I did exactly that and it still doesn't
work...

BTW, I have no pygettext module. I asked here and somebody said it was
deprecated and now included in xgettext.
And I've read the Python doc about gettext about ten times, but it seems
quite outdated since it calls pygettext (as staded above)...

I've read the info pages of gettext too (quite a long work).

Ok, you should use xgettext to create a .po file from your test1.py.
Translate that file and use msgfmt to compile it into a .mo file.
Copy this .mo file to /usr/share/locale/fr/LC_MESSAGES/
Here's my test1.py file again :

===========================
import gettext, os, locale

locale.setlocale(locale.LC_ALL)
gettext.install('test1.py', '/usr/share/locale')

test1.py is wrong, you must give the name of the .mo file.
It must be: gettext.install('test1.mo', '/usr/share/locale')
Assuming you have called the mo file like that.

Stas Z
Aug 7 '05 #4
stasz a écrit :
On Sun, 07 Aug 2005 21:33:21 +0200, cantabile wrote:

stasz a écrit :
On Sun, 07 Aug 2005 11:09:14 +0200, cantabile wrote:

Hi,
I'm failing to make it work but can't find out what's wrong. Here's what
I do :

[....]
How come ? What's wrong with what I am doing ?

Start with this little howto about gettext.
http://childsplay.sourceforge.net/translate-howto.html

And then do this in your test.py:
http://www.python.org/doc/2.4.1/lib/node330.html

You should read the part about gettext in the Python Library Reference
it's really good :-)

Good luck,
Stas Z


Well, I must be dumb, because I did exactly that and it still doesn't
work...

BTW, I have no pygettext module. I asked here and somebody said it was
deprecated and now included in xgettext.
And I've read the Python doc about gettext about ten times, but it seems
quite outdated since it calls pygettext (as staded above)...

I've read the info pages of gettext too (quite a long work).


Ok, you should use xgettext to create a .po file from your test1.py.
Translate that file and use msgfmt to compile it into a .mo file.
Copy this .mo file to /usr/share/locale/fr/LC_MESSAGES/

Here's my test1.py file again :

===========================
import gettext, os, locale

locale.setlocale(locale.LC_ALL)
gettext.install('test1.py', '/usr/share/locale')


test1.py is wrong, you must give the name of the .mo file.
It must be: gettext.install('test1.mo', '/usr/share/locale')
Assuming you have called the mo file like that.

Stas Z


Well, I did this exactly... and still no go.

Is this sequence correct :
1° xgettext test1.py
2° mv messages.po messages.pot
3° msginit
4° Translate msgstr"" in fr***@euro.po file
5° msgfmt -o test1.mo fr***@euro.po
6° cp test1.mo /usr/share/locale/LC_MESSAGES
7° python test1.py
This is what I did, without success. Must be bewitched :((
Aug 8 '05 #5
On Mon, 08 Aug 2005 02:56:35 +0200, cantabile wrote:
stasz a écrit :
On Sun, 07 Aug 2005 21:33:21 +0200, cantabile wrote:

stasz a écrit :

On Sun, 07 Aug 2005 11:09:14 +0200, cantabile wrote:

>Hi,
>I'm failing to make it work but can't find out what's wrong. Here's what
>I do :

[....]
>How come ? What's wrong with what I am doing ?

Start with this little howto about gettext.
http://childsplay.sourceforge.net/translate-howto.html

And then do this in your test.py:
http://www.python.org/doc/2.4.1/lib/node330.html

You should read the part about gettext in the Python Library Reference
it's really good :-)

Good luck,
Stas Z

Well, I must be dumb, because I did exactly that and it still doesn't
work...

BTW, I have no pygettext module. I asked here and somebody said it was
deprecated and now included in xgettext.
And I've read the Python doc about gettext about ten times, but it seems
quite outdated since it calls pygettext (as staded above)...

I've read the info pages of gettext too (quite a long work).


Ok, you should use xgettext to create a .po file from your test1.py.
Translate that file and use msgfmt to compile it into a .mo file.
Copy this .mo file to /usr/share/locale/fr/LC_MESSAGES/

Here's my test1.py file again :

===========================
import gettext, os, locale

locale.setlocale(locale.LC_ALL)
gettext.install('test1.py', '/usr/share/locale')


test1.py is wrong, you must give the name of the .mo file.
It must be: gettext.install('test1.mo', '/usr/share/locale')
Assuming you have called the mo file like that.

Stas Z


Well, I did this exactly... and still no go.

Is this sequence correct :
1° xgettext test1.py
2° mv messages.po messages.pot
3° msginit
4° Translate msgstr"" in fr***@euro.po file
5° msgfmt -o test1.mo fr***@euro.po
6° cp test1.mo /usr/share/locale/LC_MESSAGES
7° python test1.py
This is what I did, without success. Must be bewitched :((

Your steps seems alright.
Just a thought; you do start test1.py from a fr_FR@euro
environment do you?
I mean in a xterm do: export LANG=fr_FR@euro
And then start test1.py from there.

Stas

Aug 8 '05 #6
stasz a écrit :
Your steps seems alright.
Just a thought; you do start test1.py from a fr_FR@euro
environment do you?
I mean in a xterm do: export LANG=fr_FR@euro
And then start test1.py from there.

Stas


Whoooot ! Working at last, after three days... It wasn't the LANG param
which is fr_FR@euro allright. I inadvertantly suppressed the '.mo' part
of gettext.install(test1.mo') and it just worked.
I guess gettext was looking for a 'test1.mo.mo' file or something.

Anyways, you made my day my friend !
Many many thanks !
Aug 8 '05 #7
BTW stasz,
Maybe you'll have still some time for the following question. Trying my
luck :))

Suppose I have several units (.py files), say test.py test1.py tets2.py
, test.py being my main file.
I've read I can import gettext and install in the main unit. Then, must
I create .po files for each unit or is there a way to make a single .po
file for the whole app. ?
Aug 8 '05 #8
On Mon, 08 Aug 2005 17:01:21 +0200, cantabile wrote:
stasz a écrit : Whoooot ! Working at last, after three days... It wasn't the LANG param [...] Anyways, you made my day my friend !
Many many thanks !

Your welcome :-)

Stas

Aug 8 '05 #9
On Mon, 08 Aug 2005 17:39:34 +0200, cantabile wrote:
BTW stasz,
Maybe you'll have still some time for the following question. Trying my
luck :))

Suppose I have several units (.py files), say test.py test1.py tets2.py
, test.py being my main file.
I've read I can import gettext and install in the main unit. Then, must
I create .po files for each unit or is there a way to make a single .po
file for the whole app. ?

Yes there is.

As a reminder, make sure that you install gettext in the namespace
of your toplevel module.
What I mean is this:

test1.py imports test2.py and test3.py
test2.py imports test4.py

Now you have to place the gettext.install call in test1.py and
then the other modules can access it.
If you place the gettext call in, for example, test2.py then only
test2.py and test4.py have access to it.
So when it don't work as you expect make sure to check if every
module has access to the "_" from gettext.

So now for your question about one po/mo file for multiple
modules.
I use this file for a project of mine it also includes some
explanation in case I forget it myself :-)
<shameless plug>
The project is called "Guido van Robot".
http://gvr.sf.net
</shameless plug>

The files mentioned inside this file are used in the above project
and serve as an example in this case.
You could replace the files with your own to try it.
The file itself is called "FilesForTrans" so you could put this
file together with your test modules inside a directory and follow
the instructions in side "FilesForTrans".

Stas Z
Contents of "FilesForTrans"
---- cut here ---------
## These files contain i18n strings
## Use: xgettext -f ./FilesForTrans -p /tmp
## The message.po will be located in /tmp
## Now do: msgmerge ./po/nl/gvr.po /tmp/messages.po > /tmp/new_nl.po
## compile the .po to mo (place the .po after editing in ./po/nl/)
## msgfmt ./po/nl/gvr.po -o ./locale/nl/LC_MESSAGES/gvr.mo

gvr.py
gvrparser.py
world.py
worldMap.py
Editorwindows.py

Aug 8 '05 #10
Ok, I'll try that.
Thanks again Stasz !
Aug 9 '05 #11
stas a écrit :
As a reminder, make sure that you install gettext in the namespace
of your toplevel module.
What I mean is this:

test1.py imports test2.py and test3.py
test2.py imports test4.py

Now you have to place the gettext.install call in test1.py and
then the other modules can access it.
If you place the gettext call in, for example, test2.py then only
test2.py and test4.py have access to it.
So when it don't work as you expect make sure to check if every
module has access to the "_" from gettext.

Noticed something :
I must import test2.py AFTER gettext.install('test1') and even then, if
test3.py imports test2.py, messages won't be translated in test3.py. I
have to import test3.py in test1.py too.
Is this normal behaviour or is there something I'm missing (again) ?

PS : your project looks nice !
Aug 9 '05 #12
On Tue, 09 Aug 2005 23:59:26 +0200, cantabile wrote:
stas a écrit :
As a reminder, make sure that you install gettext in the namespace
of your toplevel module.
[....] Noticed something :
I must import test2.py AFTER gettext.install('test1') and even then, if
test3.py imports test2.py, messages won't be translated in test3.py. I
have to import test3.py in test1.py too.
Is this normal behaviour or is there something I'm missing (again) ? Hmm, perhaps I missed something, namespaces can be sometimes hard
to get right.
Let's see:

test1 imports and installs gettext.
Now test1 holds the gettext reference.
test1 imports test2 and test3 and 'inherits' test1 namespace
test2 and test3 have access to test1 namespace and the gettext.

So in (pseudo) Python code test1 would looks like this:

import gettext
gettext.install(test)# assuming test.mo holds all the strings
import test2, test3

Another approach would be to place the whole gettext install stuff
in a separate module wrapped in a function which can be imported
by every module that wants language support.
(That's the approach I always use)

Contents of utils.py
(Code is not tested, it's to show the principals)

import locale, gettext
def set_gettext(mo_location='/usr/share/locale'):
locale.setlocale(locale.LC_ALL)
gettext.install('test1', mo_location)

Now in your modules you could do this:

In test1.py:
from utils import set_gettext
set_gettext()# remember gettext.install installs '_' in the
# current namespace, in this case test1.
import test2, test 3

In test4.py
from utils import set_gettext
set_gettext()
.....
.....

Now test1 and test2 get their gettext from test1 while test4 import
gettext from utils.py which installs the same mo file as it did in
test1, test2 and test3.
PS : your project looks nice !

Thanks.

BTW, the principal of gettext in utils.py is also implemented in
the gvr project, so you have a real life example there :-)

Stas

Aug 10 '05 #13
stasz a écrit :
On Tue, 09 Aug 2005 23:59:26 +0200, cantabile wrote:

stas a écrit :

As a reminder, make sure that you install gettext in the namespace
of your toplevel module.


[....]
Noticed something :
I must import test2.py AFTER gettext.install('test1') and even then, if
test3.py imports test2.py, messages won't be translated in test3.py. I
have to import test3.py in test1.py too.
Is this normal behaviour or is there something I'm missing (again) ?


Hmm, perhaps I missed something, namespaces can be sometimes hard
to get right.
Let's see:

test1 imports and installs gettext.
Now test1 holds the gettext reference.
test1 imports test2 and test3 and 'inherits' test1 namespace
test2 and test3 have access to test1 namespace and the gettext.

So in (pseudo) Python code test1 would looks like this:

import gettext
gettext.install(test)# assuming test.mo holds all the strings
import test2, test3

Another approach would be to place the whole gettext install stuff
in a separate module wrapped in a function which can be imported
by every module that wants language support.
(That's the approach I always use)

Contents of utils.py
(Code is not tested, it's to show the principals)

import locale, gettext
def set_gettext(mo_location='/usr/share/locale'):
locale.setlocale(locale.LC_ALL)
gettext.install('test1', mo_location)

Now in your modules you could do this:

In test1.py:
from utils import set_gettext
set_gettext()# remember gettext.install installs '_' in the
# current namespace, in this case test1.
import test2, test 3

In test4.py
from utils import set_gettext
set_gettext()
....
....

Now test1 and test2 get their gettext from test1 while test4 import
gettext from utils.py which installs the same mo file as it did in
test1, test2 and test3.

PS : your project looks nice !


Thanks.

BTW, the principal of gettext in utils.py is also implemented in
the gvr project, so you have a real life example there :-)

Stas

Thanks for the explanation, very clear, as usual.
Cheers :)
Aug 10 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Daniel CAUSSE | last post: by
1 post views Thread by Dave Patton | last post: by
1 post views Thread by evanescent.lurker | last post: by
14 posts views Thread by Pierre Rouleau | last post: by
1 post views Thread by Terry Hancock | last post: by
reply views Thread by redhog | last post: by
1 post views Thread by James T. Dennis | last post: by
6 posts views Thread by =?iso-8859-1?B?QW5kcuk=?= | last post: by
2 posts views Thread by paolob | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.