1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 | # Introduction: i18n and django
# This is a nasty issue: on Python 2.3 gettext expects either ascii
# or unicode. While on Python 2.4 gettext can deal with utf-8 encoded
# strings too.
# One is encouraged to use unicode on strings all the time but this
# breaks django (0.95.1) in many places where it expects objects to
# to work with ``str''. This leaves your code quite inconsitant, as
# your strings are sometimes marked as unicode and sometimes not --
# everywhere wehre django would want to call ``str'' on your object.
# Helper
def d(o,encoding='utf-8'):
try:
return o.decode(encoding)
except:
return o
def e(o,encoding='utf-8'):
try:
return o.encode(encoding)
except:
return o
# Stupid quickfix: Let gettext handle utf-8 strings. on fail
# convert them to unicode. (this lets you use non unicode all over
# the place if you use the _() function for translation.
trans = _
def _(o,encoding='utf-8'):
try:
return trans(o)
except UnicodeDecodeError:
return trans(d(o,encoding))
# probably better: change all strings into utf-8 for djangos internal handling.
# this though requires the _() handle on every! string. But you could (once
# django is true unicode, just drop this wrapper and be happy :))
#def _(o,encoding='utf-8'):
# return e(trans(o),encoding)
|
Comments