gettext again

cantabile cantabile.03 at wanadoo.fr
Wed Aug 10 18:10:37 CEST 2005


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 :)



More information about the Python-list mailing list