Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I replace accented Latin characters in Ruby?

I have an ActiveRecord model, Foo, which has a name field. I'd like users to be able to search by name, but I'd like the search to ignore case and any accents. Thus, I'm also storing a canonical_name field against which to search:

class Foo   validates_presence_of :name    before_validate :set_canonical_name    private    def set_canonical_name     self.canonical_name ||= canonicalize(self.name) if self.name   end    def canonicalize(x)     x.downcase.  # something here   end end 

I need to fill in the "something here" to replace the accented characters. Is there anything better than

x.downcase.gsub(/[àáâãäå]/,'a').gsub(/æ/,'ae').gsub(/ç/, 'c').gsub(/[èéêë]/,'e').... 

And, for that matter, since I'm not on Ruby 1.9, I can't put those Unicode literals in my code. The actual regular expressions will look much uglier.

like image 927
James A. Rosen Avatar asked Oct 22 '08 12:10

James A. Rosen


1 Answers

ActiveSupport::Inflector.transliterate (requires Rails 2.2.1+ and Ruby 1.9 or 1.8.7)

example:

>> ActiveSupport::Inflector.transliterate("àáâãäå").to_s => "aaaaaa"

like image 116
Mark Wilden Avatar answered Sep 19 '22 17:09

Mark Wilden