discourse/lib/wizard/builder.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

142 lines
3.9 KiB
Ruby

# frozen_string_literal: true
class Wizard
class Builder
WIZARD_FONTS = %w[lato inter montserrat open_sans poppins roboto]
def initialize(user)
@wizard = Wizard.new(user)
end
def build
return @wizard unless SiteSetting.wizard_enabled? && @wizard.user.try(:staff?)
append_introduction_step
append_privacy_step
append_ready_step
DiscourseEvent.trigger(:build_wizard, @wizard)
@wizard
end
protected
def append_introduction_step
@wizard.append_step("introduction") do |step|
step.emoji = "wave"
step.description_vars = { base_path: Discourse.base_path }
step.add_field(
id: "title",
type: "text",
required: true,
value: SiteSetting.title == SiteSetting.defaults[:title] ? "" : SiteSetting.title,
)
step.add_field(
id: "site_description",
type: "text",
required: false,
value: SiteSetting.site_description,
)
step.add_field(
id: "default_locale",
type: "dropdown",
required: false,
value: SiteSetting.default_locale,
)
step.on_update do |updater|
updater.ensure_changed(:title)
updater.apply_settings(:title, :site_description) if updater.errors.blank?
old_locale = SiteSetting.default_locale
updater.apply_setting(:default_locale)
if old_locale != updater.fields[:default_locale]
Scheduler::Defer.later "Reseed" do
SeedData::Categories.with_default_locale.update(skip_changed: true)
SeedData::Topics.with_default_locale.update(skip_changed: true)
end
updater.refresh_required = true
end
end
end
end
def append_privacy_step
@wizard.append_step("privacy") do |step|
step.emoji = "hugs"
step.add_field(
id: "login_required",
type: "radio",
value: SiteSetting.login_required ? "private" : "public",
) do |field|
field.add_choice("public")
field.add_choice("private")
end
step.add_field(
id: "invite_only",
type: "radio",
value: SiteSetting.invite_only ? "invite_only" : "sign_up",
) do |field|
field.add_choice("sign_up")
field.add_choice("invite_only")
end
step.add_field(
id: "must_approve_users",
type: "radio",
value: SiteSetting.must_approve_users ? "yes" : "no",
) do |field|
field.add_choice("no")
field.add_choice("yes")
end
step.on_update do |updater|
updater.update_setting(:login_required, updater.fields[:login_required] == "private")
updater.update_setting(:invite_only, updater.fields[:invite_only] == "invite_only")
updater.update_setting(:must_approve_users, updater.fields[:must_approve_users] == "yes")
end
end
end
def append_ready_step
@wizard.append_step("ready") do |step|
# no form on this page, just info.
step.emoji = "rocket"
end
end
def replace_setting_value(updater, raw, field_name)
old_value = SiteSetting.get(field_name)
old_value = field_name if old_value.blank?
new_value = updater.fields[field_name.to_sym]
new_value = field_name if new_value.blank?
raw.gsub!("<ins>#{old_value}</ins>", new_value) || raw.gsub!(old_value, new_value)
end
def reserved_usernames
@reserved_usernames ||= SiteSetting.defaults[:reserved_usernames].split("|")
end
def update_tos
tos_post = Post.find_by(topic_id: SiteSetting.tos_topic_id, post_number: 1)
if tos_post.present?
raw = tos_post.raw.dup
yield(raw)
revisor = PostRevisor.new(tos_post)
revisor.revise!(@wizard.user, raw: raw)
end
end
end
end