Translated messages and 'gettext' API (was Re: [I18n-sig] Literal strings)

Peter Funk pf@artcom-gmbh.de
Fri, 2 Jun 2000 14:48:10 +0200 (MEST)


Hi, 

[M.-A. Lemburg]:
> I'm currently using my own scheme for solving the NLS problem,
> but it currently only work on a per-process basis. What I am
> looking for now, is a way to be able to set the language
> on a per user (of a single server process) basis.
> 
> Is the gettext approach useful for this too, i.e. does it
> allow fast switching of the target language ?

Not as is.  

Currently my module 'fintl.py' is simply a small wrapper around MvLs
'intl' interface to the GNU gettext C-library, if this is available
and an emulator, which does the same as GNU gettext library does in
pure Python.  My goal was to 1. avoid GPL infection and 2. to use
the same API on Non-Unix platforms like WinXX and MacOS).

But mailman seems to have a similar problem, 
Juan Carlos Rey Anaya <jcrey@uma.es> has taken the module 'gettext.py'
by James Henstridge <james@daa.com.au> and modified it to support
dynamic loading of message catalogs.

Based on a suggestion made by Françios Pinard in his mail to python-list 
from 15 Jan 2000 20:15:08 I thought it would be a nice idea, to
replace the current singleton pattern for locale and catalog setting 
with a 'Translator' class, from which you may create several instances.  
This is trivial to implement, if you don't have to pay too much attention
on memory consumption and don't insist to be API compatible with
GNU gettext.

Of course this will introduce some additional complexity: 
either you have to carry the "right" Translator instance around to 
all places, where messages are used in order to access the right 
'gettext' method, or you have to expose some global default 
state, for example through the  following two functions:

    def switch_language(new_language):
        global _current_translator
        if new_language != _current_translator.language:
	    if not _translators.has_key(new_language):
	        _translators[new_language] = Translator(new_language)
	    _current_translator = _translators[new_language]
	...
    def query_language():
	return _current_translator.language
 
I'm not sure, what is needed.

Regards, Peter
-- 
Peter Funk, Oldenburger Str.86, D-27777 Ganderkesee, Germany, Fax:+49 4222950260
office: +49 421 20419-0 (ArtCom GmbH, Grazer Str.8, D-28359 Bremen)