gettext again

C

cantabile

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.
 
C

cantabile

stasz a écrit :
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 ?
 
S

stasz

stasz a écrit :
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
 
C

cantabile

stasz a écrit :
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 (e-mail address removed) file
5° msgfmt -o test1.mo (e-mail address removed)
6° cp test1.mo /usr/share/locale/LC_MESSAGES
7° python test1.py


This is what I did, without success. Must be bewitched :((
 
S

stasz

stasz a écrit :
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 (e-mail address removed) file
5° msgfmt -o test1.mo (e-mail address removed)
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
 
C

cantabile

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 !
 
C

cantabile

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. ?
 
S

stas

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
 
C

cantabile

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 !
 
S

stasz

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
 
C

cantabile

stasz a écrit :
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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,332
Latest member
LeesaButts

Latest Threads

Top