summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Turner <9087854+AA-Turner@users.noreply.github.com>2023-04-21 19:04:26 +0100
committerGitHub <noreply@github.com>2023-04-21 19:04:26 +0100
commitaee3c0ab75974790adf359a9c5089d1d781a6b21 (patch)
treef15ca9a759ca865d656e2d78ef201b3ddf3a5412
parent186d596f3323f3141c34618e2e788e5cdff8fec3 (diff)
downloadsphinx-git-aee3c0ab75974790adf359a9c5089d1d781a6b21.tar.gz
Partially revert "Disable localisation when SOURCE_DATE_EPOCH is set (#10949)" (#11343)
This keeps some of the added tests, and avoids a full revert of ``sphinx.locale``.
-rw-r--r--CHANGES2
-rw-r--r--sphinx/builders/html/__init__.py2
-rw-r--r--sphinx/builders/latex/__init__.py2
-rw-r--r--sphinx/domains/std.py4
-rw-r--r--sphinx/locale/__init__.py25
-rw-r--r--sphinx/transforms/__init__.py2
-rw-r--r--sphinx/writers/manpage.py2
-rw-r--r--sphinx/writers/texinfo.py2
-rw-r--r--sphinx/writers/text.py4
-rw-r--r--tests/test_locale.py22
-rw-r--r--tests/test_util_inventory.py25
11 files changed, 18 insertions, 74 deletions
diff --git a/CHANGES b/CHANGES
index da414f1e2..d37710b54 100644
--- a/CHANGES
+++ b/CHANGES
@@ -97,8 +97,6 @@ Bugs fixed
* #11192: Restore correct parallel search index building.
Patch by Jeremy Maitin-Shepard
* Use the new Transifex ``tx`` client
-* #9778: Disable localisation when the ``SOURCE_DATE_EPOCH`` environment
- variable is set, to assist with 'reproducible builds'. Patch by James Addison
Testing
--------
diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py
index 5b7478e77..2bf14672d 100644
--- a/sphinx/builders/html/__init__.py
+++ b/sphinx/builders/html/__init__.py
@@ -502,7 +502,7 @@ class StandaloneHTMLBuilder(Builder):
# typically doesn't include the time of day
lufmt = self.config.html_last_updated_fmt
if lufmt is not None:
- self.last_updated = format_date(lufmt or str(_('%b %d, %Y')),
+ self.last_updated = format_date(lufmt or _('%b %d, %Y'),
language=self.config.language)
else:
self.last_updated = None
diff --git a/sphinx/builders/latex/__init__.py b/sphinx/builders/latex/__init__.py
index b8472a3c0..8efa3ad1c 100644
--- a/sphinx/builders/latex/__init__.py
+++ b/sphinx/builders/latex/__init__.py
@@ -179,7 +179,7 @@ class LaTeXBuilder(Builder):
if self.config.today:
self.context['date'] = self.config.today
else:
- self.context['date'] = format_date(self.config.today_fmt or str(_('%b %d, %Y')),
+ self.context['date'] = format_date(self.config.today_fmt or _('%b %d, %Y'),
language=self.config.language)
if self.config.latex_logo:
diff --git a/sphinx/domains/std.py b/sphinx/domains/std.py
index ad28c9f2c..d1a6b048f 100644
--- a/sphinx/domains/std.py
+++ b/sphinx/domains/std.py
@@ -242,9 +242,9 @@ class Cmdoption(ObjectDescription[str]):
# create an index entry
if currprogram:
- descr = str(_('%s command line option') % currprogram)
+ descr = _('%s command line option') % currprogram
else:
- descr = str(_('command line option'))
+ descr = _('command line option')
for option in signode.get('allnames', []):
entry = '; '.join([descr, option])
self.indexnode['entries'].append(('pair', entry, signode['ids'][0], '', None))
diff --git a/sphinx/locale/__init__.py b/sphinx/locale/__init__.py
index 8bc7a2673..c92640576 100644
--- a/sphinx/locale/__init__.py
+++ b/sphinx/locale/__init__.py
@@ -4,7 +4,7 @@ from __future__ import annotations
import locale
from gettext import NullTranslations, translation
-from os import getenv, path
+from os import path
from typing import Any, Callable
@@ -105,22 +105,10 @@ def init(
if translator.__class__ is NullTranslations:
translator = None
- if getenv('SOURCE_DATE_EPOCH') is not None:
- # Disable localization during reproducible source builds
- # See https://reproducible-builds.org/docs/source-date-epoch/
- #
- # Note: Providing an empty/none value to gettext.translation causes
- # it to consult various language-related environment variables to find
- # locale(s). We don't want that during a reproducible build; we want
- # to run through the same code path, but to return NullTranslations.
- #
- # To achieve that, specify the ISO-639-3 'undetermined' language code,
- # which should not match any translation catalogs.
- languages: list[str] | None = ['und']
- elif language:
+ if language:
if '_' in language:
# for language having country code (like "de_AT")
- languages = [language, language.split('_')[0]]
+ languages: list[str] | None = [language, language.split('_')[0]]
else:
languages = [language]
else:
@@ -203,7 +191,12 @@ def get_translation(catalog: str, namespace: str = 'general') -> Callable[[str],
.. versionadded:: 1.8
"""
def gettext(message: str) -> str:
- return _TranslationProxy(catalog, namespace, message) # type: ignore[return-value]
+ if not is_translator_registered(catalog, namespace):
+ # not initialized yet
+ return _TranslationProxy(catalog, namespace, message) # type: ignore[return-value] # noqa: E501
+ else:
+ translator = get_translator(catalog, namespace)
+ return translator.gettext(message)
return gettext
diff --git a/sphinx/transforms/__init__.py b/sphinx/transforms/__init__.py
index 2045a7426..f36473084 100644
--- a/sphinx/transforms/__init__.py
+++ b/sphinx/transforms/__init__.py
@@ -106,7 +106,7 @@ class DefaultSubstitutions(SphinxTransform):
text = self.config[refname]
if refname == 'today' and not text:
# special handling: can also specify a strftime format
- text = format_date(self.config.today_fmt or str(_('%b %d, %Y')),
+ text = format_date(self.config.today_fmt or _('%b %d, %Y'),
language=self.config.language)
ref.replace_self(nodes.Text(text))
diff --git a/sphinx/writers/manpage.py b/sphinx/writers/manpage.py
index 0731e9722..1e57f48ad 100644
--- a/sphinx/writers/manpage.py
+++ b/sphinx/writers/manpage.py
@@ -93,7 +93,7 @@ class ManualPageTranslator(SphinxTranslator, BaseTranslator):
if self.config.today:
self._docinfo['date'] = self.config.today
else:
- self._docinfo['date'] = format_date(self.config.today_fmt or str(_('%b %d, %Y')),
+ self._docinfo['date'] = format_date(self.config.today_fmt or _('%b %d, %Y'),
language=self.config.language)
self._docinfo['copyright'] = self.config.copyright
self._docinfo['version'] = self.config.version
diff --git a/sphinx/writers/texinfo.py b/sphinx/writers/texinfo.py
index 697943768..927e74f34 100644
--- a/sphinx/writers/texinfo.py
+++ b/sphinx/writers/texinfo.py
@@ -220,7 +220,7 @@ class TexinfoTranslator(SphinxTranslator):
'project': self.escape(self.config.project),
'copyright': self.escape(self.config.copyright),
'date': self.escape(self.config.today or
- format_date(self.config.today_fmt or str(_('%b %d, %Y')),
+ format_date(self.config.today_fmt or _('%b %d, %Y'),
language=self.config.language)),
})
# title
diff --git a/sphinx/writers/text.py b/sphinx/writers/text.py
index d78d600f8..3bce03ac6 100644
--- a/sphinx/writers/text.py
+++ b/sphinx/writers/text.py
@@ -791,8 +791,8 @@ class TextTranslator(SphinxTranslator):
def visit_image(self, node: Element) -> None:
if 'alt' in node.attributes:
- self.add_text(str(_('[image: %s]') % node['alt']))
- self.add_text(str(_('[image]')))
+ self.add_text(_('[image: %s]') % node['alt'])
+ self.add_text(_('[image]'))
raise nodes.SkipNode
def visit_transition(self, node: Element) -> None:
diff --git a/tests/test_locale.py b/tests/test_locale.py
index 1d90473ca..11dd95db9 100644
--- a/tests/test_locale.py
+++ b/tests/test_locale.py
@@ -74,25 +74,3 @@ def test_init_environment_language(rootdir, monkeypatch):
m.setenv("LANGUAGE", "et_EE:et")
_ = _empty_language_translation(rootdir)
assert _('Hello world') == 'Tere maailm'
-
-
-def test_init_reproducible_build_language(rootdir, monkeypatch):
- with monkeypatch.context() as m:
- m.setenv("SOURCE_DATE_EPOCH", "0")
- m.setenv("LANGUAGE", "en_US:en")
- _ = _empty_language_translation(rootdir)
- sde_en_translation = str(_('Hello world')) # str cast to evaluate lazy method
-
- with monkeypatch.context() as m:
- m.setenv("SOURCE_DATE_EPOCH", "0")
- m.setenv("LANGUAGE", "et_EE:et")
- _ = _empty_language_translation(rootdir)
- sde_et_translation = str(_('Hello world')) # str cast to evaluate lazy method
-
- with monkeypatch.context() as m:
- m.setenv("LANGUAGE", "et_EE:et")
- _ = _empty_language_translation(rootdir)
- loc_et_translation = str(_('Hello world')) # str cast to evaluate lazy method
-
- assert sde_en_translation == sde_et_translation
- assert sde_et_translation != loc_et_translation
diff --git a/tests/test_util_inventory.py b/tests/test_util_inventory.py
index 675bba06b..2833d979e 100644
--- a/tests/test_util_inventory.py
+++ b/tests/test_util_inventory.py
@@ -114,28 +114,3 @@ def test_inventory_localization(tempdir):
# Ensure that the inventory contents differ
assert inventory_et.read_bytes() != inventory_en.read_bytes()
-
-
-def test_inventory_reproducible(tempdir, monkeypatch):
- with monkeypatch.context() as m:
- # Configure reproducible builds
- # See: https://reproducible-builds.org/docs/source-date-epoch/
- m.setenv("SOURCE_DATE_EPOCH", "0")
-
- # Build an app using Estonian (EE) locale
- srcdir_et = _write_appconfig(tempdir, "et")
- reproducible_inventory_et = _build_inventory(srcdir_et)
-
- # Build the same app using English (US) locale
- srcdir_en = _write_appconfig(tempdir, "en")
- reproducible_inventory_en = _build_inventory(srcdir_en)
-
- # Also build the app using Estonian (EE) locale without build reproducibility enabled
- srcdir_et = _write_appconfig(tempdir, "et", prefix="localized")
- localized_inventory_et = _build_inventory(srcdir_et)
-
- # Ensure that the reproducible inventory contents are identical
- assert reproducible_inventory_et.read_bytes() == reproducible_inventory_en.read_bytes()
-
- # Ensure that inventory contents are different between a localized and non-localized build
- assert reproducible_inventory_et.read_bytes() != localized_inventory_et.read_bytes()