Peter Marklund

Peter Marklund's Home

Wed September 10, 2008
Programming

Upgrading to Rails 2.2 and Drinking the I18n Koolaid

The other day I got tired of waiting for the Rails 2.2 release and upgraded my application from Rails 2.1 to edge. The process went a little something like this:

rake rails:freeze:edge

cd vendor/plugins
rm -rf rspec
rm -rf rspec_on_rails
git clone git://github.com/dchelimsky/rspec.git rspec_on_rails
git clone git://github.com/dchelimsky/rspec-rails.git
rm -rf rspec/.git
rm -rf rspec-rails/.git
cd ../../
./script/generate rspec

At this point I ran my Test::Unit tests and RSpec specs with rake and ended up with a single failing test and a bunch of warnings:

Overall, it was fairly easy to transition from 2.1 to edge. The really interesting part though was I18n. Thanks to the new I18n support in Rails I got to throw out the Simple Localization plugin that apparently is no longer supported, as well as my own hack to get error messages to be localized. I used the I18n demo app as a starting point and added config/locales/sv-SE.yml. It turned out the structure of I18n keys had changed a little since the demo app was written. You can use my file as a starting point or probably better, copy the following files into your single locales file and translate those:

vendor/rails/actionpack/lib/action_view/locale/en-US.yml
vendor/rails/activerecord/lib/active_record/locale/en-US.yml
vendor/rails/activesupport/lib/active_support/locale/en-US.yml

I source the locales file from config/initializers/i18n.rb:

I18n.default_locale = 'en-US'

LOCALES_DIRECTORY = "#{RAILS_ROOT}/config/locales/"
locale_files = Dir["#{LOCALES_DIRECTORY}/*.{rb,yml}"]
LOCALES_AVAILABLE = (locale_files.collect do |locale_file|
  File.basename(File.basename(locale_file, ".rb"), ".yml")
end + ['en-US']).uniq
locale_files.each { |locale_file| I18n.load_translations locale_file }

I am using the gibberish and gibberish_translate plugins and am quite happy with those. Of course, it would be nice if they were rewritten to use the new I18n API. Another TODO item is to move over attribute names from Gibberish to I18n. I have my own override of ActiveRecord::Base.human_attribute_name that is no longer needed now that the 2.2 version of the method so nicely does I18n message lookups (with a key naming convention like 'activerecord.attributes.model_name.attribute_name').

Thanks so much to Sven Fuchs and the I18n team, Jeremy Kemper, and all the others who made I18n a part of Rails! Code will be cleaner from now on and life easier...

Comments

Tim Haines said over 6 years ago:

Hey Peter,

I was wondering how other people are getting at edge rails and rspec. I've been using git submodules. Have been warned they can get messy - but haven't struck any problems yet. Interesting to see your approach.

Tim.

--------------------------------------------------------------------------------

Peter Marklund said over 6 years ago:

For rails and rspec I just use rake rails:freeze:edge and git clone as described above. I don't update them very often.

At my last company we used Git submodules for our own plugins that we shared across our Rails apps in a distributed system. We struggled for weeks to get this to work and eventually resorted to using symlinks instead. The symlink solution was simple and worked like a charm. Apparently Git submodule don't work for something that you and others are updating frequently. If you are never updating the submodule yourself and just want to fetch changes others have made every once in a while I guess it's a good solution.

--------------------------------------------------------------------------------

Akhil Bansal said over 6 years ago:

Hi,

Nice article, I just attended a session on this new feature of rails.

thanks,

--------------------------------------------------------------------------------