set locale for anonymous from header

set locale on signup

update spec

add locale option
This commit is contained in:
scossar
2016-02-06 11:49:39 -08:00
parent f887363e83
commit 0a396583ed
8 changed files with 146 additions and 18 deletions

View File

@ -164,7 +164,15 @@ class ApplicationController < ActionController::Base
end
def set_locale
I18n.locale = current_user.try(:effective_locale) || SiteSetting.default_locale
if !current_user
if SiteSetting.allow_user_locale
I18n.locale = locale_from_header
else
I18n.locale = SiteSetting.default_locale
end
else
I18n.locale = current_user.effective_locale
end
I18n.ensure_all_loaded!
end
@ -309,6 +317,18 @@ class ApplicationController < ActionController::Base
private
def locale_from_header
begin
# Rails I18n uses underscores between the locale and the region; the request
# headers use hyphens.
available_locales = I18n.available_locales.map { |locale| locale.to_s.gsub(/_/, '-') }
http_accept_language.language_region_compatible_from(available_locales).gsub(/-/, '_')
rescue
# If Accept-Language headers are not set.
I18n.default_locale
end
end
def preload_anonymous_data
store_preloaded("site", Site.json_for(guardian))
store_preloaded("siteSettings", SiteSetting.client_settings_json)