Skip to content

Commit

Permalink
Improve obsolete msgstr fallback with fuzzy and translator comments f…
Browse files Browse the repository at this point in the history
…lags.
  • Loading branch information
mondeja committed Nov 7, 2020
1 parent 401c20f commit beaf4f5
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/docs/_build/
/dev.md
/example-project
/LC_MESSAGES

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
2 changes: 1 addition & 1 deletion docs/rationale.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Advantages
----------

* Updates into source files are synchronized. A change in one string declares
the old one fuzzy and the translation can be updated quickly.
the old one obsolete and the translation can be updated quickly.
* Translators work with ``.po`` files directly, a standard in translations.
* Parts of the Markdown files that do not need translated as code blocks or
are not included in the translation (by default), reducing possibility of
Expand Down
2 changes: 1 addition & 1 deletion mdpo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from mdpo.po2md import pofile_to_markdown


__version__ = '0.3.4'
__version__ = '0.3.5'
__version_info__ = tuple([int(i) for i in __version__.split('.')])
__title__ = 'mdpo'
__description__ = ('Markdown file translation utilities using pofiles')
Expand Down
2 changes: 2 additions & 0 deletions mdpo/md2po/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ def _save_msgid(self, msgid, tcomment=None, msgctxt=None):
compare_msgstr=False)
if _equal_entry and _equal_entry.msgstr:
entry.msgstr = _equal_entry.msgstr
if _equal_entry.fuzzy and not entry.fuzzy:
entry.flags.append('fuzzy')
if entry not in self.pofile:
self.pofile.append(entry)
self.found_entries.append(entry)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.3.4
current_version = 0.3.5

[bumpversion:file:mdpo/__init__.py]

Expand Down
87 changes: 87 additions & 0 deletions test/test_md2po/test_obsoletes.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,93 @@ def test_obsolete_msgstr_fallback(tmp_file, default_msgstr):
assert output == expected_output


@pytest.mark.parametrize(("default_msgstr"), ("", "Por defecto"))
def test_fuzzy_obsolete_msgstr_fallback(tmp_file, default_msgstr):
"""If a translated message is marked as obsolete and fuzzy, and his msgid
is found in markdown content, must be directly translated but needs to be
marked as fuzzy like the obsolete one. This behaviour is preferred
over default msgstr using ``msgstr`` parameter.
"""
markdown_content = '# Hello'
pofile_content = ('#\nmsgid ""\nmsgstr ""\n\n#, fuzzy\n'
'#~ msgid "Hello"\n#~ msgstr "Hola"\n')
expected_output = '''#
msgid ""
msgstr ""
#, fuzzy
msgid "Hello"
msgstr "Hola"
'''

with tmp_file(pofile_content, ".po") as po_filepath:
output = markdown_to_pofile(
markdown_content, po_filepath=po_filepath, msgstr=default_msgstr,
).__unicode__()
assert output == expected_output


@pytest.mark.parametrize(("default_msgstr"), ("", "Por defecto"))
def test_tcomment_obsolete_msgstr_fallback_without_found_tcomment(
tmp_file,
default_msgstr
):
"""If a translated message is marked as obsolete and has a translator
comment, and his msgid is found in markdown content and the found message
has not translator comment, must be directly translated but the tcomment
of the obsolete one is ignored. This behaviour is preferred over default
msgstr using ``msgstr`` parameter.
"""
markdown_content = '# Hello'
pofile_content = ('#\nmsgid ""\nmsgstr ""\n\n#. Translator comment\n'
'#~ msgid "Hello"\n#~ msgstr "Hola"\n')
expected_output = '''#
msgid ""
msgstr ""
msgid "Hello"
msgstr "Hola"
'''

with tmp_file(pofile_content, ".po") as po_filepath:
output = markdown_to_pofile(
markdown_content, po_filepath=po_filepath, msgstr=default_msgstr,
).__unicode__()
assert output == expected_output


@pytest.mark.parametrize(("default_msgstr"), ("", "Por defecto"))
def test_tcomment_obsolete_msgstr_fallback_with_found_tcomment(
tmp_file,
default_msgstr
):
"""If a translated message is marked as obsolete and has a translator
comment, and his msgid is found in markdown content and the found message
has a translator comment, must be directly translated and the tcomment
of the obsolete one is ignored, preserving the translator comment of the
found message. This behaviour is preferred over default msgstr using
``msgstr`` parameter.
"""
markdown_content = \
'<!-- mdpo-translator Comment for translator -->\n# Hello'
pofile_content = ('#\nmsgid ""\nmsgstr ""\n\n#. Other comment\n'
'#~ msgid "Hello"\n#~ msgstr "Hola"\n')
expected_output = '''#
msgid ""
msgstr ""
#. Comment for translator
msgid "Hello"
msgstr "Hola"
'''

with tmp_file(pofile_content, ".po") as po_filepath:
output = markdown_to_pofile(
markdown_content, po_filepath=po_filepath, msgstr=default_msgstr,
).__unicode__()
assert output == expected_output


def test_obsolete_with_msgctxt_matching_msgstr_fallback(tmp_file):
"""If a translated message with msgctxt is marked as obsolete and his msgid
with the same msgctxt is found in markdown content, must be directly
Expand Down

0 comments on commit beaf4f5

Please sign in to comment.