Monday, May 9, 2011

Rails3 UTF-8 support for strings

There is a general problem in rails 3 with utf8 string support. For example upcase, downcase, titleize or capitalize. Basically these methods produce ASCII strings because of lots of memory usage of unicode strings. mb_chars string method do the trick. Create a new rb file in /lib:
[String].each do |klass|
  klass.class_eval <<-RUBY, __FILE__, __LINE__
 
 def utf8_downcase
  mb_chars.downcase.to_s
 end
 
 def utf8_upcase
  mb_chars.upcase.to_s
 end
 
 def utf8_capitalize
  mb_chars.capitalize.to_s
 end

 def utf8_titleize
  mb_chars.titleize.to_s
 end

RUBY
end

Works with Rails 3.0.7 and Ruby 1.8 surely. Don't forget to put in config/application.rb this row after require 'rails/all':

Dir.glob("./lib/*.{rb}").each { |file| require file }
Now you can call:

irb(main):005:0> "é ÉÚŐÚ ééé ŐŐÚSDFSF".utf8_downcase
=> "é éúőú ééé őőúsdfsf"

That's all. Questions?

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.