2010-11-14 12:42:50 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
test_intl
|
|
|
|
~~~~~~~~~
|
|
|
|
|
|
|
|
Test message patching for internationalization purposes. Runs the text
|
|
|
|
builder in the test root.
|
|
|
|
|
|
|
|
:copyright: Copyright 2010 by the Sphinx team, see AUTHORS.
|
|
|
|
:license: BSD, see LICENSE for details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from subprocess import Popen, PIPE
|
2012-11-27 02:30:08 -06:00
|
|
|
import re
|
2012-12-04 22:11:16 -06:00
|
|
|
import os
|
|
|
|
from StringIO import StringIO
|
2010-11-14 12:42:50 -06:00
|
|
|
|
2012-12-11 01:36:50 -06:00
|
|
|
from sphinx.util.pycompat import relpath
|
|
|
|
|
2010-11-14 12:42:50 -06:00
|
|
|
from util import *
|
|
|
|
from util import SkipTest
|
|
|
|
|
|
|
|
|
2012-12-04 22:11:16 -06:00
|
|
|
warnfile = StringIO()
|
|
|
|
|
|
|
|
|
2010-11-14 12:42:50 -06:00
|
|
|
def setup_module():
|
2012-12-11 01:36:50 -06:00
|
|
|
# Delete remnants left over after failed build
|
|
|
|
(test_root / 'xx').rmtree(True)
|
2010-11-14 12:42:50 -06:00
|
|
|
(test_root / 'xx' / 'LC_MESSAGES').makedirs()
|
|
|
|
# Compile all required catalogs into binary format (*.mo).
|
2012-12-04 22:11:30 -06:00
|
|
|
for dirpath, dirs, files in os.walk(test_root):
|
2012-12-04 22:38:04 -06:00
|
|
|
dirpath = path(dirpath)
|
2012-12-04 22:11:30 -06:00
|
|
|
for f in [f for f in files if f.endswith('.po')]:
|
2012-12-04 22:38:04 -06:00
|
|
|
po = dirpath / f
|
2012-12-04 22:11:30 -06:00
|
|
|
mo = test_root / 'xx' / 'LC_MESSAGES' / (
|
2012-12-11 01:36:50 -06:00
|
|
|
relpath(po[:-3], test_root) + '.mo')
|
2012-12-04 22:38:04 -06:00
|
|
|
if not mo.parent.exists():
|
|
|
|
mo.parent.makedirs()
|
2012-12-04 22:11:30 -06:00
|
|
|
try:
|
|
|
|
p = Popen(['msgfmt', po, '-o', mo],
|
|
|
|
stdout=PIPE, stderr=PIPE)
|
|
|
|
except OSError:
|
|
|
|
raise SkipTest # most likely msgfmt was not found
|
|
|
|
else:
|
|
|
|
stdout, stderr = p.communicate()
|
|
|
|
if p.returncode != 0:
|
|
|
|
print stdout
|
|
|
|
print stderr
|
|
|
|
assert False, 'msgfmt exited with return code %s' % p.returncode
|
|
|
|
assert mo.isfile(), 'msgfmt failed'
|
2010-11-14 12:42:50 -06:00
|
|
|
|
|
|
|
|
|
|
|
def teardown_module():
|
|
|
|
(test_root / '_build').rmtree(True)
|
|
|
|
(test_root / 'xx').rmtree(True)
|
|
|
|
|
|
|
|
|
|
|
|
@with_app(buildername='text',
|
|
|
|
confoverrides={'language': 'xx', 'locale_dirs': ['.']})
|
2010-11-14 12:50:30 -06:00
|
|
|
def test_simple(app):
|
|
|
|
app.builder.build(['bom'])
|
2010-11-14 12:42:50 -06:00
|
|
|
result = (app.outdir / 'bom.txt').text(encoding='utf-8')
|
|
|
|
expect = (u"\nDatei mit UTF-8"
|
|
|
|
u"\n***************\n" # underline matches new translation
|
|
|
|
u"\nThis file has umlauts: äöü.\n")
|
|
|
|
assert result == expect
|
2010-11-14 12:50:30 -06:00
|
|
|
|
|
|
|
|
|
|
|
@with_app(buildername='text',
|
|
|
|
confoverrides={'language': 'xx', 'locale_dirs': ['.']})
|
|
|
|
def test_subdir(app):
|
|
|
|
app.builder.build(['subdir/includes'])
|
2010-11-14 12:42:50 -06:00
|
|
|
result = (app.outdir / 'subdir' / 'includes.txt').text(encoding='utf-8')
|
|
|
|
assert result.startswith(u"\ntranslation\n***********\n\n")
|
2012-11-21 20:43:09 -06:00
|
|
|
|
|
|
|
|
2012-12-04 22:38:04 -06:00
|
|
|
@with_app(buildername='html', cleanenv=True,
|
|
|
|
confoverrides={'language': 'xx', 'locale_dirs': ['.'],
|
|
|
|
'gettext_compact': False})
|
2012-11-21 20:43:09 -06:00
|
|
|
def test_i18n_footnote_break_refid(app):
|
|
|
|
"""test for #955 cant-build-html-with-footnotes-when-using"""
|
2012-12-04 22:38:04 -06:00
|
|
|
app.builder.build(['i18n/footnote'])
|
|
|
|
result = (app.outdir / 'i18n' / 'footnote.html').text(encoding='utf-8')
|
2012-11-21 20:43:09 -06:00
|
|
|
# expect no error by build
|
|
|
|
|
|
|
|
|
2012-12-04 22:38:04 -06:00
|
|
|
@with_app(buildername='text', cleanenv=True,
|
|
|
|
confoverrides={'language': 'xx', 'locale_dirs': ['.'],
|
|
|
|
'gettext_compact': False})
|
2012-11-21 20:43:09 -06:00
|
|
|
def test_i18n_footnote_regression(app):
|
|
|
|
"""regression test for fix #955"""
|
2012-12-04 22:38:04 -06:00
|
|
|
app.builder.build(['i18n/footnote'])
|
|
|
|
result = (app.outdir / 'i18n' / 'footnote.txt').text(encoding='utf-8')
|
2012-11-21 20:43:09 -06:00
|
|
|
expect = (u"\nI18N WITH FOOTNOTE"
|
|
|
|
u"\n******************\n" # underline matches new translation
|
2012-11-21 21:12:41 -06:00
|
|
|
u"\nI18N WITH FOOTNOTE INCLUDE THIS CONTENTS [ref] [1] [100]\n"
|
2012-11-21 20:43:09 -06:00
|
|
|
u"\n[1] THIS IS A AUTO NUMBERED FOOTNOTE.\n"
|
2012-11-21 21:12:41 -06:00
|
|
|
u"\n[ref] THIS IS A NAMED FOOTNOTE.\n"
|
|
|
|
u"\n[100] THIS IS A NUMBERED FOOTNOTE.\n")
|
2012-11-21 20:43:09 -06:00
|
|
|
assert result == expect
|
2012-11-27 02:30:08 -06:00
|
|
|
|
|
|
|
|
2012-12-18 16:49:17 -06:00
|
|
|
@with_app(buildername='html', cleanenv=True,
|
|
|
|
confoverrides={'language': 'xx', 'locale_dirs': ['.'],
|
|
|
|
'gettext_compact': False})
|
|
|
|
def test_i18n_footnote_backlink(app):
|
|
|
|
"""i18n test for #1058"""
|
|
|
|
app.builder.build(['i18n/footnote'])
|
|
|
|
result = (app.outdir / 'i18n' / 'footnote.html').text(encoding='utf-8')
|
|
|
|
expects = [
|
2012-12-18 17:02:21 -06:00
|
|
|
'<a class="footnote-reference" href="#id5" id="id1">[100]</a>',
|
|
|
|
'<a class="footnote-reference" href="#id4" id="id2">[1]</a>',
|
|
|
|
'<a class="reference internal" href="#ref" id="id3">[ref]</a>',
|
2012-12-18 16:49:17 -06:00
|
|
|
'<a class="fn-backref" href="#id2">[1]</a>',
|
|
|
|
'<a class="fn-backref" href="#id3">[ref]</a>',
|
|
|
|
'<a class="fn-backref" href="#id1">[100]</a>',
|
|
|
|
]
|
|
|
|
for expect in expects:
|
|
|
|
matches = re.findall(re.escape(expect), result)
|
|
|
|
assert len(matches) == 1
|
|
|
|
|
|
|
|
|
2012-12-04 22:38:04 -06:00
|
|
|
@with_app(buildername='text', warning=warnfile, cleanenv=True,
|
|
|
|
confoverrides={'language': 'xx', 'locale_dirs': ['.'],
|
|
|
|
'gettext_compact': False})
|
2012-12-04 22:11:16 -06:00
|
|
|
def test_i18n_warn_for_number_of_references_inconsistency(app):
|
|
|
|
app.builddir.rmtree(True)
|
2012-12-04 22:38:04 -06:00
|
|
|
app.builder.build(['i18n/refs_inconsistency'])
|
|
|
|
result = (app.outdir / 'i18n' / 'refs_inconsistency.txt').text(encoding='utf-8')
|
2012-12-04 22:11:16 -06:00
|
|
|
expect = (u"\nI18N WITH REFS INCONSISTENCY"
|
|
|
|
u"\n****************************\n"
|
2012-12-05 04:35:07 -06:00
|
|
|
u"\n* FOR FOOTNOTE [ref2].\n"
|
|
|
|
u"\n* reference FOR reference.\n"
|
2012-12-05 05:52:19 -06:00
|
|
|
u"\n* ORPHAN REFERENCE: I18N WITH REFS INCONSISTENCY.\n"
|
2012-12-04 22:11:16 -06:00
|
|
|
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")
|
|
|
|
assert result == expect
|
|
|
|
|
|
|
|
warnings = warnfile.getvalue().replace(os.sep, '/')
|
2012-12-07 01:38:21 -06:00
|
|
|
warning_fmt = u'.*/i18n/refs_inconsistency.txt:\\d+: ' \
|
|
|
|
u'WARNING: inconsistent %s in translated message\n'
|
|
|
|
expected_warning_expr = (
|
|
|
|
warning_fmt % 'footnote references' +
|
|
|
|
warning_fmt % 'references' +
|
|
|
|
warning_fmt % 'references')
|
|
|
|
assert re.search(expected_warning_expr, warnings)
|
2012-12-04 22:11:16 -06:00
|
|
|
|
|
|
|
|
2012-12-05 04:35:07 -06:00
|
|
|
@with_app(buildername='html', cleanenv=True,
|
|
|
|
confoverrides={'language': 'xx', 'locale_dirs': ['.'],
|
|
|
|
'gettext_compact': False})
|
|
|
|
def test_i18n_link_to_undefined_reference(app):
|
|
|
|
app.builder.build(['i18n/refs_inconsistency'])
|
|
|
|
result = (app.outdir / 'i18n' / 'refs_inconsistency.html').text(encoding='utf-8')
|
|
|
|
|
|
|
|
expected_expr = """<a class="reference external" href="http://www.example.com">reference</a>"""
|
2012-12-05 05:52:19 -06:00
|
|
|
assert len(re.findall(expected_expr, result)) == 2
|
2012-12-05 04:35:07 -06:00
|
|
|
|
|
|
|
expected_expr = """<a class="reference internal" href="#reference">reference</a>"""
|
2012-12-05 05:52:19 -06:00
|
|
|
assert len(re.findall(expected_expr, result)) == 0
|
|
|
|
|
|
|
|
expected_expr = """<a class="reference internal" href="#i18n-with-refs-inconsistency">I18N WITH REFS INCONSISTENCY</a>"""
|
2012-12-05 04:35:07 -06:00
|
|
|
assert len(re.findall(expected_expr, result)) == 1
|
|
|
|
|
|
|
|
|
2012-12-04 22:38:04 -06:00
|
|
|
@with_app(buildername='html', cleanenv=True,
|
|
|
|
confoverrides={'language': 'xx', 'locale_dirs': ['.'],
|
|
|
|
'gettext_compact': False})
|
2012-11-27 02:30:08 -06:00
|
|
|
def test_i18n_keep_external_links(app):
|
|
|
|
"""regression test for #1044"""
|
2012-12-04 22:38:04 -06:00
|
|
|
app.builder.build(['i18n/external_links'])
|
|
|
|
result = (app.outdir / 'i18n' / 'external_links.html').text(encoding='utf-8')
|
2012-11-27 02:30:08 -06:00
|
|
|
|
|
|
|
# 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
|
2012-12-16 07:56:38 -06:00
|
|
|
|
|
|
|
|
|
|
|
@with_app(buildername='text', warning=warnfile, cleanenv=True,
|
|
|
|
confoverrides={'language': 'xx', 'locale_dirs': ['.'],
|
|
|
|
'gettext_compact': False})
|
|
|
|
def test_i18n_literalblock_warning(app):
|
|
|
|
app.builddir.rmtree(True) #for warnings acceleration
|
|
|
|
app.builder.build(['i18n/literalblock'])
|
|
|
|
result = (app.outdir / 'i18n' / 'literalblock.txt').text(encoding='utf-8')
|
|
|
|
expect = (u"\nI18N WITH LITERAL BLOCK"
|
|
|
|
u"\n***********************\n"
|
|
|
|
u"\nCORRECT LITERAL BLOCK:\n"
|
|
|
|
u"\n this is"
|
|
|
|
u"\n literal block\n"
|
|
|
|
u"\nMISSING LITERAL BLOCK:\n"
|
|
|
|
u"\n<SYSTEM MESSAGE: ")
|
|
|
|
assert result.startswith(expect)
|
|
|
|
|
|
|
|
warnings = warnfile.getvalue().replace(os.sep, '/')
|
|
|
|
expected_warning_expr = u'.*/i18n/literalblock.txt:\\d+: ' \
|
|
|
|
u'WARNING: Literal block expected; none found.'
|
|
|
|
assert re.search(expected_warning_expr, warnings)
|