0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-11 02:59:07 +08:00
discourse/spec/services/site_setting_localizations/about_config/update_spec.rb
Natalie Tay af11f8992c
FEATURE: Localizable /about page fields via its settings page (#41123)
The /about page (unlike /guidelines or /tos) is not backed by a topic,
but instead via a bunch of site settings.

If it were backed by a topic, localization would be easy as it can just
use TopicLocalizations. We also can't swap it out to use a topic, since
there are so many fields on the about page that can't be safely
contained in a topic.

----------

This PR adds SiteSettingLocalization, allowing new attributes in site
settings config for localization.

For now this is wired into the About page settings flow:
  - admins can select a non-default locale on /admin/config/about
  - only translatable About fields are shown for that locale
  - localized About values are used on /about and /about.json
- company_url can be set manually per locale, but is excluded from
automatic translation
- ~~AI backfill can translate eligible site setting localizations~~ no
backfill (added in
https://github.com/discourse/discourse/pull/41123/commits/386c655d2f44aca54440c069bd06ea8007793d84,
removed in
https://github.com/discourse/discourse/pull/41123/commits/6f8ec9cc43dca4195bda638b1ce980eb68a59941.
about page has very non automa-ble fields

This intentionally keeps the scope narrow. It does not make every site
setting localizable, and any other setting can be localized by adding to
that allowlist.

Some screenshots

|page | 📸  | 
|--|--|
| eng setting | <img width="1278" height="871" alt="Screenshot
2026-06-23 at 11 00 34 PM"
src="https://github.com/user-attachments/assets/59431464-fb05-41d9-8c4f-fd9aae7fb45f"
/>
| ja setting | <img width="1278" height="871" alt="Screenshot 2026-06-23
at 11 01 20 PM"
src="https://github.com/user-attachments/assets/73cd2878-f59a-4dc4-a76d-2f46b74a6eed"
/>
| ja anon about page | <img width="1278" height="871" alt="Screenshot
2026-06-23 at 11 01 48 PM"
src="https://github.com/user-attachments/assets/02464a5f-9ca0-4519-9521-be41498c1967"
/>
2026-06-26 20:41:08 +08:00

155 lines
4.6 KiB
Ruby
Vendored

# frozen_string_literal: true
describe SiteSettingLocalizations::AboutConfig::Update do
describe described_class::Contract, type: :model do
it { is_expected.to validate_presence_of(:locale) }
end
describe ".call" do
subject(:result) { described_class.call(params:, **dependencies) }
fab!(:admin)
fab!(:user)
let(:params) { { locale:, general_settings: } }
let(:dependencies) { { guardian: } }
let(:guardian) { admin.guardian }
let(:locale) { "ja" }
let(:general_settings) { { name: "日本語タイトル" } }
before do
SiteSetting.content_localization_enabled = true
SiteSetting.content_localization_supported_locales = "ja|pt_BR"
end
context "when locale is blank" do
let(:locale) { nil }
it { is_expected.to fail_a_contract }
end
context "when the user is not an admin" do
let(:guardian) { user.guardian }
it { is_expected.to fail_a_policy(:can_localize_site_settings) }
end
context "when content localization is disabled" do
before { SiteSetting.content_localization_enabled = false }
it { is_expected.to fail_a_policy(:can_localize_site_settings) }
end
context "with an unsupported locale" do
let(:locale) { "de" }
it { is_expected.to fail_a_policy(:locale_is_supported) }
end
context "when everything is ok" do
let(:general_settings) do
{ name: "日本語タイトル", summary: "日本語の説明", extended_description: "日本語の **詳細** 説明" }
end
before do
SiteSettingLocalization.create!(setting_name: "title", locale: "ja", value: "古いタイトル")
end
it { is_expected.to run_successfully }
it "creates and updates localized about settings" do
expect { result }.to change {
SiteSettingLocalization.find_by(setting_name: "title", locale: "ja")&.value
}.from("古いタイトル").to("日本語タイトル").and change {
SiteSettingLocalization.exists?(setting_name: "site_description", locale: "ja")
}.from(false).to(true)
expect(
SiteSettingLocalization.find_by(
setting_name: "extended_site_description",
locale: "ja",
).cooked,
).to include("<strong>詳細</strong>")
end
it "logs the changed setting names" do
expect { result }.to change { UserHistory.count }.by(1)
log_record = UserHistory.where(action: UserHistory.actions[:custom_staff]).last
aggregate_failures do
expect(log_record.custom_type).to eq("update_site_setting_localizations")
expect(log_record.details).to include("locale: ja")
expect(log_record.details).to include(
"setting_names: extended_site_description|site_description|title",
)
end
end
it "returns the updated payload" do
expect(result.payload[:localizations].dig("title", :value)).to eq("日本語タイトル")
end
end
context "with a blank value" do
let(:general_settings) { { summary: "" } }
before do
SiteSettingLocalization.create!(
setting_name: "site_description",
locale: "ja",
value: "日本語の説明",
)
end
it "removes the localized setting" do
expect { result }.to change {
SiteSettingLocalization.exists?(setting_name: "site_description", locale: "ja")
}.from(true).to(false)
end
end
context "with omitted fields" do
let(:general_settings) { { name: "日本語タイトル" } }
before do
SiteSettingLocalization.create!(
setting_name: "site_description",
locale: "ja",
value: "既存の説明",
)
end
it "does not change existing rows for omitted fields" do
expect { result }.not_to change {
SiteSettingLocalization.find_by(setting_name: "site_description", locale: "ja")&.value
}
end
end
context "with non-general sections" do
let(:params) do
{
locale:,
general_settings: {
name: "日本語タイトル",
},
contact_information: {
contact_email: "ignored@example.com",
},
your_organization: {
company_name: "日本語会社",
},
}
end
it "ignores unsupported about sections" do
expect { result }.to change {
SiteSettingLocalization.exists?(setting_name: "title", locale: "ja")
}.from(false).to(true)
expect(SiteSettingLocalization.where(locale: "ja").pluck(:setting_name)).to eq(["title"])
end
end
end
end