Possible Duplicate:
Transliteration in ruby
I am searching for a simple way to convert strings like these:
- "spaß" to "spass"
- "über" to "ueber"
- etc.
This is needed for generating valid usernames from names of people.
Possible Duplicate:
Transliteration in ruby
I am searching for a simple way to convert strings like these:
This is needed for generating valid usernames from names of people.
This is called transliteration. An approximation of this (see examples) can be performed using the Iconv class.
Try one of the following (require 'iconv' first):
Iconv.iconv('ascii//ignore//translit', 'utf-8', string).to_s
Iconv.iconv('ascii//translit', 'utf-8', string).to_s
irb(main):013:0> Iconv.iconv('ascii//translit', 'utf-8', 'spaß').to_s
=> "spass"
irb(main):014:0> Iconv.iconv('ascii//translit', 'utf-8', 'crêpes').to_s
=> "crepes"
irb(main):017:0> Iconv.iconv('ascii//translit', 'utf-8', 'über').to_s
=> "uber"
There's also an iconv
command line utility. More information on that and some Ruby examples (search for 'ruby') here.
An alternative to this is Unidecode, which I guess was inspired by the original Perl implementation. I haven't used it in its Ruby incarnation, but it should do multi-char expansions (which apparently you want) better.
Finally, if you're running Rails, you may find this thread interesting. It details some differences between alternative approaches to transliteration, and shows a way to do this within the Rails core (ActiveSupport::Inflector.transliterate
)