Skip to content

Commit aee3c0a

Browse files
authored
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``.
1 parent 186d596 commit aee3c0a

File tree

11 files changed

+18
-74
lines changed

11 files changed

+18
-74
lines changed

CHANGES

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ Bugs fixed
9797
* #11192: Restore correct parallel search index building.
9898
Patch by Jeremy Maitin-Shepard
9999
* Use the new Transifex ``tx`` client
100-
* #9778: Disable localisation when the ``SOURCE_DATE_EPOCH`` environment
101-
variable is set, to assist with 'reproducible builds'. Patch by James Addison
102100

103101
Testing
104102
--------

sphinx/builders/html/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ def prepare_writing(self, docnames: set[str]) -> None:
502502
# typically doesn't include the time of day
503503
lufmt = self.config.html_last_updated_fmt
504504
if lufmt is not None:
505-
self.last_updated = format_date(lufmt or str(_('%b %d, %Y')),
505+
self.last_updated = format_date(lufmt or _('%b %d, %Y'),
506506
language=self.config.language)
507507
else:
508508
self.last_updated = None

sphinx/builders/latex/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def init_context(self) -> None:
179179
if self.config.today:
180180
self.context['date'] = self.config.today
181181
else:
182-
self.context['date'] = format_date(self.config.today_fmt or str(_('%b %d, %Y')),
182+
self.context['date'] = format_date(self.config.today_fmt or _('%b %d, %Y'),
183183
language=self.config.language)
184184

185185
if self.config.latex_logo:

sphinx/domains/std.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ def add_target_and_index(self, firstname: str, sig: str, signode: desc_signature
242242

243243
# create an index entry
244244
if currprogram:
245-
descr = str(_('%s command line option') % currprogram)
245+
descr = _('%s command line option') % currprogram
246246
else:
247-
descr = str(_('command line option'))
247+
descr = _('command line option')
248248
for option in signode.get('allnames', []):
249249
entry = '; '.join([descr, option])
250250
self.indexnode['entries'].append(('pair', entry, signode['ids'][0], '', None))

sphinx/locale/__init__.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import locale
66
from gettext import NullTranslations, translation
7-
from os import getenv, path
7+
from os import path
88
from typing import Any, Callable
99

1010

@@ -105,22 +105,10 @@ def init(
105105
if translator.__class__ is NullTranslations:
106106
translator = None
107107

108-
if getenv('SOURCE_DATE_EPOCH') is not None:
109-
# Disable localization during reproducible source builds
110-
# See https://reproducible-builds.org/docs/source-date-epoch/
111-
#
112-
# Note: Providing an empty/none value to gettext.translation causes
113-
# it to consult various language-related environment variables to find
114-
# locale(s). We don't want that during a reproducible build; we want
115-
# to run through the same code path, but to return NullTranslations.
116-
#
117-
# To achieve that, specify the ISO-639-3 'undetermined' language code,
118-
# which should not match any translation catalogs.
119-
languages: list[str] | None = ['und']
120-
elif language:
108+
if language:
121109
if '_' in language:
122110
# for language having country code (like "de_AT")
123-
languages = [language, language.split('_')[0]]
111+
languages: list[str] | None = [language, language.split('_')[0]]
124112
else:
125113
languages = [language]
126114
else:
@@ -203,7 +191,12 @@ def setup(app):
203191
.. versionadded:: 1.8
204192
"""
205193
def gettext(message: str) -> str:
206-
return _TranslationProxy(catalog, namespace, message) # type: ignore[return-value]
194+
if not is_translator_registered(catalog, namespace):
195+
# not initialized yet
196+
return _TranslationProxy(catalog, namespace, message) # type: ignore[return-value] # noqa: E501
197+
else:
198+
translator = get_translator(catalog, namespace)
199+
return translator.gettext(message)
207200

208201
return gettext
209202

sphinx/transforms/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def apply(self, **kwargs: Any) -> None:
106106
text = self.config[refname]
107107
if refname == 'today' and not text:
108108
# special handling: can also specify a strftime format
109-
text = format_date(self.config.today_fmt or str(_('%b %d, %Y')),
109+
text = format_date(self.config.today_fmt or _('%b %d, %Y'),
110110
language=self.config.language)
111111
ref.replace_self(nodes.Text(text))
112112

sphinx/writers/manpage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def __init__(self, document: nodes.document, builder: Builder) -> None:
9393
if self.config.today:
9494
self._docinfo['date'] = self.config.today
9595
else:
96-
self._docinfo['date'] = format_date(self.config.today_fmt or str(_('%b %d, %Y')),
96+
self._docinfo['date'] = format_date(self.config.today_fmt or _('%b %d, %Y'),
9797
language=self.config.language)
9898
self._docinfo['copyright'] = self.config.copyright
9999
self._docinfo['version'] = self.config.version

sphinx/writers/texinfo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def init_settings(self) -> None:
220220
'project': self.escape(self.config.project),
221221
'copyright': self.escape(self.config.copyright),
222222
'date': self.escape(self.config.today or
223-
format_date(self.config.today_fmt or str(_('%b %d, %Y')),
223+
format_date(self.config.today_fmt or _('%b %d, %Y'),
224224
language=self.config.language)),
225225
})
226226
# title

sphinx/writers/text.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,8 +791,8 @@ def visit_acks(self, node: Element) -> None:
791791

792792
def visit_image(self, node: Element) -> None:
793793
if 'alt' in node.attributes:
794-
self.add_text(str(_('[image: %s]') % node['alt']))
795-
self.add_text(str(_('[image]')))
794+
self.add_text(_('[image: %s]') % node['alt'])
795+
self.add_text(_('[image]'))
796796
raise nodes.SkipNode
797797

798798
def visit_transition(self, node: Element) -> None:

tests/test_locale.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,3 @@ def test_init_environment_language(rootdir, monkeypatch):
7474
m.setenv("LANGUAGE", "et_EE:et")
7575
_ = _empty_language_translation(rootdir)
7676
assert _('Hello world') == 'Tere maailm'
77-
78-
79-
def test_init_reproducible_build_language(rootdir, monkeypatch):
80-
with monkeypatch.context() as m:
81-
m.setenv("SOURCE_DATE_EPOCH", "0")
82-
m.setenv("LANGUAGE", "en_US:en")
83-
_ = _empty_language_translation(rootdir)
84-
sde_en_translation = str(_('Hello world')) # str cast to evaluate lazy method
85-
86-
with monkeypatch.context() as m:
87-
m.setenv("SOURCE_DATE_EPOCH", "0")
88-
m.setenv("LANGUAGE", "et_EE:et")
89-
_ = _empty_language_translation(rootdir)
90-
sde_et_translation = str(_('Hello world')) # str cast to evaluate lazy method
91-
92-
with monkeypatch.context() as m:
93-
m.setenv("LANGUAGE", "et_EE:et")
94-
_ = _empty_language_translation(rootdir)
95-
loc_et_translation = str(_('Hello world')) # str cast to evaluate lazy method
96-
97-
assert sde_en_translation == sde_et_translation
98-
assert sde_et_translation != loc_et_translation

0 commit comments

Comments
 (0)