I'm working on this project that has some German titles, and a lot of irregularities inside them
(question marks, underscores, etc.)
Because I have to refer the object (in this case product) by some sort of understandable name, I've decided to slugify the title
(basically turn "THIS EXAMPLE!!" to "this-example")

While I'm already doing that, I wanted to cleverly switch the "ä,ö,ü,ß" to "ae,oe,ue,ss" accordingly.
I'm pretty sure this might be useful to someone, and it's pretty harmless to share it so...
this gist was born, and the code is:

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
# -*- coding: utf-8 -*-
def replace_all(text, dic):
    for replace_with, replace_what in dic.iteritems():
        if type(replace_what) is list:
            for letter in replace_what:
                text = text.replace(letter,replace_with)
        else:
            text = text.replace(replace_what, replace_with)
    return text</p>
<p>#example input: ssdfSERFS - ^#$%43 GSDG  ____ :VKbm sdF öüoäßßEF_____-___ d a s d _e &lt;&gt;DFGDFG????
#example output: ssdfserfs-43-gsdg-vkbm-sdf-oeueoaessssef-d-a-s-d-e-dfgdfg
def slugify(string,params = []):
    import re
    #unescape html entity signs (e.g. &amp;amp; -&gt; &amp;) &amp; lowercase string
    import HTMLParser
    string = HTMLParser.HTMLParser().unescape(string).lower()
    translation_dic = {
           'ae' : ['Ä','ä','&amp;Auml;','&amp;auml;'],
           'oe' : ['Ö','ö','&amp;Ouml;','&amp;ouml;'],
           'ue' : ['Ü','ü','&amp;Uuml;','&amp;uuml;'],
           'ss' : ['ß','&amp;szlig;'],
    }
    string = replace_all(string,translation_dic)
    string = re.sub(r'[^a-zA-Z0-9\_\-\s]','',string)
    string = re.sub(r'[\s]{2,}',' ',string)
    string = string.replace(' ','-').replace('_','-').strip(' -')
    string = re.sub(r'[\-]{2,}','-',string)</p>
<p>    return string</p>