fix/i18n-accept-language-parsing#1242
Open
Sushma-1706 wants to merge 2 commits intometabrainz:masterfrom
Open
Conversation
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. |
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.
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.