FEATURE: Allow setting locale from 'lang' param (#30952)

As we start to translate more pages, we'll need a way for other sites to
link back to our translated topics.

This commit gives us the ability to use the "lang" URL param to define what
language a site should be in.

Related: https://github.com/discourse/discourse-translator/pull/199
This commit is contained in:
Natalie Tay
2025-01-24 11:53:13 +08:00
committed by GitHub
parent 5d4bb4b54e
commit 8d45755a06
4 changed files with 47 additions and 6 deletions

View File

@@ -1126,6 +1126,44 @@ RSpec.describe ApplicationController do
end
end
end
context "with set_locale_from_param enabled" do
context "when param locale differs from default locale" do
before do
SiteSetting.allow_user_locale = true
SiteSetting.set_locale_from_param = true
SiteSetting.default_locale = "en"
end
context "with an anonymous user" do
it "uses the locale from the param" do
get "/latest?lang=es"
expect(response.status).to eq(200)
expect(locale_scripts(response.body)).to contain_exactly("/assets/locales/es.js")
expect(I18n.locale.to_s).to eq(SiteSettings::DefaultsProvider::DEFAULT_LOCALE) # doesn't leak after requests
end
end
context "when the preferred locale includes a region" do
it "returns the locale and region separated by an underscore" do
get "/latest?lang=zh-CN"
expect(response.status).to eq(200)
expect(locale_scripts(response.body)).to contain_exactly("/assets/locales/zh_CN.js")
end
end
end
context "when locale param is not set" do
it "uses the site default locale" do
SiteSetting.allow_user_locale = true
SiteSetting.default_locale = "en"
get "/latest"
expect(response.status).to eq(200)
expect(locale_scripts(response.body)).to contain_exactly("/assets/locales/en.js")
end
end
end
end
describe "vary header" do