edgerunner
edgerunner

Reputation: 14983

How can I make the Rails 3 router localize URLs using localization files?

What I'd like to be able to do is:

in config/routes.rb

resources :posts

in config/locale/en.yml

en:
  resources:
    posts: "posts"
    new: "new"
    edit: "edit"

in config/locale/tr.yml

tr:
  resources:
    posts: "yazilar"
    new: "yeni"
    edit: "duzenle"

and get

I18n.locale = :en
edit_post_path(3) #=> /posts/3/edit

I18n.locale = :tr
edit_post_path(3) #=> /yazilar/3/duzenle

I'd also like Rails to match any of these routes anytime and pass the associated locale in the params hash such that when I navigate to /yazilar , the request should be routed to the posts#index action with the :tr locale in the params hash.

Any simple or complex way of doing that?

Upvotes: 3

Views: 2674

Answers (3)

Martin M
Martin M

Reputation: 8668

As the mentioned gems are no longer actively maintained, here are some more recent solutions:

both are successors of translate_routes

or a more powerfull (and complex) solution:

Upvotes: 2

edgerunner
edgerunner

Reputation: 14983

There is also the i18n_routing plugin by Guillaume Luccisano at http://github.com/kwi/i18n_routing that solves most of those problems however, it currently lacks translating the action names like ../new and ../edit. Guillaume says he will implement that "soon", though.

Update: Action name translating has been implemented in i18n_routing. Thanks Guillaume. :)

Upvotes: 7

marzagao
marzagao

Reputation: 3786

The translate_routes plugin by Raul Murciano provides some of the features you are asking for:

http://github.com/raul/translate_routes

It only works for Rails 2.3.x as far as I know though. But at least you can either get some ideas or even fork the plugin and make it work on Rails 3 yourself.

Upvotes: 0

Related Questions