File tree Expand file tree Collapse file tree 4 files changed +47
-6
lines changed Expand file tree Collapse file tree 4 files changed +47
-6
lines changed Original file line number Diff line number Diff line change @@ -1737,6 +1737,7 @@ en:
17371737 allow_user_locale : " Allow users to choose their own language interface preference"
17381738 set_locale_from_accept_language_header : " set interface language for anonymous users from their web browser's language headers"
17391739 set_locale_from_cookie : " Allows setting an anonymous user's locale via the 'locale' browser cookie"
1740+ set_locale_from_param : " Allows setting an anonymous user's locale via the 'lang' URL param, e.g. ?lang=es"
17401741 support_mixed_text_direction : " Support mixed left-to-right and right-to-left text directions."
17411742 min_post_length : " Minimum allowed post length in characters (excluding personal messages)"
17421743 min_first_post_length : " Minimum allowed first post (topic body) length (excluding personal messages)"
Original file line number Diff line number Diff line change @@ -154,6 +154,10 @@ basic:
154154 default : false
155155 client : true
156156 validator : " AllowUserLocaleEnabledValidator"
157+ set_locale_from_param :
158+ default : false
159+ client : true
160+ validator : " AllowUserLocaleEnabledValidator"
157161 support_mixed_text_direction :
158162 client : true
159163 default : false
Original file line number Diff line number Diff line change @@ -1215,13 +1215,11 @@ def self.clear_all_theme_cache!
12151215 end
12161216
12171217 def self . anonymous_locale ( request )
1218- locale =
1219- HttpLanguageParser . parse ( request . cookies [ "locale" ] ) if SiteSetting . set_locale_from_cookie
1218+ locale = request . params [ "lang" ] if SiteSetting . set_locale_from_param
1219+ locale ||= request . cookies [ "locale" ] if SiteSetting . set_locale_from_cookie
12201220 locale ||=
1221- HttpLanguageParser . parse (
1222- request . env [ "HTTP_ACCEPT_LANGUAGE" ] ,
1223- ) if SiteSetting . set_locale_from_accept_language_header
1224- locale
1221+ request . env [ "HTTP_ACCEPT_LANGUAGE" ] if SiteSetting . set_locale_from_accept_language_header
1222+ HttpLanguageParser . parse ( locale )
12251223 end
12261224
12271225 # For test environment only
Original file line number Diff line number Diff line change @@ -1126,6 +1126,44 @@ def locale_scripts(body)
11261126 end
11271127 end
11281128 end
1129+
1130+ context "with set_locale_from_param enabled" do
1131+ context "when param locale differs from default locale" do
1132+ before do
1133+ SiteSetting . allow_user_locale = true
1134+ SiteSetting . set_locale_from_param = true
1135+ SiteSetting . default_locale = "en"
1136+ end
1137+
1138+ context "with an anonymous user" do
1139+ it "uses the locale from the param" do
1140+ get "/latest?lang=es"
1141+ expect ( response . status ) . to eq ( 200 )
1142+ expect ( locale_scripts ( response . body ) ) . to contain_exactly ( "/assets/locales/es.js" )
1143+ expect ( I18n . locale . to_s ) . to eq ( SiteSettings ::DefaultsProvider ::DEFAULT_LOCALE ) # doesn't leak after requests
1144+ end
1145+ end
1146+
1147+ context "when the preferred locale includes a region" do
1148+ it "returns the locale and region separated by an underscore" do
1149+ get "/latest?lang=zh-CN"
1150+ expect ( response . status ) . to eq ( 200 )
1151+ expect ( locale_scripts ( response . body ) ) . to contain_exactly ( "/assets/locales/zh_CN.js" )
1152+ end
1153+ end
1154+ end
1155+
1156+ context "when locale param is not set" do
1157+ it "uses the site default locale" do
1158+ SiteSetting . allow_user_locale = true
1159+ SiteSetting . default_locale = "en"
1160+
1161+ get "/latest"
1162+ expect ( response . status ) . to eq ( 200 )
1163+ expect ( locale_scripts ( response . body ) ) . to contain_exactly ( "/assets/locales/en.js" )
1164+ end
1165+ end
1166+ end
11291167 end
11301168
11311169 describe "vary header" do
You can’t perform that action at this time.
0 commit comments