|
Peter Marklund's Home |
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:
- A controller test failed due to some routing change. It expected a redirect to http://test.host/ticket_types/106767319 but got http://test.host/events/592144301/ticket_types/106767319. I fixed this by commenting out the test since I was going to remove the UI anyway.
- DEPRECATION WARNING: The binding argument of #concat is no longer needed. I simply removed the binding argument.
- ActiveSupport namespacing. I needed to move OrderedOptions into ActiveSupport::OrderedOptions in the app_config plugin.
- DEPRECATION WARNING: truncate takes an option hash. I changed my invocations of truncate to use a hash.
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 = "/config/locales/"
locale_files = Dir["/*.{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
Peter Marklund said about 1 year 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 about 1 year ago:
Hi,
Nice article, I just attended a session on this new feature of rails.
thanks,




Tim Haines said about 1 year 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.