Skip to content

Commit dfa6243

Browse files
authored
Don't skip normal locale negotiation if Accept-Language is set (#17968)
* Add failing test * Accept-Language shouldn't bypass local negotiation This should more closely mimic https://github.com/pypi/infra/blob/d5374e6e92f5a09729d649a0a587935d19f2d76f/terraform/warehouse/vcl/main.vcl#L160-L174 * Update test to use AcceptLanguageValidHeader * Add failing test for Accept-Language fallback * Don't renegotiate possibly unknown default locale Currently if the default locale negotiation fails, we fall back to trying to match based on Accept-Language. If this fails, it falls back on the default value, but because we re-negotiate the locale to determine the default value (without determining if the default is a known locale) we could potentially still set a garbage locale. Since the default for the default_match in best_match is already `None`, we can just use the default instead of re-negotiating.
1 parent 46b8ed6 commit dfa6243

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

tests/unit/i18n/test_init.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from pyramid import viewderivers
1717
from pyramid.i18n import Localizer
18+
from webob.acceptparse import AcceptLanguageValidHeader
1819

1920
from warehouse import i18n
2021
from warehouse.i18n.extensions import FallbackInternationalizationExtension
@@ -120,11 +121,9 @@ def test_when_locale_is_missing(monkeypatch):
120121
pretend.stub(
121122
params={},
122123
cookies={},
123-
accept_language=pretend.stub(
124-
best_match=lambda *a, **kw: "fake-locale-best-match"
125-
),
124+
accept_language=AcceptLanguageValidHeader(header_value="eo"),
126125
),
127-
"fake-locale-best-match",
126+
"eo",
128127
),
129128
(
130129
pretend.stub(
@@ -144,6 +143,20 @@ def test_when_locale_is_missing(monkeypatch):
144143
),
145144
None,
146145
),
146+
(
147+
pretend.stub(
148+
_LOCALE_="he",
149+
accept_language=AcceptLanguageValidHeader(header_value="eo"),
150+
),
151+
"he",
152+
),
153+
(
154+
pretend.stub(
155+
_LOCALE_="garbage",
156+
accept_language=AcceptLanguageValidHeader(header_value="xx"),
157+
),
158+
None,
159+
),
147160
],
148161
)
149162
def test_negotiate_locale(monkeypatch, req, expected):

warehouse/i18n/__init__.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,12 @@ def _locale(request):
8080

8181

8282
def _negotiate_locale(request):
83-
if not request.accept_language:
84-
locale_name = default_locale_negotiator(request)
85-
if locale_name in KNOWN_LOCALES:
86-
return locale_name
87-
else:
88-
return request.accept_language.best_match(
89-
tuple(KNOWN_LOCALES.keys()),
90-
default_match=default_locale_negotiator(request),
91-
)
83+
locale_name = default_locale_negotiator(request)
84+
if locale_name in KNOWN_LOCALES:
85+
return locale_name
86+
87+
if request.accept_language:
88+
return request.accept_language.best_match(tuple(KNOWN_LOCALES.keys()))
9289

9390
return None
9491

0 commit comments

Comments
 (0)