Skip to content

Commit 55ca37f

Browse files
authored
Add more internationalisation tests (#12277)
- Add test for basic literal block & literalinclude translation - Add gettext builder test for litteral blocks additional targets - Add i18n test for xref roles in titles - Add i18n tests for strange markup
1 parent 2f2078f commit 55ca37f

File tree

7 files changed

+136
-13
lines changed

7 files changed

+136
-13
lines changed

tests/roots/test-intl/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ CONTENTS
3131
section
3232
translation_progress
3333
topic
34+
markup
3435

3536
.. toctree::
3637
:maxdepth: 2

tests/roots/test-intl/markup.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
i18n with strange markup
2+
========================
3+
4+
1. title starting with 1.
5+
-------------------------
6+

tests/roots/test-intl/role_xref.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ i18n role xref
77

88
link to :term:`Some term`, :ref:`i18n-role-xref`, :doc:`index`.
99

10+
link to :term:`Some term`, :ref:`i18n-role-xref`, :doc:`index`.
11+
---------------------------------------------------------------
12+
1013
.. _same-type-links:
1114

1215
same type links

tests/roots/test-intl/xx/LC_MESSAGES/literalblock.po

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: sphinx 1.0\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2012-11-22 08:28+0000\n"
11+
"POT-Creation-Date: 2024-04-14 15:05+0200\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -22,6 +22,11 @@ msgstr "I18N WITH LITERAL BLOCK"
2222
msgid "Correct literal block::"
2323
msgstr "CORRECT LITERAL BLOCK::"
2424

25+
msgid ""
26+
"this is\n"
27+
"literal block"
28+
msgstr "THIS IS\nLITERAL BLOCK"
29+
2530
msgid "Missing literal block::"
2631
msgstr "MISSING LITERAL BLOCK::"
2732

@@ -31,6 +36,25 @@ msgstr "THAT'S ALL."
3136
msgid "included raw.txt"
3237
msgstr "INCLUDED RAW.TXT"
3338

39+
msgid ""
40+
"===\n"
41+
"Raw\n"
42+
"===\n"
43+
"\n"
44+
".. raw:: html\n"
45+
"\n"
46+
" <iframe src=\"https://sphinx-doc.org\"></iframe>\n"
47+
"\n"
48+
msgstr ""
49+
"===\n"
50+
"RAW\n"
51+
"===\n"
52+
"\n"
53+
".. raw:: html\n"
54+
"\n"
55+
" <iframe src=\"HTTPS://SPHINX-DOC.ORG\"></iframe>\n"
56+
"\n"
57+
3458
msgid "code blocks"
3559
msgstr "CODE-BLOCKS"
3660

@@ -43,9 +67,6 @@ msgstr ""
4367
" 'RESULT'\n"
4468
"end"
4569

46-
msgid "example of C language"
47-
msgstr "EXAMPLE OF C LANGUAGE"
48-
4970
msgid ""
5071
"#include <stdlib.h>\n"
5172
"int main(int argc, char** argv)\n"
@@ -59,6 +80,9 @@ msgstr ""
5980
" return 0;\n"
6081
"}"
6182

83+
msgid "example of C language"
84+
msgstr "EXAMPLE OF C LANGUAGE"
85+
6286
msgid ""
6387
"#include <stdio.h>\n"
6488
"int main(int argc, char** argv)\n"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# SOME DESCRIPTIVE TITLE.
2+
# Copyright (C) 2018, dev
3+
# This file is distributed under the same license as the sphinx package.
4+
# FIRST AUTHOR <EMAIL@ADDRESS>, 2018.
5+
#
6+
#, fuzzy
7+
msgid ""
8+
msgstr ""
9+
"Project-Id-Version: sphinx 1.0\n"
10+
"Report-Msgid-Bugs-To: \n"
11+
"POT-Creation-Date: 2018-05-06 16:44+0900\n"
12+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
13+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14+
"Language-Team: LANGUAGE <[email protected]>\n"
15+
"MIME-Version: 1.0\n"
16+
"Content-Type: text/plain; charset=utf-8\n"
17+
"Content-Transfer-Encoding: 8bit\n"
18+
"Generated-By: Babel 2.4.0\n"
19+
20+
msgid "i18n with strange markup"
21+
msgstr "I18N WITH STRANGE MARKUP"
22+
23+
msgid "1. title starting with 1."
24+
msgstr "1. TITLE STARTING WITH 1."
25+

tests/test_builders/test_build_gettext.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616
else:
1717
from sphinx.util.osutil import _chdir as chdir
1818

19-
_MSGID_PATTERN = re.compile(r'msgid "(.*)"')
19+
_MSGID_PATTERN = re.compile(r'msgid "((?:\n|.)*?)"\nmsgstr', re.MULTILINE)
2020

2121

22-
def msgid_getter(msgid):
23-
if m := _MSGID_PATTERN.search(msgid):
24-
return m[1]
25-
return None
22+
def get_msgids(pot):
23+
matches = _MSGID_PATTERN.findall(pot)
24+
return [m.replace('"\n"', '') for m in matches[1:]]
2625

2726

2827
def test_Catalog_duplicated_message():
@@ -105,7 +104,7 @@ def test_gettext_index_entries(app):
105104
app.build(filenames=[app.srcdir / 'index_entries.txt'])
106105

107106
pot = (app.outdir / 'index_entries.pot').read_text(encoding='utf8')
108-
msg_ids = list(filter(None, map(msgid_getter, pot.splitlines())))
107+
msg_ids = get_msgids(pot)
109108

110109
assert msg_ids == [
111110
"i18n with index entries",
@@ -134,7 +133,7 @@ def test_gettext_disable_index_entries(app):
134133
app.build(filenames=[app.srcdir / 'index_entries.txt'])
135134

136135
pot = (app.outdir / 'index_entries.pot').read_text(encoding='utf8')
137-
msg_ids = list(filter(None, map(msgid_getter, pot.splitlines())))
136+
msg_ids = get_msgids(pot)
138137

139138
assert msg_ids == [
140139
"i18n with index entries",
@@ -200,7 +199,7 @@ def test_gettext_prolog_epilog_substitution(app):
200199

201200
assert (app.outdir / 'prolog_epilog_substitution.pot').is_file()
202201
pot = (app.outdir / 'prolog_epilog_substitution.pot').read_text(encoding='utf8')
203-
msg_ids = list(filter(None, map(msgid_getter, pot.splitlines())))
202+
msg_ids = get_msgids(pot)
204203

205204
assert msg_ids == [
206205
"i18n with prologue and epilogue substitutions",
@@ -227,9 +226,43 @@ def test_gettext_prolog_epilog_substitution_excluded(app):
227226

228227
assert (app.outdir / 'prolog_epilog_substitution_excluded.pot').is_file()
229228
pot = (app.outdir / 'prolog_epilog_substitution_excluded.pot').read_text(encoding='utf8')
230-
msg_ids = list(filter(None, map(msgid_getter, pot.splitlines())))
229+
msg_ids = get_msgids(pot)
231230

232231
assert msg_ids == [
233232
"i18n without prologue and epilogue substitutions",
234233
"This is content that does not include prologue and epilogue substitutions.",
235234
]
235+
236+
237+
@pytest.mark.sphinx(
238+
'gettext', srcdir='gettext',
239+
confoverrides={'gettext_compact': False,
240+
'gettext_additional_targets': ['literal-block', 'doctest-block']})
241+
def test_gettext_literalblock_additional(app):
242+
app.build(force_all=True)
243+
244+
assert (app.outdir / 'literalblock.pot').is_file()
245+
pot = (app.outdir / 'literalblock.pot').read_text(encoding='utf8')
246+
msg_ids = get_msgids(pot)
247+
248+
assert msg_ids == [
249+
'i18n with literal block',
250+
'Correct literal block::',
251+
'this is\\nliteral block',
252+
'Missing literal block::',
253+
"That's all.",
254+
'included raw.txt',
255+
'===\\nRaw\\n===\\n\\n.. raw:: html\\n\\n <iframe src=\\"https://sphinx-doc.org\\"></iframe>\\n\\n',
256+
'code blocks',
257+
"def main\\n 'result'\\nend",
258+
'#include <stdlib.h>\\nint main(int argc, char** argv)\\n{\\n return 0;\\n}',
259+
'example of C language',
260+
'#include <stdio.h>\\nint main(int argc, char** argv)\\n{\\n return 0;\\n}',
261+
'literal-block\\nin list',
262+
'test_code_for_noqa()\\ncontinued()',
263+
'doctest blocks',
264+
'>>> import sys # sys importing\\n>>> def main(): # define main '
265+
"function\\n... sys.stdout.write('hello') # call write method of "
266+
"stdout object\\n>>>\\n>>> if __name__ == '__main__': # if run this py "
267+
'file as python script\\n... main() # call main',
268+
]

tests/test_intl/test_intl.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,15 @@ def test_xml_role_xref(app):
11911191
['i18n-role-xref', 'index',
11921192
'glossary_terms#term-Some-term'])
11931193

1194+
sec1_1, = sec1.findall('section')
1195+
title, = sec1_1.findall('title')
1196+
assert_elem(
1197+
title,
1198+
['LINK TO', "I18N ROCK'N ROLE XREF", ',', 'CONTENTS', ',',
1199+
'SOME NEW TERM', '.'],
1200+
['i18n-role-xref', 'index',
1201+
'glossary_terms#term-Some-term'])
1202+
11941203
para2 = sec2.findall('paragraph')
11951204
assert_elem(
11961205
para2[0],
@@ -1293,6 +1302,19 @@ def test_xml_label_targets(app):
12931302
'section-and-label'])
12941303

12951304

1305+
@sphinx_intl
1306+
@pytest.mark.sphinx('xml')
1307+
@pytest.mark.test_params(shared_result='test_intl_basic')
1308+
def test_xml_strange_markup(app):
1309+
app.build()
1310+
et = etree_parse(app.outdir / 'markup.xml')
1311+
secs = et.findall('section')
1312+
1313+
subsec1, = secs[0].findall('section')
1314+
title1, = subsec1.findall('title')
1315+
assert_elem(title1, ['1. TITLE STARTING WITH 1.'])
1316+
1317+
12961318
@sphinx_intl
12971319
@pytest.mark.sphinx('html')
12981320
@pytest.mark.test_params(shared_result='test_intl_basic')
@@ -1377,6 +1399,15 @@ def test_additional_targets_should_be_translated(app):
13771399
# [literalblock.txt]
13781400
result = (app.outdir / 'literalblock.html').read_text(encoding='utf8')
13791401

1402+
# basic literal bloc should be translated
1403+
expected_expr = ('<span class="n">THIS</span> <span class="n">IS</span>\n'
1404+
'<span class="n">LITERAL</span> <span class="n">BLOCK</span>')
1405+
assert_count(expected_expr, result, 1)
1406+
1407+
# literalinclude should be translated
1408+
expected_expr = '<span class="s2">&quot;HTTPS://SPHINX-DOC.ORG&quot;</span>'
1409+
assert_count(expected_expr, result, 1)
1410+
13801411
# title should be translated
13811412
expected_expr = 'CODE-BLOCKS'
13821413
assert_count(expected_expr, result, 2)

0 commit comments

Comments
 (0)