[Twisted-web] support for other languages in newov
Mike Mueller
twisted-web@twistedmatrix.com
Fri, 16 Jan 2004 01:00:03 +0100
>Mike Mueller [Thu, Jan 15, 2004 at 05:02:29AM +0100]:
> > I came up with a simple solution using a dict that holds all the messages
> > in different languages.
>[...]
> > How do you thing about it?
At 09:02 15.01.2004 +0100, Michal wrote:
>It's really nice. Any particular reasons you didn't use gettext?
Just that I didn't know that it exists. Well, I didn't look for it.
After looking at it, the problem is that gettext assumes that there is a
global language that is valid for the the whole application at any given
point in time.
I would like to have two (or more) languages at the same time. Depending on
the preferred language of the current visitor, each session has "its" language.
Even with my approach, using the lang attribute on the module level, will
not work if asynchronous programming is involved. Since I am going to use
deferreds, time is 'not linear any more'. So changing the lang attribute of
a module back and forth might have effects on different sessions(?)
A solution would be to make the lang a session attribute:
At the first encounter of the session do:
request = context.locate(iwoven.IRequest)
session = request.getSession()
lang = getattr(session, 'lang', currentLang)
later you might want to change the language:
request = context.locate(iwoven.IRequest)
session = request.getSession()
session.lang = currentLangNow
Extend the relevant parts of freeform.py (and other modules that return
messages) to:
class PasswordValidator(components.Adapter):
__implements__ = IInputValidator,
def validate(self, context, boundTo, data):
"""Password needs to look at two passwords in the data,
"""
request = context.locate(iwoven.IRequest)
session = request.getSession()
pw1 = data[0]
if pw1 == '':
raise
formless.InputError(multilangDict['emptyPasswd'][session.lang]) #changed
args = context.locate(iwoven.IRequest).args
binding = context.locate(formless.IBinding)
pw2 = args.get("%s____2" % binding.name, [''])[0]
if pw1 != pw2:
raise
formless.InputError(multilangDict['passwdDontMatch'][session.lang]) #changed
return self.original.coerce(data[0])
This should give you the possibility to guess the language from
request.getHeader('user-agent')
and also let the visitor to select his/her preferred languages (you know
those small icons with flags).
I don't know if this is the best way to do it. If yes, is there any way
that it makes it into the source? It would essentially mean adding this two
lines to all the methods that return some kind of message to the visitor,
provided all of the relevant methods take a context argument. And of course
it means using multilangDict['phraseKey'][session.lang] instead of a
message English message consistently .
I would volunteer to implement this and could also make the German messages.
Mike