- March 7, 2007
- Pre .96
- unicode gettext utf-8 2.4 2.3
- 0 (after 0 ratings)
I ran into this because my development system is django on python 2.4 and I deploy to 2.3. It's a corner case where you use gettext, the
# -*- coding: utf-8 -*-
header and want to have a consistant style in your file.
It is encouraged to use the unicode marker like u'string', but this does not work for str methods of your models as they are called by the ``str'' function and that function again can not handle unicode. It would be really nice to have all unicode intern and only do the appropriated encoding on the output.
Anyway. With this little helper you can clutter your files with _('stirng of heart with € äüöß') ... With the coding directive in the header python 2.4 and gettext can handle this on 2.3 though they can't. So this script adds a parachut to the gettext wrapper that kicks in if gettext is failing.
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)
More like this
- Character encoding fix by mrtron 6 years, 10 months ago
- UnicodeFixer by jeanmachuca 5 years, 2 months ago
- A view for downloading attachment by achimnol 5 years, 6 months ago
- SQLLoggerMidleware + infobar by robvdl 7 years, 1 month ago
- CustomChoiceField, Selectable label field version of ModelChoiceField by mauro 6 years, 11 months ago