Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
devketanpro committed Jan 6, 2025
1 parent a42a10b commit 78bb33b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
16 changes: 10 additions & 6 deletions server/ntb/macros/nob_NO_translate_macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,16 @@ def nob_NO_translate_macro(item, **kwargs):
response = r.json()
item.update(response["document"])

# restore exempted text by removing `<span lang="nb">` tags
for field, value in item.items():
if isinstance(value, str):
item[field] = re.sub(r"<span lang=\"nb\">(.*?)</span>", r"\1", value)
item[field] = clean_exempted_text(value)

# map translated `description_text` fields back to the associations.
for editor in item.get("associations", {}):
flat_key = f"associations_desc_{editor}"
if flat_key in response["document"]:
item["associations"][editor]["description_text"] = re.sub(
r"<span lang=\"nb\">(.*?)</span>",
r"\1",
response["document"][flat_key],
item["associations"][editor]["description_text"] = clean_exempted_text(
response["document"][flat_key]
)
return item

Expand All @@ -87,6 +84,13 @@ def get_user_preference_params():
return [field.strip() for field in field_param.split(",") if field.strip()]


def clean_exempted_text(text):
"""Helper function to clean text by removing <span lang="nb"> tags."""
if isinstance(text, str):
return re.sub(r'<span lang="nb">(.*?)</span>', r"«\1»", text)
return text


name = "Bokmal to Nynorsk Translate Macro"
label = "Omsett NB til NN"
callback = nob_NO_translate_macro
Expand Down
6 changes: 3 additions & 3 deletions server/ntb/tests/macros/nob_NO_translate_macro_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ def test_second_item_translation(self, mock_post, mock_get_user):

nob_NO_translate_macro.callback(item)

self.assertEqual(item["headline"], "Bekrefter ny sesong av Forræder")
self.assertEqual(item["headline"], "Bekrefter ny sesong av «Forræder»")
self.assertEqual(
item["body_html"],
"<p>Dette er en tekst med Forræder nevnt flere ganger.</p>",
"<p>Dette er en tekst med «Forræder» nevnt flere ganger.</p>",
)
self.assertEqual(
item["associations"]["editor_1"]["description_text"],
"Forræder nevnt flere ganger",
"«Forræder» nevnt flere ganger",
)

@patch("ntb.macros.nob_NO_translate_macro.get_user", return_value=user_preferences)
Expand Down

0 comments on commit 78bb33b

Please sign in to comment.