[Twisted-web] support for other languages in newov
Mike Mueller
twisted-web@twistedmatrix.com
Thu, 15 Jan 2004 05:02:29 +0100
I am playing around with newov and especially freeform. It is very nice.
Since I need to support German at my web site it would nice if error=20
messages and other information that are sent back to the visitor could be=20
in an other language.
I came up with a simple solution using a dict that holds all the messages=20
in different languages.
multilang.py
multilangDict =3D {'emptyPasswd':{'eng':"Please enter a non-empty=
password.",
'de':"Ein leeres Passwort ist nicht=20
erlaubt. Eingabe bitte wiederholen!"
},
'passwdDontMatch':{'eng':"Passwords do not match. Please=
=20
reenter.",
'de':"Die Passw=F6rter stimmen nicht=20
=FCberrein. Eingabe bitte wiederholen!"
}
}
Umlauts (=F6 and =FC) work with my browser settings but might need to use &=
=20
uml; notation:
multilang2.py
from newov stan import xml
multilangDict =3D {'emptyPasswd':{'eng':"Please enter a non-empty=
password.",
'de':"Ein leeres Passwort ist nicht=20
erlaubt. Eingabe bitte wiederholen!"
},
'passwdDontMatch':{'eng':"Passwords do not match. Please=
=20
reenter.",
'de':xml("Die Passw¨rter stimmen=20
nicht =E6uuml;berrein. Eingabe bitte wiederholen!")
}
}
Unicode might be necessary if languages like Chinese are needed.
in freeform.py you just put:
from nevow import multilang
multilangDict =3D multilang.multilangDict
lang =3D 'de' #read from configuration somewhere or set by visitor or url=
=20
like www.example.com/mylang/foo
and change the formless.InputError arguments to dict lookups, e.g.:
class PasswordValidator(components.Adapter):
__implements__ =3D IInputValidator,
def validate(self, context, boundTo, data):
"""Password needs to look at two passwords in the data,
"""
pw1 =3D data[0]
if pw1 =3D=3D '':
raise formless.InputError(multilangDict['emptyPasswd'][lang])=
=20
#changed
args =3D context.locate(iwoven.IRequest).args
binding =3D context.locate(formless.IBinding)
pw2 =3D args.get("%s____2" % binding.name, [''])[0]
if pw1 !=3D pw2:
raise=20
formless.InputError(multilangDict['passwdDontMatch'][lang]) #changed f
return self.original.coerce(data[0])
This works the way I want it. Since the number of messages is rather small=
=20
a dict seems ok in terms of memory usage.
Because the languages part is in a separate file, it can be maintained by=20
anybody who knows the (foreign) languages without the need of looking=20
through the code. If the same message is used at several places changing it=
=20
becomes much easier too.
How do you thing about it?
Mike