0

I need to translate my site and I'm using I18n for this purpose. Using locale as URL param, I want to redirect users to their locale, which saved in their cookies.
Here is string from routes.rb which I'm using to redirect:

get "/path", to: redirect("/#{I18n.locale}/%{path}", status: 302), constraints: {path: /(?!(#{I18n.available_locales.join("|")})/)./}, format: false

And I'm using Rack middleware to get cookies and set I18n.locale before routes:
require 'i18n'

module Rack
  class Locale
    def initialize(app)
      @app = app
    end

    def call(env)
      request = ActionDispatch::Request.new(env)
      I18n.locale = request.cookies['locale'].to_sym if request.cookies['locale'].present? && request.params[:locale].nil?
      @app.call(env)
    end
  end
end

The problem is in routes.rb file: I18.locale is always set there to default locale, so there is no redirect to user's locale, but to the default locale.

Also, I've debugged middleware, and as I see, I18n.locale sets there successfully.

Any ideas how to set I18n.locale in routes.rb?

rado
  • 111
  • 1
  • 3
  • 12

1 Answers1

0

Is there a particular reason why you are using the locale as a part of your routes? If not I'd suggest you follow the Rails I18n guide which recommends to set the locale in a before_action hook which for your case then could fetch it from the cookie (code is quoted as is from the Rails guide):

before_action :set_locale

def set_locale
  I18n.locale = params[:locale] || I18n.default_locale
end
Jonas
  • 515
  • 4
  • 13
  • http://guides.rubyonrails.org/i18n.html#storing-the-locale-from-the-session-or-cookies Here is the main reason to set locale as part of url - I want to use links which will be looking the same, not depending from cookies. But also I don't want to cause trouble to my current clients, who set links from my site to their bookmarks, etc. – rado Jul 29 '16 at 05:26
  • I see. Then scroll a bit further down to 2.2.2, there is an explanation how to use a locale scope in the routing – Jonas Jul 29 '16 at 07:41
  • I haven't problem with 2.2.2, I have problem with redirects to current user locale, if user set not default locale. – rado Aug 01 '16 at 07:04