2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-03 23:54:20 +08:00
discourse/spec/models/locale_site_setting_spec.rb
Natalie Tay 235c673fe8
FEATURE: Localize language names (#33790)
This PR adds localized language names to settings. The language names
are localized in the frontend, not the backend, due to setting
initialization complexity.

This change affects these areas:
- `SiteSetting.available_locales` 
- this "setting" is a lookup table to get language names. use
`languageNameLookup` service to get the name for a locale
- it returns an object that looks like the following, then gets
re-hydrated with client localized values when initializing the
`siteSettingService` in the frontend.
  ```
  [
{"native_name":"اللغة العربية","value":"ar","name":"languages.ar.name"},
    ...
  ]  
  ```

- `SiteSetting.default_locale` 
- this is a single-value `enum` setting that has always been hardcoded.
This caused quite an issue as it is not initialized the same way as
other site settings in the yml file. It has always relied on reading
directly from a `names.yml` file to load native language names, thus
bypassing the need for I18n to be initialized from the backend. A new
locale_enum type has been introduced for this setting, and any future
settings.
  
- `SiteSetting.content_localization_supported_locales` - this is a
`enum_list` setting,
  - enum_list is introduced, leveraging both `list` and `enum`
  
- theme translations

- site texts

- Wizard's default_locale choices 
- it was set up from the backend using `LocaleSiteSetting.value`. This
proved problematic, as a Japanese user would be getting the locales in
English because the values are initialized using English even without
memoization
- therefore we're now initializing the choices in the frontend using
`available_locales` as defined above
  
- content localization meta data
- post language in the composer, localization composer, post history
modal, post language tooltip, language switcher



/t/151409
2025-07-29 11:48:45 +08:00

124 lines
4.1 KiB
Ruby

# frozen_string_literal: true
RSpec.describe LocaleSiteSetting do
def core_locales
pattern = File.join(Rails.root, "config", "locales", "client.*.yml")
Dir.glob(pattern).map { |x| x.split(".")[-2] }
end
def native_locale_name(locale)
value = LocaleSiteSetting.values.find { |v| v[:value] == locale }
value[:native_name]
end
describe ".valid_value?" do
it "returns true for a locale that we have translations for" do
expect(LocaleSiteSetting.valid_value?("en")).to eq(true)
end
it "returns false for a locale that we do not have translations for" do
expect(LocaleSiteSetting.valid_value?("swedish-chef")).to eq(false)
end
end
describe ".values" do
it "returns all the locales that we have translations for" do
expect(LocaleSiteSetting.values.map { |x| x[:value] }).to include(*core_locales)
end
it "returns native names" do
expect(native_locale_name("de")).to eq("Deutsch")
expect(native_locale_name("zh_CN")).to eq("简体中文")
expect(native_locale_name("zh_TW")).to eq("繁體中文")
end
end
context "with locales from plugin" do
before do
DiscoursePluginRegistry.register_locale("foo", name: "Foo", nativeName: "Native Foo")
DiscoursePluginRegistry.register_locale("bar", name: "Bar", nativeName: "Native Bar")
DiscoursePluginRegistry.register_locale(
"de",
name: "Renamed German",
nativeName: "Native renamed German",
)
DiscoursePluginRegistry.register_locale(
"de_AT",
name: "German (Austria)",
nativeName: "Österreichisch",
fallbackLocale: "de",
)
DiscoursePluginRegistry.register_locale("tlh")
# Plugins normally register a locale before LocaleSiteSetting is initialized.
# That's not happening in tests, so we need to call reset!
LocaleSiteSetting.reset!
end
after do
DiscoursePluginRegistry.unregister_locale("foo")
DiscoursePluginRegistry.unregister_locale("bar")
DiscoursePluginRegistry.unregister_locale("de")
DiscoursePluginRegistry.unregister_locale("de_AT")
DiscoursePluginRegistry.unregister_locale("tlh")
LocaleSiteSetting.reset!
end
describe ".valid_value?" do
it "returns true for locales from core" do
expect(LocaleSiteSetting.valid_value?("en")).to eq(true)
expect(LocaleSiteSetting.valid_value?("de")).to eq(true)
expect(LocaleSiteSetting.valid_value?("en|de")).to eq(true)
end
it "returns true for locales added by plugins" do
expect(LocaleSiteSetting.valid_value?("foo")).to eq(true)
expect(LocaleSiteSetting.valid_value?("bar")).to eq(true)
end
end
describe ".values" do
it "returns native names added by plugin" do
expect(native_locale_name("foo")).to eq("Native Foo")
expect(native_locale_name("bar")).to eq("Native Bar")
end
it "does not allow plugins to override native names that exist in core" do
expect(native_locale_name("de")).to eq("Deutsch")
end
it "returns nothing when no nativeName is set" do
expect(native_locale_name("tlh")).to eq(nil)
end
end
describe ".fallback_locale" do
it "returns the fallback locale registered by plugin" do
expect(LocaleSiteSetting.fallback_locale("de_AT")).to eq(:de)
expect(LocaleSiteSetting.fallback_locale(:de_AT)).to eq(:de)
end
it "returns nothing when no fallback locale was registered" do
expect(LocaleSiteSetting.fallback_locale("foo")).to be_nil
end
it "returns English for English (UK)" do
expect(LocaleSiteSetting.fallback_locale("en_GB")).to eq(:en)
end
end
end
describe ".fallback_locale" do
it "returns English for English (UK)" do
expect(LocaleSiteSetting.fallback_locale("en_GB")).to eq(:en)
end
end
describe ".supported_locales" do
it "has a language name for each supported locale" do
LocaleSiteSetting.supported_locales.each do |locale|
expect(LocaleSiteSetting.language_names[locale]).to be_present
end
end
end
end