6

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

Ole Spaarmann
  • 15,845
  • 27
  • 98
  • 160
  • Duplicate of http://stackoverflow.com/questions/225471/how-do-i-replace-accented-latin-characters-in-ruby – James A. Rosen May 18 '09 at 22:05
  • Actually, I take that back. You seem to want to replace only select characters, not all. – James A. Rosen May 18 '09 at 22:07
  • What version of Ruby are you using? 1.8 was barely Unicode aware. 1.9.2 is much, much, better and provides some good code-set translation tools which are aware of multi-byte characters, instead of being tied to bytes. – the Tin Man Dec 30 '10 at 22:24

7 Answers7

17

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.

eikes
  • 4,811
  • 2
  • 31
  • 31
9

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
  • 5,889
  • 2
  • 34
  • 48
7

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.

mb21
  • 34,845
  • 8
  • 116
  • 142
David Schmitt
  • 58,259
  • 26
  • 121
  • 165
6
"äöü".gsub('ä','ae').gsub('ö','oe').gsub('ü','ue')

;)

mb21
  • 34,845
  • 8
  • 116
  • 142
Bijan
  • 25,559
  • 8
  • 79
  • 71
0

Try String.sub!.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
0

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.

Community
  • 1
  • 1
Tomalak
  • 332,285
  • 67
  • 532
  • 628
-1

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

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101