Skip to content
kwi edited this page Sep 14, 2010 · 21 revisions

I18n_routing

I18n_routing is a plugin for Ruby on Rails that lets you easily translate your routes trough the I18n api included in Rails since version 2.2

Works with : resources, resource and named_routes (or Rails3 match)

Note: This plugin does not provide any url locale recognition, it just deserve its main purpose: translate your rails routes. You need to find your own way for setting the current locale (checking the domain or the subdomain for example !)

Why translating routes ?

  • A must have if your Rails website is localized.
  • More friendly for your visitors.
  • Url are a key point for SEO, place keywords in them !

I18n_routing keypoints

  • No translations are made during runtime, all is precompiled when building routes at startup.
  • Works with Rails 2.x series (> 2.2) and with Rails 3
  • Built on top of I18n api => translating your routes has never been as simple as now
  • Works with simple resource(s) to deep nested resource(s)
  • Can translates path names like new/edit and custome ones

Example (Rails 3)

First, declare a localized resources in your routes.rb :

localized do
  resources :users
end

Then, translate your resources in your locales files (if you are using the simple Backend, or anywhere else depending on your I18n backend):

fr:
  resources:
    users: 'utilisateurs'

After that, when you will use any users route helpers in your app, it will transparently use the correct route path depending on your current locale.

$ rails console
ruby-1.8.7-p249 > I18n.locale = :en
 => :en 
ruby-1.8.7-p249 > app.users_path
 => "/users" 
ruby-1.8.7-p249 > I18n.locale = :fr
 => :fr 
ruby-1.8.7-p249 > app.users_path
 => "/utilisateurs" 
ruby-1.8.7-p249 > 

I18n gem version warning

Be careful when running Rails 2.3 with the shipped i18n gem: this is an old i18n version and i18_routing will install the latest i18n version as a gem.
This latest version may be incompatible with some old usages.
Furthermore, if the i18n gem is present on your system, Rails will load it and skip the shipped version; Keep that in mind.

Clone this wiki locally