Skip to content

fix(i18n): honor first-match-wins in computePreferredLocale codes branch#16600

Merged
ematipico merged 8 commits intowithastro:mainfrom
web-dev0521:fix/i18n-compute-preferred-locale-codes-break-16598
May 5, 2026
Merged

fix(i18n): honor first-match-wins in computePreferredLocale codes branch#16600
ematipico merged 8 commits intowithastro:mainfrom
web-dev0521:fix/i18n-compute-preferred-locale-codes-break-16598

Conversation

@web-dev0521
Copy link
Copy Markdown
Contributor

Changes

  • Fix computePreferredLocale violating its "first match wins" semantics when a Locales config contains object-form entries ({ path, codes }).
  • Root cause: the inner break in the codes-array branch only exits the inner loop, so a later normalize-equivalent code can overwrite an earlier (correct) match. The string-entry branch correctly breaks the outer loop.
  • Refactored to extract a findFirstMatchingLocale(locales, target) helper that uses return instead of break. Early return naturally exits both enclosing loops, making the bug type-impossible. Also normalizes the target once instead of per-iteration.
  • Closes i18n: computePreferredLocale's inner break only exits the inner loop #16598.

Before:

  • [{ path: 'us', codes: ['EN-US'] }, 'en-us'] + Accept-Language: en-us'en-us' (wrong)
  • [{ path: 'us', codes: ['EN'] }, { path: 'gb', codes: ['en'] }] + Accept-Language: en'en' (wrong)

After: Both return the first matching code: 'EN-US' and 'EN' respectively.

Don't forget a changeset! Run pnpm changeset.

Testing

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 4, 2026

🦋 Changeset detected

Latest commit: 9dd036a

The changes in this PR will be included in the next version bump.

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

@github-actions github-actions Bot added the pkg: astro Related to the core `astro` package (scope) label May 4, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 4, 2026

Merging this PR will not alter performance

✅ 18 untouched benchmarks


Comparing web-dev0521:fix/i18n-compute-preferred-locale-codes-break-16598 (9dd036a) with main (3740b24)

Open in CodSpeed

@matthewp matthewp requested a review from ematipico May 4, 2026 23:45
@matthewp
Copy link
Copy Markdown
Contributor

matthewp commented May 4, 2026

Assigning to @ematipico who understands the logic here the best.

Copy link
Copy Markdown
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waiting for the changeset

@web-dev0521
Copy link
Copy Markdown
Contributor Author

Hi, @ematipico ,
Thanks for your time.
I just added changeset.
Could you please review my PR again?

'astro': patch
---

Fixes `computePreferredLocale` violating its "first match wins" semantics when the `i18n.locales` config contains object-form entries (`{ path, codes }`). The inner `break` only exited the codes-array loop, so a later normalize-equivalent code could overwrite the earlier match. The function now correctly returns the first matching code across both string and object entries.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just updated changeset.

Copy link
Copy Markdown
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't the reason of this big refactor. The fix is way smaller. In the second loop, use break to exit from the outer loop.

@web-dev0521
Copy link
Copy Markdown
Contributor Author

Yes, you are right @ematipico , Thanks.

@web-dev0521 web-dev0521 requested a review from ematipico May 5, 2026 06:50
@web-dev0521
Copy link
Copy Markdown
Contributor Author

Hi, @ematipico ,
I think this testing failure is not related to my updates.
Could you please review again?

assert.equal(computePreferredLocale(req, locales), undefined);
});

describe('regression: issue #16598 (first-match wins on object-form locales)', () => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why so much code. Just add three it like the rest of the cases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just updated.
Please check again.
I appreciate your feedback.

@web-dev0521 web-dev0521 requested a review from ematipico May 5, 2026 11:30
Comment thread packages/astro/test/units/i18n/i18n-utils.test.ts Outdated
@ematipico ematipico merged commit 94e4b7c into withastro:main May 5, 2026
27 checks passed
@web-dev0521
Copy link
Copy Markdown
Contributor Author

Thanks, @ematipico .

@astrobot-houston astrobot-houston mentioned this pull request May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

i18n: computePreferredLocale's inner break only exits the inner loop

3 participants