Skip to content

fix/i18n-accept-language-parsing#1242

Open
Sushma-1706 wants to merge 2 commits intometabrainz:masterfrom
Sushma-1706:fix/i18n-accept-language-parsing
Open

fix/i18n-accept-language-parsing#1242
Sushma-1706 wants to merge 2 commits intometabrainz:masterfrom
Sushma-1706:fix/i18n-accept-language-parsing

Conversation

@Sushma-1706
Copy link
Copy Markdown

Motivation

Improve robustness, correctness, and standards compliance of Accept-Language header parsing used for language preference selection. The previous implementation did not strictly validate tokens, lacked proper normalization, and did not enforce q-value validation consistently.

Description

This update strengthens the parseAcceptLanguage and getAcceptedLanguageCodes helpers to ensure predictable and standards-aligned behavior.

Improvements

  • Trim and ignore empty language entries before parsing.

  • Use a fully anchored regular expression (^...$) to prevent partial or malformed matches.

  • Support primary language tags (2–3 letters) with optional subtags (e.g., fr-CA, en-US).

  • Support optional whitespace around ;q= (e.g., EN-us ; q=0.9).

  • Default weight to 1 when q is not specified.

  • Validate q values to ensure they are numeric and within the range [0, 1]; skip invalid entries.

  • Normalize language codes and subtags to lowercase.

  • Sort parsed languages in descending order by weight.

  • Ensure type safety using a TypeScript type guard when filtering invalid matches.

Testing

  • Added comprehensive tests covering:

  • Correct parsing and sorting by q weight.

  • Whitespace tolerance and case normalization.

  • Proper handling of subtags.

  • Rejection of malformed language tokens (e.g., @@, *).

  • Rejection of invalid q values (e.g., q=abc, q=1.5).

  • Correct output ordering and normalization in getAcceptedLanguageCodes.

Result

These changes improve standards compliance, security, and deterministic language preference resolution while maintaining backward-compatible behavior for valid headers.

@leftmostcat
Copy link
Copy Markdown
Collaborator

I'm a little concerned about this as I'm not sure parsing Accept-Language is something we should be doing by hand, and this adds a fair bit of complexity to the process.

I'll leave it to monkey whether to go with this approach, but if it were to be accepted, I would at least want to see references in comments to the relevant RFC to explain what's happening and why.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants