2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-03 23:54:20 +08:00
discourse/app/models/locale_site_setting.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

69 lines
1.8 KiB
Ruby

# frozen_string_literal: true
class LocaleSiteSetting < EnumSiteSetting
def self.translate_names?
true
end
def self.valid_value?(val)
val.split("|").all? { |v| supported_locales.include?(v) }
end
def self.values
supported_locales.map do |locale|
lang = language_names[locale] || language_names[locale.split("_").first]
native_name = lang&.dig("nativeName")
{ native_name:, value: locale, name: "languages.#{locale}.name" }
end
end
@lock = Mutex.new
def self.language_names
return @language_names if @language_names
@lock.synchronize do
@language_names ||=
begin
names = YAML.safe_load(File.read(File.join(Rails.root, "config", "locales", "names.yml")))
DiscoursePluginRegistry.locales.each do |locale, options|
if !names.key?(locale) && options[:name] && options[:nativeName]
names[locale] = { "name" => options[:name], "nativeName" => options[:nativeName] }
end
end
names
end
end
end
def self.supported_locales
@lock.synchronize do
@supported_locales ||=
begin
locales =
Dir
.glob(File.join(Rails.root, "config", "locales", "client.*.yml"))
.map { |x| x.split(".")[-2] }
locales += DiscoursePluginRegistry.locales.keys
locales.uniq.sort
end
end
end
def self.reset!
@lock.synchronize { @values = @language_names = @supported_locales = nil }
end
FALLBACKS = { en_GB: :en }
def self.fallback_locale(locale)
fallback_locale = FALLBACKS[locale.to_sym]
return fallback_locale if fallback_locale
plugin_locale = DiscoursePluginRegistry.locales[locale.to_s]
plugin_locale ? plugin_locale[:fallbackLocale]&.to_sym : nil
end
end