Skip to content

Commit

Permalink
Add a fix_nls unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Jul 4, 2024
1 parent 80fb3cd commit f52d3df
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
14 changes: 13 additions & 1 deletion projects/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.test.utils import override_settings

from projects.models import Catalog, Project
from projects.translators import TranslationError
from projects.translators import TranslationError, fix_nls


class ProjectsTest(TestCase):
Expand Down Expand Up @@ -201,3 +201,15 @@ def test_suggest(self):
def test_invalid_catalog(self):
c = Catalog(language_code="it", domain="django", pofile="blub")
self.assertEqual(str(c), "Italian, django (Invalid)")

def test_fix_nls(self):
for test in [
("", "", ""),
("a\nb", "a\r\nb", "a\nb"),
("\na", "a", "\na"),
("a\n", "a", "a\n"),
("a", "\na\n", "a"),
("a", "\n", ""),
]:
with self.subTest(test=test):
self.assertEqual(fix_nls(test[0], test[1]), test[2])
24 changes: 24 additions & 0 deletions projects/translators.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,27 @@ def translate_by_deepl(text, to_language, auth_key):
raise TranslationError(
"Deepl returned a non-JSON or unexpected response."
) from exc


def fix_nls(in_, out_):
# Thanks, django-rosetta!
"""Fixes submitted translations by filtering carriage returns and pairing
newlines at the begging and end of the translated string with the original
"""
if len(in_) == 0 or len(out_) == 0:
return out_

if "\r" in out_ and "\r" not in in_:
out_ = out_.replace("\r", "")

if in_[0] == "\n" and out_[0] != "\n":
out_ = "\n" + out_
elif in_[0] != "\n" and out_[0] == "\n":
out_ = out_.lstrip()
if len(out_) == 0:
pass
elif in_[-1] == "\n" and out_[-1] != "\n":
out_ = out_ + "\n"
elif in_[-1] != "\n" and out_[-1] == "\n":
out_ = out_.rstrip()
return out_
27 changes: 2 additions & 25 deletions projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,6 @@ def __init__(self, *args, **kwargs):
)
self.entry_rows[-1]["msgstr"].append(self[name])

def fix_nls(self, in_, out_):
# Thanks, django-rosetta!
"""Fixes submitted translations by filtering carriage returns and pairing
newlines at the begging and end of the translated string with the original
"""
if len(in_) == 0 or len(out_) == 0:
return out_

if "\r" in out_ and "\r" not in in_:
out_ = out_.replace("\r", "")

if in_[0] == "\n" and out_[0] != "\n":
out_ = "\n" + out_
elif in_[0] != "\n" and out_[0] == "\n":
out_ = out_.lstrip()
if len(out_) == 0:
pass
elif in_[-1] == "\n" and out_[-1] != "\n":
out_ = out_ + "\n"
elif in_[-1] != "\n" and out_[-1] == "\n":
out_ = out_.rstrip()
return out_

def update(self, po, *, user):
for index in range(ENTRIES_PER_PAGE):
msgid_with_context = self.cleaned_data.get(f"msgid_{index}")
Expand All @@ -138,10 +115,10 @@ def update(self, po, *, user):

for entry in po:
if entry.msgid_with_context == msgid_with_context:
entry.msgstr = self.fix_nls(entry.msgid, msgstr)
entry.msgstr = translators.fix_nls(entry.msgid, msgstr)
if entry.msgid_plural:
for count in entry.msgstr_plural:
entry.msgstr_plural[count] = self.fix_nls(
entry.msgstr_plural[count] = translators.fix_nls(
entry.msgid_plural,
self.cleaned_data.get(f"msgstr_{index}:{count}", ""),
)
Expand Down

0 comments on commit f52d3df

Please sign in to comment.