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?