Rails Gotchas with Rails 3, Ruby 1.9.2, i8ln and Emacs

28 Nov 2010

Recently, I've started working on a new app that needs internationalization (a.k.a i8ln in short) from the get go. Since this is a new project, we've decided to use the latest and greatest stack (and just in time too, there seemed to have been several issues with earlier releases). We're using rvm, Rails 3.0.3 and Ruby 1.9.2 (p0) with Passenger 3. Everything seemed to run smoothly, except for the dreaded "Invalid byte sequence in UTF-8" error, whenever we're using a non-Latin character. Which was weird, because all the conditions that make this happen appear to have already been fixed, according to several sources (check if config.encoding = "utf-8" is set in config/application.rb file) so it should have already been taken care of.

After several frustrating searches, and even going to the extent of switching back to Ruby 1.8.7 to see if it's some sort of the dreaded 1.9.2 string encoding issue, I finally found the problem. I use Emacs for most of my coding, and turns out that by default emacs uses a LATIN-1 encoding, so Rails 3 throws the above mentioned error whenever I type non-ASCII characters in the YAML file used for placing all the translations (en.yml, ru.yml, fr.yml, etc). I had to make emacs read/write the file in UTF-8. Just in case you're interested, here's how to do that in your .emacs:

1     ;; set up unicode
2     (prefer-coding-system       'utf-8)
3     (set-default-coding-systems 'utf-8)
4     (set-terminal-coding-system 'utf-8)
5     (set-keyboard-coding-system 'utf-8)
6     (setq default-buffer-file-coding-system 'utf-8)
7     (setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING))

Hope that helps! Now everything works smoothly, also thanks to Emacs hints picked up from here and here.