gab
gab

Reputation: 25

Rails internationalization

I am following the guide from http://guides.rubyonrails.org/i18n.html to add several translation to my page, using the "http://mysite/en/page" and everything is working fine, except that I am not being able to add the language switcher to my page.

I'd like to go to the same page I am showing but using the other language. For that, I presume I should use the same url, except for the :locale parameter. How to do that with a link_to call?

Really dummy question but I've just started in Rails and I am stuck with it.

Thanks!

Upvotes: 2

Views: 562

Answers (2)

Thilo
Thilo

Reputation: 17735

To link to the current page with a different locale, use url_for with only the locale parameter:

url_for(:locale => "de")

This will preserve any other URL parameters. So for example, to provide a link to the German version of the current page:

link_to "German version", url_for(:locale => "de")

Upvotes: 2

Anatoly
Anatoly

Reputation: 15530

you can use an extra method to setup user's local seamless (it will be stored in cookie/sessions) as

controllers/application.rb

before_filter :set_user_language
def set_user_language
  I18n.locale = current_user.language if logged_in?
end

please see more details at Railscasts.com

Upvotes: 1

Related Questions