Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-allow localisation when SOURCE_DATE_EPOCH is set (follow-up to #10949) #11303

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 3 additions & 15 deletions sphinx/locale/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import locale
from gettext import NullTranslations, translation
from os import getenv, path
from os import path
from typing import Any, Callable


Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,5 @@ def test_init_reproducible_build_language(rootdir, monkeypatch):
_ = _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
assert sde_en_translation != sde_et_translation
assert sde_et_translation == loc_et_translation
8 changes: 4 additions & 4 deletions tests/test_util_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ def test_inventory_reproducible(tempdir, monkeypatch):
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 each (reproducible) localized inventory is different
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()
# Ensure that inventory contents are the same between a reproducible and non-reproducible build
assert reproducible_inventory_et.read_bytes() == localized_inventory_et.read_bytes()