0

I tried to implement 'change language' feature at my app. I've looked through official guide http://guides.rubyonrails.org/i18n.html and everything was clear for me. Unfortunatly I dont know how to fix one issue. For example when I am accessing login form through link /en/login everything is fine till I click on submit form.

After redirection param[:locale] is not passed as expected and I am getting default locales. Is there any "Rails-way" soultion to redirect with previously chosen locales? I guess that it is possible to pass every time param to redirect_to but its kinda problematic to do on every controller.

My routes:

    scope "(:locale)", :locale => /pl|en/ do
      devise_for :users
      resources :pages
      get "/login"    => "sessions#new"
      post "/users/sign_in"    => "sessions#create"
      delete "/users/sign_out" => "sessions#destroy"
      root :to => "pages#index"
      resources :websites
    end
      match '/:locale' => 'pages#index'

App controller:

    class ApplicationController < ActionController::Base
      protect_from_forgery
      before_filter :set_locale

      def set_locale
         I18n.locale = params[:locale] || I18n.default_locale
      end

    end
Arti
  • 407
  • 5
  • 15
  • there is something wrong with your routes. :-) I have devise and I18n and works like a charm. I would put `match '/:locale' => 'pages#index'` after the `scope`. As it said in the guide, `Do take special care about the order of your routes, so this route declaration does not “eat” other ones. (You may want to add it directly before the root :to declaration.)` –  Feb 24 '12 at 14:29
  • @marcolinux, I corrected routes (please look above), no effect :( – Arti Feb 24 '12 at 14:45
  • do you have in your application controller `def default_url_options(options={}) { :locale => I18n.locale } end` (in the same guide you used). it will redirect you after the signin to localhost:3000/?locale=en (or what is the locale you used for signin) –  Feb 24 '12 at 15:09
  • Thanks marcolinux! Exactly that was causing this mess. – Arti Feb 24 '12 at 17:20
  • glad to hear. For everyone benefit (as the comment are not searchables), I will format it as answer. –  Feb 25 '12 at 04:28

2 Answers2

2

as the same guide is recommending, you can add

class ApplicationController < ActionController::Base
def default_url_options(options={}) 
    { :locale => I18n.locale } 
end

It will redirect your user to the proper localised root page

localhost:3000/?locale=pl
localhost:3000/?locale=en
....
1

Try this

before_filter :check_for_previous_locale_in_cookie

def check_for_previous_locale_in_cookie       
      I18n.locale = cookies[:last_seen_locale].blank? ? cookies[:last_seen_locale] : (cookies[:last_seen_locale] = params[:locale] || I18n.default_locale )        
end
Jak
  • 2,893
  • 3
  • 19
  • 33
  • I thought about cookie but I've read in guide: "You may be tempted to store the chosen locale in a session or a cookie. Do not do so. " Maybe its another way to achieve this? – Arti Feb 24 '12 at 14:08
  • Then add preferred locale for the user in db. I mean store it in db. – Jak Feb 24 '12 at 14:11