Fix preferredLocaleList dropping object-form locale codes that differ in case - #17700
Open
winklemad wants to merge 1 commit into
Open
Fix preferredLocaleList dropping object-form locale codes that differ in case#17700winklemad wants to merge 1 commit into
preferredLocaleList dropping object-form locale codes that differ in case#17700winklemad wants to merge 1 commit into
Conversation
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.
🦋 Changeset detectedLatest commit: 86365a9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 420 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
computePreferredLocaleListcompared object-form locale codes exactly, while every other locale comparison ini18n/utils.tsnormalizes both sides first. That one raw comparison is now normalized like the rest.sortAndFilterLocalesfilters on normalized codes, so a locale configured as{ path: 'english', codes: ['en-us'] }passes the filter when a browser sendsAccept-Language: en-US, and is then silently dropped by the exact comparison.Astro.preferredLocalereturns'en-us'whileAstro.preferredLocaleListreturns[].locales: ['en-us']works today), the object-form branch did not. Underscore codes such asen_USwere affected the same way, sincenormalizeTheLocalemaps_to-.codeis what's pushed, matching howgetLocaleByPathcompares normalized and returns the configured value.Production change is one line:
One thing I deliberately left out:
computePreferredLocalebreaks out of its codes loop on first match (#16600), and this list function doesn't. That's a visible behaviour difference between the pair, but it's a separate change with its own semantics, so I'd rather ask than bundle it — happy to follow up if you want them aligned.Testing
Added to the existing
packages/astro/test/units/i18n/i18n-utils.test.ts, in the style of the surrounding cases:codes: ['en-us']vsen-US) — failed before this change, returning[]codes: ['en_US']) — failed before, returning[]The last three pass both before and after, so they guard against a regression rather than describing the fix.
Locally: 32/32 in that file, and the full i18n unit suite (396 tests),
tsc -b,format:ci, biome and eslint all pass. One caveat —astro-scripts testfails on my machine withERR_UNKNOWN_FILE_EXTENSIONfor.tson Node 22.17.1 (the repo's.nvmrcis 24.14.0); it fails identically on files I didn't touch, so I ran the units via the equivalentnode --experimental-strip-types --test. CI on Node 24 will run them through the wrapper.Docs
No docs change needed.
Astro.preferredLocaleListis already documented as returning the list of matching locales; this makes the object-form config shape behave as documented, rather than changing any documented behaviour.