问题
I've spent the last few hours trying everything possible to make this work, googled, redesigned, tested, etc. - but somehow doesn't get it working.
ok, I would like to set the I18n.locale - fairly simple.
Basically I implemented what is written on the guide here:
http://guides.rubyonrails.org/i18n.html
Application_controller.rb
before_action :set_locale
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
In fact mine looks like this:
class ApplicationController < ActionController::Base
logger.info "Point A..."
before_action :set_locale
def set_locale
logger.info "Point B..."
I18n.locale = extract_locale_from_accept_language_header
end
private
def extract_locale_from_accept_language_header
....
This Problem is that the locale gets set before I can set the locale, when first accessing the page. I would like to read browser settings of user first, if no locale is yet set. To give the user a chance to get to their localized site. If they change the locale subsequently (change language - fine, then the locale is set and that's what they are using until they decide to switch again).
I placed two lines in the code above and this is the output:
Point A...
Processing by StaticPagesController#home as HTML
Parameters: {"locale"=>"en"}
Point B...
I tried the following: - removed default locale in application.rb: #config.i18n.default_locale = :en - disabled all gems that could possible interfere (restarted the server each time)
Any help would be appreciated. Thanks, G
回答1:
Could you try and see if this works for you?
def set_locale
@locale = extract_locale_from_accept_language_header
I18n.locale = @locale
end
UPDATE:
After a lot of comments and the logs you paste it looks like you are getting the locale via routes. This is why you see in the log locale: en . But what you actually want to do is different, set the locale based on the headers and not the url. Remove the locale references from routes files and see if that works for you.
来源:https://stackoverflow.com/questions/23164970/locale-not-set-in-application-controller