Skip to content

About

Minimal reproduction: Astro.preferredLocaleList drops object-form locale codes that differ in case

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Astro.preferredLocaleList drops object-form locale codes that differ in case

Minimal reproduction for a bug in Astro's server-side i18n helper computePreferredLocaleList (Astro.preferredLocaleList / the endpoint context.preferredLocaleList).

When a locale is configured in object form ({ path, codes }), preferredLocaleList matches the browser's Accept-Language code with an exact, case-sensitive string comparison, while the sibling APIs (preferredLocale, and the string-form locale path) normalize case and the -/_ separator. So a locale configured as en-us is dropped when the browser sends en-US.

Config

// astro.config.mjs
i18n: {
  defaultLocale: 'english',
  locales: [{ path: 'english', codes: ['en-us'] }, 'fr'],
}

Steps

pnpm install
pnpm dev
# in another terminal:
curl -H "Accept-Language: en-US" http://localhost:4321/pref.json

Result

// curl -H "Accept-Language: en-US"  (case differs from the configured "en-us")
{
  "acceptLanguage": "en-US",
  "preferredLocale": "en-us",     // singular: correct (normalizes case)
  "preferredLocaleList": []       // plural: BUG — empty, should be ["en-us"]
}

The two helpers disagree on the same request: preferredLocale resolves en-us, but preferredLocaleList returns [].

Controls that behave correctly:

// curl -H "Accept-Language: en-us"  (exact case)   -> ["en-us"]
// curl -H "Accept-Language: FR"     (string locale) -> ["fr"]

Root cause

packages/astro/src/i18n/utils.ts, in computePreferredLocaleList: the string-locale branch uses normalizeTheLocale(...), but the object-form branch compares code === browserLocale.locale directly. computePreferredLocale (singular) already normalizes the object-form code, which is why the two APIs disagree.

About

Minimal reproduction: Astro.preferredLocaleList drops object-form locale codes that differ in case

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages