add site setting for setting locale from header

This commit is contained in:
scossar 2016-03-14 16:18:19 -07:00
parent 9fc9f029ea
commit 0cbeda8414
5 changed files with 26 additions and 3 deletions

View file

@ -165,7 +165,7 @@ class ApplicationController < ActionController::Base
def set_locale
if !current_user
if SiteSetting.allow_user_locale
if SiteSetting.set_locale_from_accept_language_header
I18n.locale = locale_from_header
else
I18n.locale = SiteSetting.default_locale

View file

@ -787,6 +787,7 @@ en:
delete_old_hidden_posts: "Auto-delete any hidden posts that stay hidden for more than 30 days."
default_locale: "The default language of this Discourse instance (ISO 639-1 Code)"
allow_user_locale: "Allow users to choose their own language interface preference"
set_locale_from_accept_language_header: "set interface language for anonymous users from their web browser's language headers."
min_post_length: "Minimum allowed post length in characters"
min_first_post_length: "Minimum allowed first post (topic body) length in characters"
min_private_message_post_length: "Minimum allowed post length in characters for messages"

View file

@ -64,6 +64,9 @@ basic:
allow_user_locale:
client: true
default: false
set_locale_from_accept_language_header:
default: false
validator: "AllowUserLocaleEnabledValidator"
suggested_topics:
client: true
default: 5

View file

@ -0,0 +1,18 @@
class AllowUserLocaleEnabledValidator
def initialize(opts={})
@opts = opts
end
def valid_value?(val)
# only validate when enabling setting locale from headers
return true if val == "f"
# ensure that allow_user_locale is enabled
SiteSetting.allow_user_locale
end
def error_message
"You must first enable 'allow user locale' before enabling this setting."
end
end

View file

@ -148,10 +148,11 @@ describe TopicsController do
end
end
context "allow_user_locale enabled" do
context "set_locale_from_accept_language_header enabled" do
context "accept-language header differs from default locale" do
before do
SiteSetting.stubs(:allow_user_locale).returns(true)
SiteSetting.stubs(:set_locale_from_accept_language_header).returns(true)
SiteSetting.stubs(:default_locale).returns("en")
set_accept_language("fr")
end
@ -178,7 +179,7 @@ describe TopicsController do
context "the preferred locale includes a region" do
it "returns the locale and region separated by an underscore" do
SiteSetting.stubs(:allow_user_locale).returns(true)
SiteSetting.stubs(:set_locale_from_accept_language_header).returns(true)
SiteSetting.stubs(:default_locale).returns("en")
set_accept_language("zh-CN")