Thanks a lot for your replies. The following code does the job for me ...
>From 8a110f8358f545fae0a9d0c8d69b496fd0e35cdf Mon Sep 17 00:00:00 2001 From: Thomas Rega <m...@thoreg.org> Date: Mon, 29 Nov 2010 16:51:01 +0100 Subject: [PATCH] custom template filter to convert umlauts This simple custom template filter converts German umlauts from unicode to ascii. 99,99 percent of this code are stolen from: http://djangosnippets.org/snippets/588/ --- plain_site/templatetags/unicode_to_ascii.py | 75 +++++++++++++++++++++++++++ 1 files changed, 75 insertions(+), 0 deletions(-) create mode 100644 plain_site/templatetags/unicode_to_ascii.py diff --git a/plain_site/templatetags/unicode_to_ascii.py b/plain_site/templatetags/unicode_to_ascii.py new file mode 100644 index 0000000..730ab52 --- /dev/null +++ b/plain_site/templatetags/unicode_to_ascii.py @@ -0,0 +1,75 @@ + +import unicodedata, sys + +from django import template +from django.template.defaultfilters import stringfilter + +register = template.Library() + +# Translation dictionary. Translation entries are added to this +# dictionary as needed. + +CHAR_REPLACEMENT = { + + 0xc4: u"Ä", + 0xd6: u"Ö", + 0xdc: u"Ö", + + 0xdf: u"ß", + + 0xe4: u"ä", + 0xf6: u"ö", + 0xfc: u"ü", + + 0x2018: u"'", # LEFT SINGLE QUOTATION MARK + 0x2019: u"'", # RIGHT SINGLE QUOTATION MARK + 0x201c: u'"', # LEFT DOUBLE QUOTATION MARK + 0x201d: u'"', # RIGHT DOUBLE QUOTATION MARK +} + + +class unaccented_map(dict): + """ + Maps a unicode character code (the key) to a replacement code + (either a character code or a unicode string). + """ + + def mapchar(self, key): + ch = self.get(key) + if ch is not None: + return ch + + de = unicodedata.decomposition(unichr(key)) + if key not in CHAR_REPLACEMENT and de: + try: + ch = int(de.split(None, 1)[0], 16) + except (IndexError, ValueError): + ch = key + else: + ch = CHAR_REPLACEMENT.get(key, key) + self[key] = ch + return ch + + if sys.version >= "2.5": + # use __missing__ where available + __missing__ = mapchar + else: + # otherwise, use standard __getitem__ hook (this is slower, + # since it's called for each character) + __getitem__ = mapchar + +...@register.filter(name='unicode_to_ascii') +...@stringfilter +def unicode_to_ascii(unicodestring): + """ + Convert a unicode string into an ASCII representation, converting non-ascii + characters into close approximations where possible. + + Special thanks to http://effbot.org/zone/unicode-convert.htm + + @param Unicode String unicodestring The string to translate + @result String + """ + charmap = unaccented_map() + return unicodestring.translate(charmap).encode("ascii", "ignore") + -- 1.5.6.5 2010/11/29 David De La Harpe Golden <david.delaharpe.gol...@ichec.ie>: > On 29/11/10 13:20, Thomas Rega wrote: >> Hi, >> >> is there a recommended way to deal with German 'umlauts' (like ü.ö.etc.) >> I was lookingfor some kind of template filter but without success :( >> The data is stored as utf-8 format. What I am looking for is an >> automatic transformation into 'ü, ö etc. >> Is such a template filter the right approach? >> > > Well, I suppose you could, but just in case do bear in mind most stuff > these days is after all utf-8 capable. I just wouldn't bother mapping > to html entities unless you know you need something else, really. > Django in fact defaults to utf-8 [1] > > [1] http://docs.djangoproject.com/en/dev/ref/settings/#default-charset > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- --- http://thoreg.org/ --- -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.