Skip to content

Commit

Permalink
[#4363] Fixed translations of the radio and selectboxes description o…
Browse files Browse the repository at this point in the history
…n frontend

In the formio components we use a dynamic method localize in order to
add the translated literals of the values of the components
(selectboxes, radio..). For this we were using only the label so the
description was added as well.

Backport-of: #4363
  • Loading branch information
vaszig authored and sergei-maertens committed Jun 14, 2024
1 parent 0f08271 commit 5211292
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/openforms/formio/components/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ def translate_options(
if not (translations := option.get("openForms", {}).get("translations")):
continue

translated_label = translations.get(language_code, {}).get("label", "")
if enabled and translated_label:
option["label"] = translated_label
if enabled:
translated_label = translations.get(language_code, {}).get("label", "")
translated_description = translations.get(language_code, {}).get(
"description", ""
)

if translated_label:
option["label"] = translated_label

if translated_description:
option["description"] = translated_description

# always clean up
del option["openForms"]["translations"] # type: ignore
15 changes: 13 additions & 2 deletions src/openforms/formio/tests/test_component_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,10 @@ def test_options_translated(self, lang_code, translation):
"label": "First option",
"openForms": {
"translations": {
lang_code: {"label": translation},
lang_code: {
"label": translation,
"description": translation,
},
}
},
}
Expand All @@ -565,8 +568,11 @@ def test_options_translated(self, lang_code, translation):
assert "values" in component
opt1 = component["values"][0]
assert "label" in opt1
assert "description" in opt1
assert "openForms" in opt1

self.assertEqual(opt1["label"], translation)
self.assertEqual(opt1["description"], translation)
self.assertNotIn("translations", opt1["openForms"])


Expand Down Expand Up @@ -726,7 +732,10 @@ def test_options_translated(self, lang_code, translation):
"label": "First option",
"openForms": {
"translations": {
lang_code: {"label": translation},
lang_code: {
"label": translation,
"description": translation,
},
}
},
}
Expand All @@ -738,8 +747,10 @@ def test_options_translated(self, lang_code, translation):
assert "values" in component
opt1 = component["values"][0]
assert "label" in opt1
assert "description" in opt1
assert "openForms" in opt1
self.assertEqual(opt1["label"], translation)
self.assertEqual(opt1["description"], translation)
self.assertNotIn("translations", opt1["openForms"])


Expand Down
1 change: 1 addition & 0 deletions src/openforms/formio/typing/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class OptionDict(TypedDict):

value: str
label: str
description: NotRequired[str]
openForms: NotRequired[OpenFormsOptionExtension]


Expand Down

0 comments on commit 5211292

Please sign in to comment.