diff --git a/projects/test_views.py b/projects/test_views.py index 838ac38..a8bdee2 100644 --- a/projects/test_views.py +++ b/projects/test_views.py @@ -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): @@ -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]) diff --git a/projects/translators.py b/projects/translators.py index 4f1c24e..ae2556b 100644 --- a/projects/translators.py +++ b/projects/translators.py @@ -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_ diff --git a/projects/views.py b/projects/views.py index d69bb20..a7ca4e4 100644 --- a/projects/views.py +++ b/projects/views.py @@ -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}") @@ -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}", ""), )