views:

180

answers:

3

I have several resources and I want to somehow have a :lang parameter in the header. So /en/posts/ would direct to posts_controller/index with params[:lang] == en.

How is this usually done? It looks ugly to have to add ?lang=en on every link and feel very un-railsy.

I think you know what I'm talking about even if I'm not very clear, what is the norm here?

+1  A: 

One way of doing it would be:

map.resources :yourresources, :path_prefix => '/:lang'

You can also add language parameter filter:

map.resources :yourresources, :path_prefix => '/:lang', :lang => /(en|de|jp)/
Milan Novota
How would i then go about creating links with the resource?Would i have to do link_to myresource_path(:lang => "en") or could i set some sort of standard language to use when no argument is given?
ique
Yes, you'd have to pass some language into the link helper. However, you can set a default language in the environment.rb (as a constant) and then create a method (say get_language) in Application controller that will return this default value if params[:lang] is not set.
Milan Novota
A: 

Seems like the newest feature scheduled for Rails 2.3 is right up your alley: http://afreshcup.com/2009/02/01/rails-23-localized-views/

Matt Darby
While cool, this doesn't address routing...
Orion Edwards
+2  A: 

Aren't you supposed to eschew routes entirely, and set language based on the browser's HTTP Accept-Language header? See Here.

I'd imagine you should give your users an option to change their language, and then store that value in a cookie, which could then be pulled out in a before_filter.

Why do this? Well, "HTTP best practice" states that each resource should have one URI. An article is still the same article, whether it's in spanish or english, so should have the same URI. site.com/en/article and site.com/es/article are clearly not the same URI, which violates this principle

Orion Edwards
But not beeing able to link the correct language would cause people who visit a page to think they've come to the wrong page? If you speak english and get a link from an english friend and then come to a page that's filled with swedish. One would be quite confused?
ique