views:

778

answers:

5

Hi,

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

+3  A: 

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.

David Schmitt
A: 

Try String.sub!.

Charlie Martin
A: 

I asked a similar question once. It was for JavaScript, and it takes a regex based aproach. Maybe the solution still carries some value for you, methodologically speaking.

Tomalak
+1  A: 

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.

levinalex
A: 

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

steffen