Saturday 5 July 2008

Euro-friendly permalinks

Permalink_fu is rather an awesome thing. The only drawback is that it doesn't really handle accented characters very well. I've looked around and seen a bunch of solutions that attempt to be 'clever' and do the substitution on one line, etc. But I decided that I'd rather go for brute force.
I created an extension to String with one method called 'no_accents':
class String
def no_accents


Then I do a whack of these:

    str = self
#Spanish letters
str = str.gsub("á", "a")
str = str.gsub("é","e")

For upper and lower case.
Obviously there is a big list. Right now I am just doing Spanish, French and German as they are the ones I am most interested in.

I had to make a change to Permalink_fu to call my method first:
    def escape(str)
str = str.no_accents
s = ((translation_to && translation_from) ? Iconv.iconv(translation_to, translation_from, str) : str).to_s


But that's all.

Here is the file. What I did is put it in my initializers directory.

If you add to it maybe you could post your additions. I am sure some clever-clogs out there will come up with a more slick solution. But... mine works!

No comments: