summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2020-08-14 16:11:23 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2020-10-04 13:31:37 +0900
commit37235c71e04aff3b93ae98f12456a34666635a9d (patch)
tree9d1f0ad545fdf33c7a3705c7e0c4842a2b1f48c0
parentb428cd2404675475a5c3dc2a2b0790ba57676202 (diff)
downloadsphinx-git-37235c71e04aff3b93ae98f12456a34666635a9d.tar.gz
Fix #6640: i18n: Failed to override system message translation
Our document describes that users can override system messages via their own message catalog named `sphinx.mo` under the locale_dirs. But it has not been used since its beginning of i18n mechanism because the priority of users' message catalog is lower than system's. This makes the priority of users' message catalog higher than system's.
-rw-r--r--CHANGES1
-rw-r--r--sphinx/application.py7
-rw-r--r--sphinx/locale/__init__.py2
-rw-r--r--tests/test_intl.py29
4 files changed, 36 insertions, 3 deletions
diff --git a/CHANGES b/CHANGES
index e30d42e43..481ade0b8 100644
--- a/CHANGES
+++ b/CHANGES
@@ -27,6 +27,7 @@ Bugs fixed
----------
* #8085: i18n: Add support for having single text domain
+* #6640: i18n: Failed to override system message translation
* #8143: autodoc: AttributeError is raised when False value is passed to
autodoc_default_options
* #8103: autodoc: functools.cached_property is not considered as a property
diff --git a/sphinx/application.py b/sphinx/application.py
index 385b74d8a..f91027bf7 100644
--- a/sphinx/application.py
+++ b/sphinx/application.py
@@ -18,7 +18,7 @@ import warnings
from collections import deque
from io import StringIO
from os import path
-from typing import Any, Callable, Dict, IO, List, Tuple, Union
+from typing import Any, Callable, Dict, IO, List, Optional, Tuple, Union
from docutils import nodes
from docutils.nodes import Element, TextElement
@@ -293,7 +293,10 @@ class Sphinx:
if catalog.domain == 'sphinx' and catalog.is_outdated():
catalog.write_mo(self.config.language)
- locale_dirs = [None, path.join(package_dir, 'locale')] + list(repo.locale_dirs)
+ locale_dirs = [None] # type: List[Optional[str]]
+ locale_dirs += list(repo.locale_dirs)
+ locale_dirs += [path.join(package_dir, 'locale')]
+
self.translator, has_translation = locale.init(locale_dirs, self.config.language)
if has_translation or self.config.language == 'en':
# "en" never needs to be translated
diff --git a/sphinx/locale/__init__.py b/sphinx/locale/__init__.py
index 385ca3566..5210dc725 100644
--- a/sphinx/locale/__init__.py
+++ b/sphinx/locale/__init__.py
@@ -106,7 +106,7 @@ class _TranslationProxy(UserString):
translators = defaultdict(NullTranslations) # type: Dict[Tuple[str, str], NullTranslations]
-def init(locale_dirs: List[str], language: str,
+def init(locale_dirs: List[Optional[str]], language: str,
catalog: str = 'sphinx', namespace: str = 'general') -> Tuple[NullTranslations, bool]:
"""Look for message catalogs in `locale_dirs` and *ensure* that there is at
least a NullTranslations catalog set in `translators`. If called multiple
diff --git a/tests/test_intl.py b/tests/test_intl.py
index 1d1282baa..c0b87d5ce 100644
--- a/tests/test_intl.py
+++ b/tests/test_intl.py
@@ -14,8 +14,10 @@ import re
import pytest
from babel.messages import pofile, mofile
+from babel.messages.catalog import Catalog
from docutils import nodes
+from sphinx import locale
from sphinx.testing.util import (
path, etree_parse, strip_escseq,
assert_re_search, assert_not_re_search, assert_startswith, assert_node
@@ -1289,3 +1291,30 @@ def test_image_glob_intl_using_figure_language_filename(app):
def getwarning(warnings):
return strip_escseq(warnings.getvalue().replace(os.sep, '/'))
+
+
+@pytest.mark.sphinx('html', testroot='basic', confoverrides={'language': 'de'})
+def test_customize_system_message(make_app, app_params, sphinx_test_tempdir):
+ try:
+ # clear translators cache
+ locale.translators.clear()
+
+ # prepare message catalog (.po)
+ locale_dir = sphinx_test_tempdir / 'basic' / 'locales' / 'de' / 'LC_MESSAGES'
+ locale_dir.makedirs()
+ with (locale_dir / 'sphinx.po').open('wb') as f:
+ catalog = Catalog()
+ catalog.add('Quick search', 'QUICK SEARCH')
+ pofile.write_po(f, catalog)
+
+ # construct application and convert po file to .mo
+ args, kwargs = app_params
+ app = make_app(*args, **kwargs)
+ assert (locale_dir / 'sphinx.mo').exists()
+ assert app.translator.gettext('Quick search') == 'QUICK SEARCH'
+
+ app.build()
+ content = (app.outdir / 'index.html').read_text()
+ assert 'QUICK SEARCH' in content
+ finally:
+ locale.translators.clear()