Takayuki Shimizukawa avatar Takayuki Shimizukawa committed b685727

fix #1044 refs #955: fix external/inline links with localization

Comments (0)

Files changed (5)

sphinx/environment.py

             footnote_refs = [r for r in node.children
                              if isinstance(r, nodes.footnote_reference)
                              and r.get('auto') == 1]
+            refs = [r for r in node.children if isinstance(r, nodes.reference)]
+
             for i, child in enumerate(patch.children): # update leaves
                 if isinstance(child, nodes.footnote_reference) \
                    and child.get('auto') == 1:
                     # Some duplicated footnote_reference in msgstr cause
                     # IndexError by .pop(0). That is invalid msgstr.
 
+                elif isinstance(child, nodes.reference):
+                    # reference use original 'refname'.
+                    # * reference target ".. _Python: ..." is not translatable.
+                    # * section refname is not translatable.
+                    # * inline reference "`Python <...>`_" have no 'refname'.
+                    if refs and 'refname' in refs[0]:
+                        refname = child['refname'] = refs.pop(0)['refname']
+                        self.document.refnames.setdefault(
+                                refname, []).append(child)
+                    # if reference node count is not same before translation
+                    # that offen generate unknown link target warning.
+
             for child in patch.children: # update leaves
                 child.parent = node
             node.children = patch.children

tests/root/contents.txt

    versioning/index
    only
    i18n_footnote
+   i18n_external_links
 
    Python <http://python.org/>
 

tests/root/i18n_external_links.po

+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) 2012, foof
+# This file is distributed under the same license as the foo package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: sphinx 1.0\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2012-11-22 08:28\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+msgid "i18n with external links"
+msgstr "EXTERNAL LINKS"
+
+msgid "External link to Python_."
+msgstr "EXTERNAL LINK TO Python_."
+
+msgid "Internal link to `i18n with external links`_."
+msgstr "`EXTERNAL LINKS`_ IS INTERNAL LINK."
+
+msgid "Inline link by `Sphinx <http://sphinx-doc.org>`_."
+msgstr "INLINE LINK BY `SPHINX <http://sphinx-doc.org>`_."
+
+msgid "Unnamed link__."
+msgstr "UNNAMED LINK__."

tests/root/i18n_external_links.txt

+:tocdepth: 2
+
+i18n with external links
+========================
+.. #1044 external-links-dont-work-in-localized-html
+
+* External link to Python_.
+* Internal link to `i18n with external links`_.
+* Inline link by `Sphinx <http://sphinx-doc.org>`_.
+* Unnamed link__.
+
+.. _Python: http://python.org
+.. __: http://google.com

tests/test_intl.py

 """
 
 from subprocess import Popen, PIPE
+import re
 
 from util import *
 from util import SkipTest
 def setup_module():
     (test_root / 'xx' / 'LC_MESSAGES').makedirs()
     # Compile all required catalogs into binary format (*.mo).
-    for catalog in 'bom', 'subdir', 'i18n_footnote':
+    for catalog in 'bom', 'subdir', 'i18n_footnote', 'i18n_external_links':
         try:
             p = Popen(['msgfmt', test_root / '%s.po' % catalog, '-o',
                 test_root / 'xx' / 'LC_MESSAGES' / '%s.mo' % catalog],
               u"\n[ref] THIS IS A NAMED FOOTNOTE.\n"
               u"\n[100] THIS IS A NUMBERED FOOTNOTE.\n")
     assert result == expect
+
+
+@with_app(buildername='html',
+          confoverrides={'language': 'xx', 'locale_dirs': ['.']})
+def test_i18n_keep_external_links(app):
+    """regression test for #1044"""
+    app.builder.build(['i18n_external_links'])
+    result = (app.outdir / 'i18n_external_links.html').text(encoding='utf-8')
+
+    # external link check
+    expect_line = u"""<li>EXTERNAL LINK TO <a class="reference external" href="http://python.org">Python</a>.</li>"""
+    matched = re.search('^<li>EXTERNAL LINK TO .*$', result, re.M)
+    matched_line = ''
+    if matched:
+        matched_line = matched.group()
+    assert expect_line == matched_line
+
+    # internal link check
+    expect_line = u"""<li><a class="reference internal" href="#i18n-with-external-links">EXTERNAL LINKS</a> IS INTERNAL LINK.</li>"""
+    matched = re.search('^<li><a .* IS INTERNAL LINK.</li>$', result, re.M)
+    matched_line = ''
+    if matched:
+        matched_line = matched.group()
+    assert expect_line == matched_line
+
+    # inline link check
+    expect_line = u"""<li>INLINE LINK BY <a class="reference external" href="http://sphinx-doc.org">SPHINX</a>.</li>"""
+    matched = re.search('^<li>INLINE LINK BY .*$', result, re.M)
+    matched_line = ''
+    if matched:
+        matched_line = matched.group()
+    assert expect_line == matched_line
+
+    # unnamed link check
+    expect_line = u"""<li>UNNAMED <a class="reference external" href="http://google.com">LINK</a>.</li>"""
+    matched = re.search('^<li>UNNAMED .*$', result, re.M)
+    matched_line = ''
+    if matched:
+        matched_line = matched.group()
+    assert expect_line == matched_line
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.