Takayuki Shimizukawa avatar Takayuki Shimizukawa committed 9890e2e

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

Comments (0)

Files changed (4)

sphinx/environment.py

                     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

tests/root/i18n/refs_inconsistency.po

 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."
 

tests/root/i18n/refs_inconsistency.txt

 
 * [100]_ for [#]_ footnote [ref2]_.
 * for reference_.
+* normal text.
 
 .. [#] This is a auto numbered footnote.
 .. [ref2] This is a named footnote.

tests/test_intl.py

               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")
 
     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,
     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
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.