Implement negotiateLanguage(header, supported, fallback) for an HTTP service.
supported is a non-empty array of unique, non-empty language tags; matching is case-insensitive but return the original supported spelling.
fallback must be one of the supported tags.
- Parse comma-separated ranges with optional
q weights (en;q=0.8). Whitespace around ranges and parameters is ignored. A missing weight is 1.
- Drop a range entirely when its
q value is not a number with 0 < q ≤ 1 (an empty, non-numeric, zero, negative or greater-than-1 value). A dropped range never matches; for example, fr;q=abc, en is negotiated as if the header were en.
- Consider ranges by descending quality, preserving header order for ties. An exact tag wins; otherwise a basic range such as
en may match the first supported tag beginning en-.
* selects the fallback. A missing or blank header, or no match, also returns the fallback.
- Do not mutate caller inputs.