Skip to content

Commit 86365a9

Browse files
committed
fix(i18n): normalize object-form codes in computePreferredLocaleList
The codes branch compared the configured code against the browser locale raw, while the sibling string branch and sortAndFilterLocales both compare normalized values. A code differing only by case or separator passed the filter and was then dropped by the raw compare, so preferredLocaleList came back empty while preferredLocale returned the code on the same request.
1 parent 8c193f6 commit 86365a9

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

‎.changeset/dark-bobcats-do.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes `Astro.preferredLocaleList` returning an empty list when a locale is configured with the object form (`{ path, codes }`) and the browser sends the code with different casing or an underscore, such as `en-US` matching a configured `en-us`

‎packages/astro/src/i18n/utils.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export function computePreferredLocaleList(request: Request, locales: Locales):
131131
}
132132
} else {
133133
for (const code of loopLocale.codes) {
134-
if (code === browserLocale.locale) {
134+
if (normalizeTheLocale(code) === normalizeTheLocale(browserLocale.locale)) {
135135
result.push(code);
136136
}
137137
}

‎packages/astro/test/units/i18n/i18n-utils.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,56 @@ describe('computePreferredLocaleList', () => {
123123
});
124124
assert.deepEqual(computePreferredLocaleList(req, locales), []);
125125
});
126+
127+
it('matches string locales case-insensitively', () => {
128+
const req = new Request('http://example.com/', {
129+
headers: { 'Accept-Language': 'EN' },
130+
});
131+
assert.deepEqual(computePreferredLocaleList(req, locales), ['en']);
132+
});
133+
134+
it('matches object-form codes case-insensitively', () => {
135+
const localesObject: Locales = [{ path: 'english', codes: ['en-us'] }, 'fr'];
136+
const req = new Request('http://example.com/', {
137+
headers: { 'Accept-Language': 'en-US' },
138+
});
139+
assert.deepEqual(computePreferredLocaleList(req, localesObject), ['en-us']);
140+
});
141+
142+
it('matches object-form codes written with an underscore separator', () => {
143+
const localesUnderscore: Locales = [{ path: 'english', codes: ['en_US'] }, 'fr'];
144+
const req = new Request('http://example.com/', {
145+
headers: { 'Accept-Language': 'en-US' },
146+
});
147+
assert.deepEqual(computePreferredLocaleList(req, localesUnderscore), ['en_US']);
148+
});
149+
150+
it('returns the configured casing of an object-form code', () => {
151+
const localesExact: Locales = [{ path: 'english', codes: ['en-US'] }, 'fr'];
152+
const req = new Request('http://example.com/', {
153+
headers: { 'Accept-Language': 'en-US' },
154+
});
155+
assert.deepEqual(computePreferredLocaleList(req, localesExact), ['en-US']);
156+
});
157+
158+
it('sorts object-form matches by quality value', () => {
159+
const localesMulti: Locales = [
160+
{ path: 'english', codes: ['en-us'] },
161+
{ path: 'french', codes: ['fr-fr'] },
162+
];
163+
const req = new Request('http://example.com/', {
164+
headers: { 'Accept-Language': 'fr-FR;q=0.9,en-US;q=1.0' },
165+
});
166+
assert.deepEqual(computePreferredLocaleList(req, localesMulti), ['en-us', 'fr-fr']);
167+
});
168+
169+
it('returns every configured code for a wildcard header', () => {
170+
const localesObject: Locales = [{ path: 'english', codes: ['en-us'] }, 'fr'];
171+
const req = new Request('http://example.com/', {
172+
headers: { 'Accept-Language': '*' },
173+
});
174+
assert.deepEqual(computePreferredLocaleList(req, localesObject), ['en-us', 'fr']);
175+
});
126176
});
127177

128178
describe('getPathByLocale', () => {

0 commit comments

Comments
 (0)