autotranslatslugify

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# -*- coding: utf-8 -*-
from django.template import defaultfilters

# TODO: Study whether this is enough, if Django does not do any strange stuff this should be enough to make it overwrite the slugify function in any case.

# Get the old slugifier
old_slugify = defaultfilters.slugify

def translat_slugify(str):
    char_translat = { 
        ("ä","Ä",) : "a",
        ("ö","Ö",) : "o",
    }

    for chars,trans in char_translat.items():
        for char in chars:
            str = str.replace(char.decode('utf-8'),trans)
    
    return old_slugify(str)


# "Overwrite" the old slugify
defaultfilters.slugify = translat_slugify

More like this

  1. Unique Slugify by SmileyChris 6 years ago
  2. Admin Input Field Character Count via jQuery by joshman 5 years, 4 months ago
  3. i18n base model for translatable content by foxbunny 5 years, 9 months ago
  4. pyserver -- runserver alias by presclark 5 years, 11 months ago
  5. Multilingual Models by Archatas 7 years, 1 month ago

Comments

exogen (on April 15, 2007):

The idea is good, it should be extended to do total ASCII replacement like AsciiDammit.py. Also, I recommend using re.sub for multiple replacements like this.

#

Ciantic (on April 19, 2007):

Exogen, thanks! I didn't knew about AsciiDammit.py before.

Although it looks like (what I've found from google) that the translat table is hardcoded to python code in AsciiDammit.py. It is not good, after all it really isn't languages problem to handle such general things. I know better solution, using iconv and it's ratherly new feature translat to convert umlauts and friends to ASCII. Unfortunately I have no time to implement that.

And yes, this was more of intended as example how you can get rid of the "My 3rd party app (for example one in contrib) uses slugify function that does not support my umlauts blah blah..."

#

(Forgotten your password?)