diff --git a/sphinx/environment.py b/sphinx/environment.py
index bfad00f43..9e971ed38 100644
--- a/sphinx/environment.py
+++ b/sphinx/environment.py
@@ -226,11 +226,20 @@ class Locale(Transform):
if not isinstance(patch, nodes.paragraph):
continue # skip for now
- # copy text children
- for i, child in enumerate(patch.children):
- if isinstance(child, nodes.Text):
- child.parent = node
- node.children[i] = child
+ footnote_refs = [r for r in node.children
+ if isinstance(r, nodes.footnote_reference)
+ and r.get('auto') == 1]
+ for i, child in enumerate(patch.children): # update leaves
+ if isinstance(child, nodes.footnote_reference):
+ # use original 'footnote_reference' object.
+ # this object already registered in self.document.autofootnote_refs
+ patch.children[i] = footnote_refs.pop(0)
+ # Some duplicated footnote_reference in msgstr cause
+ # IndexError by .pop(0). That is invalid msgstr.
+
+ for child in patch.children: # update leaves
+ child.parent = node
+ node.children = patch.children
class SphinxStandaloneReader(standalone.Reader):
diff --git a/tests/root/contents.txt b/tests/root/contents.txt
index ad246cb77..3ba41eedb 100644
--- a/tests/root/contents.txt
+++ b/tests/root/contents.txt
@@ -28,6 +28,7 @@ Contents:
extensions
versioning/index
only
+ i18n_footnote
Python
diff --git a/tests/root/i18n_footnote.po b/tests/root/i18n_footnote.po
new file mode 100644
index 000000000..b7f2b584b
--- /dev/null
+++ b/tests/root/i18n_footnote.po
@@ -0,0 +1,30 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) 2012, foof
+# This file is distributed under the same license as the foo package.
+# FIRST AUTHOR , 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 \n"
+"Language-Team: LANGUAGE \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+msgid "i18n with Footnote"
+msgstr "I18N WITH FOOTNOTE"
+
+msgid "Contents [#]_ for `i18n with Footnote`_ [ref]_"
+msgstr "`I18N WITH FOOTNOTE`_ INCLUDE THIS CONTENTS [ref]_ [#]_"
+
+msgid "This is a auto numbered footnote."
+msgstr "THIS IS A AUTO NUMBERED FOOTNOTE."
+
+msgid "This is a named footnote."
+msgstr "THIS IS A NAMED FOOTNOTE."
+
diff --git a/tests/root/i18n_footnote.txt b/tests/root/i18n_footnote.txt
new file mode 100644
index 000000000..3a1097af2
--- /dev/null
+++ b/tests/root/i18n_footnote.txt
@@ -0,0 +1,10 @@
+:tocdepth: 2
+
+i18n with Footnote
+==================
+.. #955 cant-build-html-with-footnotes-when-using
+
+Contents [#]_ for `i18n with Footnote`_ [ref]_
+
+.. [#] This is a auto numbered footnote.
+.. [ref] This is a named footnote.
diff --git a/tests/test_intl.py b/tests/test_intl.py
index d1e28002f..010051661 100644
--- a/tests/test_intl.py
+++ b/tests/test_intl.py
@@ -19,7 +19,7 @@ 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':
+ for catalog in 'bom', 'subdir', 'i18n_footnote':
try:
p = Popen(['msgfmt', test_root / '%s.po' % catalog, '-o',
test_root / 'xx' / 'LC_MESSAGES' / '%s.mo' % catalog],
@@ -63,3 +63,26 @@ def test_subdir(app):
app.builder.build(['subdir/includes'])
result = (app.outdir / 'subdir' / 'includes.txt').text(encoding='utf-8')
assert result.startswith(u"\ntranslation\n***********\n\n")
+
+
+@with_app(buildername='html',
+ confoverrides={'language': 'xx', 'locale_dirs': ['.']})
+def test_i18n_footnote_break_refid(app):
+ """test for #955 cant-build-html-with-footnotes-when-using"""
+ app.builder.build(['i18n_footnote'])
+ result = (app.outdir / 'i18n_footnote.html').text(encoding='utf-8')
+ # expect no error by build
+
+
+@with_app(buildername='text',
+ confoverrides={'language': 'xx', 'locale_dirs': ['.']})
+def test_i18n_footnote_regression(app):
+ """regression test for fix #955"""
+ app.builder.build(['i18n_footnote'])
+ result = (app.outdir / 'i18n_footnote.txt').text(encoding='utf-8')
+ expect = (u"\nI18N WITH FOOTNOTE"
+ u"\n******************\n" # underline matches new translation
+ u"\nI18N WITH FOOTNOTE INCLUDE THIS CONTENTS [ref] [1]\n"
+ u"\n[1] THIS IS A AUTO NUMBERED FOOTNOTE.\n"
+ u"\n[ref] THIS IS A NAMED FOOTNOTE.\n")
+ assert result == expect