fix: rescue extra named-reference that appeared in same translation message.

This commit is contained in:
Takayuki Shimizukawa
2012-12-05 20:52:19 +09:00
parent e9275d3f0c
commit bfe96c14a7
4 changed files with 25 additions and 6 deletions
+15 -2
View File
@@ -251,12 +251,25 @@ class Locale(Transform):
and 'refname' in node
old_refs = node.traverse(is_refnamed_ref)
new_refs = patch.traverse(is_refnamed_ref)
applied_refname_map = {}
if len(old_refs) != len(new_refs):
env.warn_node('The number of reference are inconsistent '
'in both the translated form and the '
'untranslated form. skip translation.', node)
for old, new in zip(old_refs, new_refs):
new['refname'] = old['refname']
for new in new_refs:
if new['refname'] in applied_refname_map:
# 2nd appearance of the reference
new['refname'] = applied_refname_map[new['refname']]
elif old_refs:
# 1st appearance of the reference in old_refs
old = old_refs.pop(0)
refname = old['refname']
new['refname'] = refname
applied_refname_map[new['refname']] = refname
else:
# the reference is not found in old_refs
applied_refname_map[new['refname']] = new['refname']
self.document.note_refname(new)
# update leaves
+3
View File
@@ -25,6 +25,9 @@ msgstr "FOR FOOTNOTE [ref2]_."
msgid "for reference_."
msgstr "reference_ FOR reference_."
msgid "normal text."
msgstr "ORPHAN REFERENCE: `I18N WITH REFS INCONSISTENCY`_."
msgid "This is a auto numbered footnote."
msgstr "THIS IS A AUTO NUMBERED FOOTNOTE."
+1
View File
@@ -5,6 +5,7 @@ i18n with refs inconsistency
* [100]_ for [#]_ footnote [ref2]_.
* for reference_.
* normal text.
.. [#] This is a auto numbered footnote.
.. [ref2] This is a named footnote.
+6 -4
View File
@@ -113,6 +113,7 @@ def test_i18n_warn_for_number_of_references_inconsistency(app):
u"\n****************************\n"
u"\n* FOR FOOTNOTE [ref2].\n"
u"\n* reference FOR reference.\n"
u"\n* ORPHAN REFERENCE: I18N WITH REFS INCONSISTENCY.\n"
u"\n[1] THIS IS A AUTO NUMBERED FOOTNOTE.\n"
u"\n[ref2] THIS IS A NAMED FOOTNOTE.\n"
u"\n[100] THIS IS A NUMBERED FOOTNOTE.\n")
@@ -120,7 +121,7 @@ def test_i18n_warn_for_number_of_references_inconsistency(app):
warnings = warnfile.getvalue().replace(os.sep, '/')
expected_warning_expr = "i18n/refs_inconsistency.txt:\d+: WARNING: The number of reference are inconsistent in both the translated form and the untranslated form. skip translation."
assert len(re.findall(expected_warning_expr, warnings)) == 2
assert len(re.findall(expected_warning_expr, warnings)) == 3
@with_app(buildername='html', cleanenv=True,
@@ -131,11 +132,12 @@ def test_i18n_link_to_undefined_reference(app):
result = (app.outdir / 'i18n' / 'refs_inconsistency.html').text(encoding='utf-8')
expected_expr = """<a class="reference external" href="http://www.example.com">reference</a>"""
assert len(re.findall(expected_expr, result)) == 1
assert len(re.findall(expected_expr, result)) == 2
# the 2nd 'reference_' is to be internal-link instead of external-link.
# TODO: Can we re-use the same name named-reference?
expected_expr = """<a class="reference internal" href="#reference">reference</a>"""
assert len(re.findall(expected_expr, result)) == 0
expected_expr = """<a class="reference internal" href="#i18n-with-refs-inconsistency">I18N WITH REFS INCONSISTENCY</a>"""
assert len(re.findall(expected_expr, result)) == 1