Skip to content

Commit

Permalink
Merge pull request #4385 from open-formulieren/fix/4363-checkboxes-ra…
Browse files Browse the repository at this point in the history
…dio-value-description-not-translated

[#4363] Missing translated description from the values of the components
  • Loading branch information
sergei-maertens authored Jun 14, 2024
2 parents 4079d14 + dd0e91d commit 41eb77f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/openforms/formio/components/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ def translate_options(
enabled: bool,
) -> None:
for option in options:
if not (translations := option.get("openForms", {}).get("translations")):
if "openForms" not in option:
continue

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

if enabled and (_translations := translations.get(language_code)):
if translated_label := _translations.get("label"):
option["label"] = translated_label

if translated_description := _translations.get("description"):
option["description"] = translated_description

# always clean up
del option["openForms"]["translations"] # type: ignore
del option["openForms"]["translations"]
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 @@ -52,6 +52,7 @@ class OptionDict(TypedDict):

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


Expand Down

0 comments on commit 41eb77f

Please sign in to comment.