I18n_routing: Translate your Rails2/3 routes with ease

Nowadays, more and more of our Rails applications have to be localized in order to support some languages and handle lots more of customers. Since Rails 2.2 we can do this easily through the awsome i18n gem.

That’s great, but with Rails, we are stuck with basics url in just one main language, and that’s really sucks in term of SEO performance.

So why not translate your Rails routes too ? You will gain more customers through SEO optimization and by the way you will make your clients happy !

Yesterday I have released a new version i18n_routing. It’s now fully compatible with both Rails2 and Rail3.

This gem give you the capability to translates easily all your routes through the shipped i18n gem :
There is nothing to change in your code: just add a few translations in your locale files !

Usage and examples

First of all, install the i18n_routing gem and add it to your Rails project. (Help if needed in the wiki)
Then, in your routes.rb file, just set wich routes you want to translate with the localized method:


Rails2 examples :

map.localized(I18n.available_locales, :verbose => true) do
  map.about, 'about', {:controller => 'contents', :action => :about}

  map.resources :users
  map.resource  :contact
end

Rails3 examples :

localized(I18n.available_locales, :verbose => true) do
  match 'about' => 'contents#about', :as  => :about

  resources :users
  resource  :contact
end

(I18n.available_locales is an array of all locales available given by the i18n gem (version > 0.3.5), you can pass a custom table like this if you prefer : ['en', 'fr', 'es'])

So, now our routes have been declared, just edit your locale files with translations :

fr:
  resource:
    contact: 'contactez-nous'
  named_routes_path:
    about: 'a-propos'
  routes:
    users:
      as: utilisateurs
      path_names:
        new: 'nouvel_utilisateur'
        edit: 'edition_utilisateur'

(This works with any i18n backend, just choose your favorite one and go ahead.)


Now it’s done, just watch the result in your irb console :

$ 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 > app.contact_path
=> "/contactez-nous"


Here we are ! You can now translate all your routes without any modification in your actual code !

All your routes are going to be automatically generated and recognized in the correct language depending on the current I18n.locale.

Keypoints and Features overview

  • 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

If you want to know more about i18n_routing, go to the i18n_routing github and have a look at the wiki.

  • Share/Bookmark

Comments (20)

Hello!
I installed your gem plugin yesterday to test it, now I have a little problem to use it but it is due to our routing here…so I just uninstall the gem to use it another time.

But now I have big issue in my application: all my locale about date format are now returning the error below, and I want to know if you have see or head about this:

missing interpolation argument in “%{count}-%m-%Y” ({:object=>”2010-05-17 14:17:33 UTC”} given)

Our application is stable and in production, so the code hasn’t change, except the trial of your gem. Is it possible than your has change anything like locale settings, on i18n version?

Thanks a lot for you help ;)

I’ve solved the problem: the gem i18n_routing install the gem i18n also, which now override the standard i18n include in rails, so it cause a misconduct in the way we use the locale.

I uninstall the gem, and now our internationalization are working.

It could be great that you has a little notice to your readme file…

Nice day to you!

Hi Jeff,

You were faster than me to answer to your issue !
Yes, it’s come from the shipped i18n version that come with rails 2.3 which is a little bit old now.

I will add a little notice on the readme ;)
Thanks for the feedback !

Hi,

very nice gem!

It looks as if nesting resources inside a singleton resource is not working yet: http://github.com/mixr/i18n_routing/commit/ec0f6f60375a64d0ac974a6a8680b7eeaef4a6ca

the routes look like the following: fr_foo_fr_bars

Greetings!

Hi,

I have just fixed this issue and released by the way version 0.3.3
Just do a gem update and this should works.
Thanks for the feedback !

I have been banging my head for ages : should I put the language in the subdomain or in the url first (and rewrite all my link_to), what’s best for SEO etc…

Using subdomain is better from the app standpoint but pretty bad for SEO (e.G. http://www.charliechaplin.com is pretty well established but fr.charliechaplin.com not at all….)

Finally problem solved!!!!

Anyway thanks a million for this gem, will be implementing it soon.

Bonjour Guillaume,

Pourrait-on localiser des routes un peu plus complexes tels que :
map.website_page ‘/website/:name/:type/index.html’, :controller => ‘website’, :action => ‘something’

D’avance merci pour la réponse et bravo pour ce gem fort sympathique :-)

Fro

Hi Charly,

I have not really religion for this, with the same domain and locale in urls: this can be confusing for search engine and not really easy when designing your app.

With different domains or subdomains, search engines will attribute a language to your site really more easily and that’s not a pain for your app conception.

That’s a difficult choice !


Bonsoir Fro,

Tu peux déjà traduire ce genre d’url ! Il te suffit de mettre en clé de traduction ‘/website/:name/:type/index.html’

fr:
named_routes_path:
‘/website/:name/:type/index.html’: ‘/site/:name/:type/index.html’

Cela devrait faire l’affaire :)

Ok ! Merci pour la réponse, je vais essayer ça…
Nice gem by the way ;-)

[...] Очень интересная статья о том как для разных локализаций вашего приложения можно генерировать свои route. Такой подход работавет в Rails 2 и 3. [...]

Bonjour Guillaume,

Ce gem fonctionne vraiment très bien et il est facile à implémenter. C’est top.

Petite question néanmoins: comment pourrais-je avoir ça :
‘/headlines/:id/:name’: “/actualites-:id-:name.html”

au lieu de ça :
‘/headlines/:id/:name’: “/actualites/:id/:name.html”

Remplacer les “/” par des “-” ?

D’avance merci pour ta réponse…

Bonjour Fro,

Je ne suis pas sur que tu puisses le faire, car c’est le routeur interne de Rails qui ne le supporte pas !

Hi there,

I have relied on the translate_routes plugin for my 2.3.x apps. As I’m moving to 3.0 I’m looking for a new route translation plugin as the translate_routes seems like it’s not going to be supported voor rails 3. I’m trying to mimic some of its features in your plugin, such as a locale prefix for each route that is being created. This would give me routes like;

/nl/opdrachten
/fr/travaux

I’m playing with a scope inside localized route block;

localized(['nl', 'fr'], :verbose => true) do
scope ‘/:locale’ do
resources :jobs
end
end

However I’m also looking for a way to get translated routes in other languages (in order to switch to pages). I’m trying to do this;

‘fr’) %>

Or

But in the first case it would print out;

/fr/opdrachten

And in the second case it would print out;

/nl/travaux

Could you please point me in the right direction on how to do this in your plugin (if it’s available of course)?

Thanks!

Stijn

Hello Stijn,

Sorry to get back to you really late, but long time I haven’t play with the plugin, and right now, I have no clue on how to handle this easily.

Your problem looks familiar to this one : http://groups.google.com/group/i18n-routing/browse_thread/thread/4d4745e9e798d519
Isn’it ?

Stijn,

I’ve made a similar plugin called translate_routes for Rails 3. It’s not as powerful as i18n_routing, but is really simple to install: http://github.com/josei/translate_routes

Sorry, wrong URL :) The right one: http://github.com/josei/localized_routes

Wohoo!

This plugin is awsome!
Thanks!

The translation lookup doesn’t properly work for me. I’m on Rails 3.1 and most of my translations are not found…

fr-CH:
resource:
video_albums: video_albums-fr-CH
named_routes_path:
advertise: advertise-fr-CH
routes:
users:
as: users-fr-CH
path_names:
new: new-fr-CH

ruby-1.9.2-p290 > I18n.locale = :’fr-CH’
=> :”fr-CH”
ruby-1.9.2-p290 > app.users_path
=> “/users-fr-CH”
ruby-1.9.2-p290 > app.video_albums_path
=> “/video_albums”
ruby-1.9.2-p290 > app.advertise_path
=> “/advertise”
ruby-1.9.2-p290 > app.new_user_path
=> “/users-fr-CH/new”
ruby-1.9.2-p290 >

Oops, the indentation in my post got lost and also “resource:” should be plural “resources:” See also here: http://groups.google.com/group/i18n-routing/browse_thread/thread/10c6517dce3804dd?pli=1

Great Work man.
Thanks for your GEM and this little tutorial. I just integrated it into your Plattform at http://resourcearea.cpu-24-7.com/fr/ .
So this helps! (Right now it’s not on the productive system yet, but already on staging and dev system)

So, thanks again, it’s a valuable contribution to Rails!
greetings danou

Write a comment