mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Close #9618: i18n: Add gettext_allow_fuzzy_translations
This commit is contained in:
parent
8e350495b0
commit
203094b4c3
2
CHANGES
2
CHANGES
@ -43,6 +43,8 @@ Features added
|
|||||||
* #9691: C, added new info-field ``retval``
|
* #9691: C, added new info-field ``retval``
|
||||||
for :rst:dir:`c:function` and :rst:dir:`c:macro`.
|
for :rst:dir:`c:function` and :rst:dir:`c:macro`.
|
||||||
* C++, added new info-field ``retval`` for :rst:dir:`cpp:function`.
|
* C++, added new info-field ``retval`` for :rst:dir:`cpp:function`.
|
||||||
|
* #9618: i18n: Add :confval:`gettext_allow_fuzzy_translations` to allow "fuzzy"
|
||||||
|
messages for translation
|
||||||
* #9672: More CSS classes on Python domain descriptions
|
* #9672: More CSS classes on Python domain descriptions
|
||||||
* #9695: More CSS classes on Javascript domain descriptions
|
* #9695: More CSS classes on Javascript domain descriptions
|
||||||
* #9683: Revert the removal of ``add_stylesheet()`` API. It will be kept until
|
* #9683: Revert the removal of ``add_stylesheet()`` API. It will be kept until
|
||||||
|
@ -802,6 +802,13 @@ documentation on :ref:`intl` for details.
|
|||||||
.. versionchanged:: 1.5
|
.. versionchanged:: 1.5
|
||||||
Use ``locales`` directory as a default value
|
Use ``locales`` directory as a default value
|
||||||
|
|
||||||
|
.. confval:: gettext_allow_fuzzy_translations
|
||||||
|
|
||||||
|
If true, "fuzzy" messages in the message catalogs are used for translation.
|
||||||
|
The default is ``False``.
|
||||||
|
|
||||||
|
.. versionadded:: 4.3
|
||||||
|
|
||||||
.. confval:: gettext_compact
|
.. confval:: gettext_compact
|
||||||
|
|
||||||
.. versionadded:: 1.1
|
.. versionadded:: 1.1
|
||||||
|
@ -284,7 +284,8 @@ class Sphinx:
|
|||||||
self.config.language, self.config.source_encoding)
|
self.config.language, self.config.source_encoding)
|
||||||
for catalog in repo.catalogs:
|
for catalog in repo.catalogs:
|
||||||
if catalog.domain == 'sphinx' and catalog.is_outdated():
|
if catalog.domain == 'sphinx' and catalog.is_outdated():
|
||||||
catalog.write_mo(self.config.language)
|
catalog.write_mo(self.config.language,
|
||||||
|
self.config.gettext_allow_fuzzy_translations)
|
||||||
|
|
||||||
locale_dirs: List[Optional[str]] = list(repo.locale_dirs)
|
locale_dirs: List[Optional[str]] = list(repo.locale_dirs)
|
||||||
locale_dirs += [None]
|
locale_dirs += [None]
|
||||||
|
@ -217,7 +217,8 @@ class Builder:
|
|||||||
for catalog in status_iterator(catalogs, __('writing output... '), "darkgreen",
|
for catalog in status_iterator(catalogs, __('writing output... '), "darkgreen",
|
||||||
len(catalogs), self.app.verbosity,
|
len(catalogs), self.app.verbosity,
|
||||||
stringify_func=cat2relpath):
|
stringify_func=cat2relpath):
|
||||||
catalog.write_mo(self.config.language)
|
catalog.write_mo(self.config.language,
|
||||||
|
self.config.gettext_allow_fuzzy_translations)
|
||||||
|
|
||||||
def compile_all_catalogs(self) -> None:
|
def compile_all_catalogs(self) -> None:
|
||||||
repo = CatalogRepository(self.srcdir, self.config.locale_dirs,
|
repo = CatalogRepository(self.srcdir, self.config.locale_dirs,
|
||||||
|
@ -103,6 +103,7 @@ class Config:
|
|||||||
'language': (None, 'env', [str]),
|
'language': (None, 'env', [str]),
|
||||||
'locale_dirs': (['locales'], 'env', []),
|
'locale_dirs': (['locales'], 'env', []),
|
||||||
'figure_language_filename': ('{root}.{language}{ext}', 'env', [str]),
|
'figure_language_filename': ('{root}.{language}{ext}', 'env', [str]),
|
||||||
|
'gettext_allow_fuzzy_translations': (False, 'gettext', []),
|
||||||
|
|
||||||
'master_doc': ('index', 'env', []),
|
'master_doc': ('index', 'env', []),
|
||||||
'root_doc': (lambda config: config.master_doc, 'env', []),
|
'root_doc': (lambda config: config.master_doc, 'env', []),
|
||||||
|
@ -59,7 +59,7 @@ class CatalogInfo(LocaleFileInfoBase):
|
|||||||
not path.exists(self.mo_path) or
|
not path.exists(self.mo_path) or
|
||||||
path.getmtime(self.mo_path) < path.getmtime(self.po_path))
|
path.getmtime(self.mo_path) < path.getmtime(self.po_path))
|
||||||
|
|
||||||
def write_mo(self, locale: str) -> None:
|
def write_mo(self, locale: str, use_fuzzy: bool = False) -> None:
|
||||||
with open(self.po_path, encoding=self.charset) as file_po:
|
with open(self.po_path, encoding=self.charset) as file_po:
|
||||||
try:
|
try:
|
||||||
po = read_po(file_po, locale)
|
po = read_po(file_po, locale)
|
||||||
@ -69,7 +69,7 @@ class CatalogInfo(LocaleFileInfoBase):
|
|||||||
|
|
||||||
with open(self.mo_path, 'wb') as file_mo:
|
with open(self.mo_path, 'wb') as file_mo:
|
||||||
try:
|
try:
|
||||||
write_mo(file_mo, po)
|
write_mo(file_mo, po, use_fuzzy)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.warning(__('writing error: %s, %s'), self.mo_path, exc)
|
logger.warning(__('writing error: %s, %s'), self.mo_path, exc)
|
||||||
|
|
||||||
|
@ -1301,6 +1301,44 @@ def getwarning(warnings):
|
|||||||
return strip_escseq(warnings.getvalue().replace(os.sep, '/'))
|
return strip_escseq(warnings.getvalue().replace(os.sep, '/'))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.sphinx('html', testroot='basic',
|
||||||
|
srcdir='gettext_allow_fuzzy_translations',
|
||||||
|
confoverrides={
|
||||||
|
'language': 'de',
|
||||||
|
'gettext_allow_fuzzy_translations': True
|
||||||
|
})
|
||||||
|
def test_gettext_allow_fuzzy_translations(app):
|
||||||
|
locale_dir = app.srcdir / 'locales' / 'de' / 'LC_MESSAGES'
|
||||||
|
locale_dir.makedirs()
|
||||||
|
with (locale_dir / 'index.po').open('wb') as f:
|
||||||
|
catalog = Catalog()
|
||||||
|
catalog.add('features', 'FEATURES', flags=('fuzzy',))
|
||||||
|
pofile.write_po(f, catalog)
|
||||||
|
|
||||||
|
app.build()
|
||||||
|
content = (app.outdir / 'index.html').read_text()
|
||||||
|
assert 'FEATURES' in content
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.sphinx('html', testroot='basic',
|
||||||
|
srcdir='gettext_disallow_fuzzy_translations',
|
||||||
|
confoverrides={
|
||||||
|
'language': 'de',
|
||||||
|
'gettext_allow_fuzzy_translations': False
|
||||||
|
})
|
||||||
|
def test_gettext_disallow_fuzzy_translations(app):
|
||||||
|
locale_dir = app.srcdir / 'locales' / 'de' / 'LC_MESSAGES'
|
||||||
|
locale_dir.makedirs()
|
||||||
|
with (locale_dir / 'index.po').open('wb') as f:
|
||||||
|
catalog = Catalog()
|
||||||
|
catalog.add('features', 'FEATURES', flags=('fuzzy',))
|
||||||
|
pofile.write_po(f, catalog)
|
||||||
|
|
||||||
|
app.build()
|
||||||
|
content = (app.outdir / 'index.html').read_text()
|
||||||
|
assert 'FEATURES' not in content
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.sphinx('html', testroot='basic', confoverrides={'language': 'de'})
|
@pytest.mark.sphinx('html', testroot='basic', confoverrides={'language': 'de'})
|
||||||
def test_customize_system_message(make_app, app_params, sphinx_test_tempdir):
|
def test_customize_system_message(make_app, app_params, sphinx_test_tempdir):
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user