ruby-on-railsrubystringseoreplace

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


Solution

  • 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 lookup can be improved by using a lookup table, but the principle should be clear.