diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6e5f80e1e..26a14fd45 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -13,7 +13,7 @@ class ApplicationController < ActionController::Base layout :layout_by_resource 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 @@ -154,18 +154,18 @@ class ApplicationController < ActionController::Base def set_locale # update session if passed session[:locale] = params[:locale] if params[:locale] - - # set locale based on session or default - begin - # 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]) - I18n.locale = I18n.default_locale - else - I18n.locale = session[:locale] - end - rescue - I18n.locale = I18n.default_locale - end + browser_locale = condition = nil + # check if locale is valid for non site pages + if LIST[:forbidden_item_names].include?(env['PATH_INFO'].split('/')[1].to_s) + condition = VALID_LOCALES.include?(session[:locale]) + elsif session[:locale] + condition = @site_in_use_locales.include?(session[:locale]) + else + browser_locale = request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first rescue nil + condition = @site_in_use_locales.include?(browser_locale) + end + session[:locale] = condition ? (browser_locale || session[:locale]) : I18n.default_locale.to_s + I18n.locale = session[:locale].to_sym end # Set the site variables