mirror of
https://github.com/discourse/discourse.git
synced 2026-03-03 23:54:20 +08:00
This commit adds support for translating enum choices in theme settings. The ThemeSettingsSerializer has been updated to include translations for enum choices based on the current locale. This brings theme settings more in line with what is possible with enum types for site settings. This will be used in discourse/discourse#36902 <img width="632" height="278" alt="image" src="https://github.com/user-attachments/assets/5f786cc0-2596-4d77-b701-db541ce401bb" />
117 lines
2.3 KiB
Ruby
117 lines
2.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class ThemeSettingsSerializer < ApplicationSerializer
|
|
attributes :setting,
|
|
:humanized_name,
|
|
:type,
|
|
:default,
|
|
:value,
|
|
:description,
|
|
:valid_values,
|
|
:list_type,
|
|
:textarea,
|
|
:json_schema,
|
|
:objects_schema
|
|
|
|
def setting
|
|
object.name
|
|
end
|
|
|
|
def humanized_name
|
|
SiteSetting.humanized_name(object.name)
|
|
end
|
|
|
|
def type
|
|
object.type_name
|
|
end
|
|
|
|
def default
|
|
object.default
|
|
end
|
|
|
|
def value
|
|
object.value
|
|
end
|
|
|
|
def description
|
|
description_regexp = /^theme_metadata\.settings\.#{setting}(\.description)?$/
|
|
|
|
locale_file_description =
|
|
object.theme.internal_translations.find { |t| t.key.match?(description_regexp) }&.value
|
|
|
|
resolved_description = locale_file_description || object.description
|
|
|
|
if resolved_description
|
|
catch(:exception) do
|
|
return I18n.interpolate(resolved_description, base_path: Discourse.base_path)
|
|
end
|
|
resolved_description
|
|
end
|
|
end
|
|
|
|
def valid_values
|
|
choices = object.choices
|
|
labels = choice_labels
|
|
|
|
choices.map do |choice|
|
|
label = labels[choice.to_s]
|
|
label ? { name: label, value: choice } : choice
|
|
end
|
|
end
|
|
|
|
def include_valid_values?
|
|
object.type == ThemeSetting.types[:enum]
|
|
end
|
|
|
|
def include_description?
|
|
description.present?
|
|
end
|
|
|
|
def list_type
|
|
object.list_type
|
|
end
|
|
|
|
def include_list_type?
|
|
object.type == ThemeSetting.types[:list]
|
|
end
|
|
|
|
def textarea
|
|
object.textarea
|
|
end
|
|
|
|
def include_textarea?
|
|
object.type == ThemeSetting.types[:string]
|
|
end
|
|
|
|
def objects_schema
|
|
object.schema
|
|
end
|
|
|
|
def include_objects_schema?
|
|
object.type == ThemeSetting.types[:objects]
|
|
end
|
|
|
|
def json_schema
|
|
object.json_schema
|
|
end
|
|
|
|
def include_json_schema?
|
|
object.type == ThemeSetting.types[:string] && object.json_schema.present?
|
|
end
|
|
|
|
private
|
|
|
|
def choice_labels
|
|
labels = {}
|
|
key_prefix = "theme_metadata.settings.#{setting}.choices."
|
|
|
|
object.theme.internal_translations.each do |translation|
|
|
if translation.key.start_with?(key_prefix)
|
|
choice_key = translation.key.delete_prefix(key_prefix)
|
|
labels[choice_key] = translation.value
|
|
end
|
|
end
|
|
|
|
labels
|
|
end
|
|
end
|