Skip to content

Commit 1317eee

Browse files
authored
Merge pull request #9828 from tk0miya/9618_gettext_allow_fuzzy_translations
Close #9618: i18n: Add gettext_allow_fuzzy_translations
2 parents 563936b + 203094b commit 1317eee

7 files changed

Lines changed: 54 additions & 4 deletions

File tree

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Features added
4444
* #9691: C, added new info-field ``retval``
4545
for :rst:dir:`c:function` and :rst:dir:`c:macro`.
4646
* C++, added new info-field ``retval`` for :rst:dir:`cpp:function`.
47+
* #9618: i18n: Add :confval:`gettext_allow_fuzzy_translations` to allow "fuzzy"
48+
messages for translation
4749
* #9672: More CSS classes on Python domain descriptions
4850
* #9695: More CSS classes on Javascript domain descriptions
4951
* #9683: Revert the removal of ``add_stylesheet()`` API. It will be kept until

doc/usage/configuration.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,13 @@ documentation on :ref:`intl` for details.
802802
.. versionchanged:: 1.5
803803
Use ``locales`` directory as a default value
804804

805+
.. confval:: gettext_allow_fuzzy_translations
806+
807+
If true, "fuzzy" messages in the message catalogs are used for translation.
808+
The default is ``False``.
809+
810+
.. versionadded:: 4.3
811+
805812
.. confval:: gettext_compact
806813

807814
.. versionadded:: 1.1

sphinx/application.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ def _init_i18n(self) -> None:
284284
self.config.language, self.config.source_encoding)
285285
for catalog in repo.catalogs:
286286
if catalog.domain == 'sphinx' and catalog.is_outdated():
287-
catalog.write_mo(self.config.language)
287+
catalog.write_mo(self.config.language,
288+
self.config.gettext_allow_fuzzy_translations)
288289

289290
locale_dirs: List[Optional[str]] = list(repo.locale_dirs)
290291
locale_dirs += [None]

sphinx/builders/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ def cat2relpath(cat: CatalogInfo) -> str:
217217
for catalog in status_iterator(catalogs, __('writing output... '), "darkgreen",
218218
len(catalogs), self.app.verbosity,
219219
stringify_func=cat2relpath):
220-
catalog.write_mo(self.config.language)
220+
catalog.write_mo(self.config.language,
221+
self.config.gettext_allow_fuzzy_translations)
221222

222223
def compile_all_catalogs(self) -> None:
223224
repo = CatalogRepository(self.srcdir, self.config.locale_dirs,

sphinx/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class Config:
103103
'language': (None, 'env', [str]),
104104
'locale_dirs': (['locales'], 'env', []),
105105
'figure_language_filename': ('{root}.{language}{ext}', 'env', [str]),
106+
'gettext_allow_fuzzy_translations': (False, 'gettext', []),
106107

107108
'master_doc': ('index', 'env', []),
108109
'root_doc': (lambda config: config.master_doc, 'env', []),

sphinx/util/i18n.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def is_outdated(self) -> bool:
5959
not path.exists(self.mo_path) or
6060
path.getmtime(self.mo_path) < path.getmtime(self.po_path))
6161

62-
def write_mo(self, locale: str) -> None:
62+
def write_mo(self, locale: str, use_fuzzy: bool = False) -> None:
6363
with open(self.po_path, encoding=self.charset) as file_po:
6464
try:
6565
po = read_po(file_po, locale)
@@ -69,7 +69,7 @@ def write_mo(self, locale: str) -> None:
6969

7070
with open(self.mo_path, 'wb') as file_mo:
7171
try:
72-
write_mo(file_mo, po)
72+
write_mo(file_mo, po, use_fuzzy)
7373
except Exception as exc:
7474
logger.warning(__('writing error: %s, %s'), self.mo_path, exc)
7575

tests/test_intl.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,44 @@ def getwarning(warnings):
13011301
return strip_escseq(warnings.getvalue().replace(os.sep, '/'))
13021302

13031303

1304+
@pytest.mark.sphinx('html', testroot='basic',
1305+
srcdir='gettext_allow_fuzzy_translations',
1306+
confoverrides={
1307+
'language': 'de',
1308+
'gettext_allow_fuzzy_translations': True
1309+
})
1310+
def test_gettext_allow_fuzzy_translations(app):
1311+
locale_dir = app.srcdir / 'locales' / 'de' / 'LC_MESSAGES'
1312+
locale_dir.makedirs()
1313+
with (locale_dir / 'index.po').open('wb') as f:
1314+
catalog = Catalog()
1315+
catalog.add('features', 'FEATURES', flags=('fuzzy',))
1316+
pofile.write_po(f, catalog)
1317+
1318+
app.build()
1319+
content = (app.outdir / 'index.html').read_text()
1320+
assert 'FEATURES' in content
1321+
1322+
1323+
@pytest.mark.sphinx('html', testroot='basic',
1324+
srcdir='gettext_disallow_fuzzy_translations',
1325+
confoverrides={
1326+
'language': 'de',
1327+
'gettext_allow_fuzzy_translations': False
1328+
})
1329+
def test_gettext_disallow_fuzzy_translations(app):
1330+
locale_dir = app.srcdir / 'locales' / 'de' / 'LC_MESSAGES'
1331+
locale_dir.makedirs()
1332+
with (locale_dir / 'index.po').open('wb') as f:
1333+
catalog = Catalog()
1334+
catalog.add('features', 'FEATURES', flags=('fuzzy',))
1335+
pofile.write_po(f, catalog)
1336+
1337+
app.build()
1338+
content = (app.outdir / 'index.html').read_text()
1339+
assert 'FEATURES' not in content
1340+
1341+
13041342
@pytest.mark.sphinx('html', testroot='basic', confoverrides={'language': 'de'})
13051343
def test_customize_system_message(make_app, app_params, sphinx_test_tempdir):
13061344
try:

0 commit comments

Comments
 (0)