Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace umlaute (äüö) for SEO link in rails - best way

I'm using the permalink_fu plugin to create permalinks from titles. My problem is: If the title contains german characters, they are just replaced with '_'.

What I need is something that replaces ä with ae ü with ue ö with oe

I fount String.tr but the problem here is that it replaces 1 character with 1 replacement, so it would work for replacing

é with e ø with o

etc.

Does anyone have a nice and clean solution for that?

Thanks

like image 775
Ole Spaarmann Avatar asked May 18 '09 17:05

Ole Spaarmann


2 Answers

Look at transliterate and parameterize (with transliterations in locales/de.yml):

http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-transliterate

I18n.transliterate("Über der Höhenstraße")
 => "Ueber der Hoehenstrasse"

http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-parameterize

"Über der Höhenstraße".parameterize
 => "ueber-der-hoehenstrasse"

If you don't want to write the transliterations yourself, you can install the rails-i18n gem.

like image 90
eikes Avatar answered Nov 15 '22 11:11

eikes


I have written a small Library called Asciify for exactly that purpose

$ sudo gem install asciify

Usage:

#!/bin/ruby
require "asciify"

"Lücke".asciify   #=> "Luecke"

You can provide a YAML-file for custom mappings like so:

translator = Asciify.new("/path/to/mappings.yaml")
output_string = translator.convert("input string")

(see the builtin default mapping for the expected format)

The whole project is quite old, but maybe it does the job you need it to. If not, maybe the source code will be helpful.

like image 36
levinalex Avatar answered Nov 15 '22 09:11

levinalex