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

I use the permalink_fu plugin to create permalinks from headers. My problem: if the header contains German characters, they are simply replaced with "_".

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

I fount String.tr, but the problem is that it replaces 1 character with 1 replacement, so it will work to replace

é c e ø c o

and etc.

Does anyone have a good and clean solution for this?

thanks

+5
source share
7 answers

Use String.gsub():

"ich bin doch nicht blöd, mann!".gsub(/[äöü]/) do |match|
    case match
        when "ä"
          'ae'
        when "ö"
          'oe'
        when "ü"
          'ue'
    end
end

Of course, the search can be improved using a lookup table, but the principle should be clear.

+7

( 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"

, rails-i18n.

+12

Asciify

$ sudo gem install asciify

:

#!/bin/ruby
require "asciify"

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

YAML , :

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

(. )

, , , , . , , .

+9
"äöü".gsub('ä','ae').gsub('ö','oe').gsub('ü','ue')

;)

+5

. JavaScript, aproach. , - , .

0

: "Ich bin doch nicht böld ähhh ühh öhhh".gsub(/[äöüßÄÖÜ„"§%&–+]/){|t|t.to_xs}

-1

All Articles