Skip to content

A platform for community discussion. Free, open, simple.

DEV: Allow theme setting enum choices to be translated (#37096)

Martin Brennan7098a90c2ae4
diff --git a/app/serializers/theme_settings_serializer.rb b/app/serializers/theme_settings_serializer.rb
index b7007859777..28b2512c934 100644
--- a/app/serializers/theme_settings_serializer.rb
+++ b/app/serializers/theme_settings_serializer.rb
@@ -50,7 +50,13 @@ class ThemeSettingsSerializer < ApplicationSerializer
end
def valid_values
- object.choices
+ 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?
@@ -92,4 +98,20 @@ class ThemeSettingsSerializer < ApplicationSerializer
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
diff --git a/spec/serializers/theme_settings_serializer_spec.rb b/spec/serializers/theme_settings_serializer_spec.rb
index 39fcc1a9481..5f5a6d976f4 100644
--- a/spec/serializers/theme_settings_serializer_spec.rb
+++ b/spec/serializers/theme_settings_serializer_spec.rb
@@ -17,4 +17,125 @@ RSpec.describe ThemeSettingsSerializer do
expect(payload[:theme_settings][:objects_schema][:name]).to eq("section")
end
end
+
+ describe "#valid_values" do
+ fab!(:theme_with_enum, :theme)
+
+ before do
+ theme_with_enum.set_field(target: :settings, name: "yaml", value: <<~YAML)
+ my_enum:
+ type: enum
+ default: option_a
+ choices:
+ - option_a
+ - option_b
+ - option_c
+ YAML
+ theme_with_enum.save!
+ end
+
+ it "returns choice labels from locale files when defined" do
+ ThemeField.create!(
+ theme_id: theme_with_enum.id,
+ name: "en",
+ type_id: ThemeField.types[:yaml],
+ target_id: Theme.targets[:translations],
+ value: <<~YAML,
+ en:
+ theme_metadata:
+ settings:
+ my_enum:
+ choices:
+ option_a: "Option A Label"
+ option_b: "Option B Label"
+ option_c: "Option C Label"
+ YAML
+ )
+
+ payload =
+ ThemeSettingsSerializer.new(theme_with_enum.reload.settings[:my_enum]).as_json[
+ :theme_settings
+ ]
+
+ expect(payload[:valid_values]).to eq(
+ [
+ { name: "Option A Label", value: "option_a" },
+ { name: "Option B Label", value: "option_b" },
+ { name: "Option C Label", value: "option_c" },
+ ],
+ )
+ end
+
+ it "returns raw values when no locale labels are defined" do
+ payload =
+ ThemeSettingsSerializer.new(theme_with_enum.settings[:my_enum]).as_json[:theme_settings]
+
+ expect(payload[:valid_values]).to eq(%w[option_a option_b option_c])
+ end
+
+ it "handles mixed case where some choices have labels and some don't" do
+ ThemeField.create!(
+ theme_id: theme_with_enum.id,
+ name: "en",
+ type_id: ThemeField.types[:yaml],
+ target_id: Theme.targets[:translations],
+ value: <<~YAML,
+ en:
+ theme_metadata:
+ settings:
+ my_enum:
+ choices:
+ option_a: "Option A Label"
+ YAML
+ )
+
+ payload =
+ ThemeSettingsSerializer.new(theme_with_enum.reload.settings[:my_enum]).as_json[
+ :theme_settings
+ ]
+
+ expect(payload[:valid_values]).to eq(
+ [{ name: "Option A Label", value: "option_a" }, "option_b", "option_c"],
+ )
+ end
+
+ it "handles integer choice values with string locale keys" do
+ theme_with_enum.set_field(target: :settings, name: "yaml", value: <<~YAML)
+ int_enum:
+ type: enum
+ default: 1
+ choices:
+ - 1
+ - 2
+ - 3
+ YAML
+ theme_with_enum.save!
+
+ ThemeField.create!(
+ theme_id: theme_with_enum.id,
+ name: "en",
+ type_id: ThemeField.types[:yaml],
+ target_id: Theme.targets[:translations],
+ value: <<~YAML,
+ en:
+ theme_metadata:
+ settings:
+ int_enum:
+ choices:
+ "1": "One"
+ "2": "Two"
+ "3": "Three"
+ YAML
+ )
+
+ payload =
+ ThemeSettingsSerializer.new(theme_with_enum.reload.settings[:int_enum]).as_json[
+ :theme_settings
+ ]
+
+ expect(payload[:valid_values]).to eq(
+ [{ name: "One", value: 1 }, { name: "Two", value: 2 }, { name: "Three", value: 3 }],
+ )
+ end
+ end
end