From 0cbeda84144f183f93eec45ea631e38a8a833961 Mon Sep 17 00:00:00 2001 From: scossar Date: Mon, 14 Mar 2016 16:18:19 -0700 Subject: [PATCH] add site setting for setting locale from header --- app/controllers/application_controller.rb | 2 +- config/locales/server.en.yml | 1 + config/site_settings.yml | 3 +++ .../allow_user_locale_enabled_validator.rb | 18 ++++++++++++++++++ .../controllers/application_controller_spec.rb | 5 +++-- 5 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 lib/validators/allow_user_locale_enabled_validator.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 8a09c8fbee7..c4ad643c746 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 7831d5e7463..5396a43a131 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -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" diff --git a/config/site_settings.yml b/config/site_settings.yml index 7247038e0ef..fe5f85ba999 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -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 diff --git a/lib/validators/allow_user_locale_enabled_validator.rb b/lib/validators/allow_user_locale_enabled_validator.rb new file mode 100644 index 00000000000..9f36c32d570 --- /dev/null +++ b/lib/validators/allow_user_locale_enabled_validator.rb @@ -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 \ No newline at end of file diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index 49ddc8f8145..84e770e4393 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -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")