Set I18n from browser

This commit is contained in:
chris 2013-02-26 20:14:03 +08:00
parent 6abf53f34d
commit 8374bc8d12
1 changed files with 13 additions and 13 deletions

View File

@ -13,7 +13,7 @@ class ApplicationController < ActionController::Base
layout :layout_by_resource layout :layout_by_resource
helper :admin, :orbit_form helper :admin, :orbit_form
before_filter :set_locale, :set_site, :prepare_for_mobile before_filter :set_site, :set_locale, :prepare_for_mobile
helper_attr :site_valid_locales helper_attr :site_valid_locales
@ -154,18 +154,18 @@ class ApplicationController < ActionController::Base
def set_locale def set_locale
# update session if passed # update session if passed
session[:locale] = params[:locale] if params[:locale] session[:locale] = params[:locale] if params[:locale]
browser_locale = condition = nil
# set locale based on session or default
begin
# check if locale is valid for non site pages # check if locale is valid for non site pages
if LIST[:forbidden_item_names].include?(env['PATH_INFO'].split('/')[1].to_s) && !VALID_LOCALES.include?(session[:locale]) if LIST[:forbidden_item_names].include?(env['PATH_INFO'].split('/')[1].to_s)
I18n.locale = I18n.default_locale condition = VALID_LOCALES.include?(session[:locale])
elsif session[:locale]
condition = @site_in_use_locales.include?(session[:locale])
else else
I18n.locale = session[:locale] browser_locale = request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first rescue nil
end condition = @site_in_use_locales.include?(browser_locale)
rescue
I18n.locale = I18n.default_locale
end end
session[:locale] = condition ? (browser_locale || session[:locale]) : I18n.default_locale.to_s
I18n.locale = session[:locale].to_sym
end end
# Set the site variables # Set the site variables