16 May 2012

Ruby: working with non-English strings

I’m working on several Ruby on Rails web applications that supports English but it’s not the default locale. The localization support in Rails is very good in my opinion. I’m following this guide and everything works as expected so far.

However I have detected a weird behaviour in one of my sites, accentuated vocals and the Spanish ‘ñ’ are not properly upcased.

    1.9.3p0 :004 > name = "Martínez"
    => "Martínez"
    1.9.3p0 :005 > name.upcase
    => "MARTíNEZ"
    1.9.3p0 :006 > country = "España"
    => "España"
    1.9.3p0 :007 > country.upcase
    => "ESPAñA"

In order to fix this I installed the UnicodeUtils gem that does exactly what I want (among many other Unicode string manipulations).

    1.9.3p0 :008 > UnicodeUtils.upcase country
     => "ESPAÑA"
     1.9.3p0 :009 > UnicodeUtils.upcase name
    => "MARTÍNEZ"