Accept Language Negotiator
RFC4647 compliant accept-language negotiator for parsing HTTP Accept-Language header and returning matched lists or language.
Features:
- No external dependencies
- 100% test coverage
- RFC2616 14.4 Accept-Language compliant
- implements RFC2616 14.4 Accept-Language range quality weights
- RFC4647 compliant
- implements RFC4647 2.3 priority list
- implements RFC4647 3.3.1 basic filter
- implements RFC4647 3.3.2 extended filter
- implements RFC4647 3.4 lookup
Usage
Install
npm i accept-language-negotiator --save
API
languagePriorityList(range)
range
String A single or quality weighted range string
Given a weighted range, wildcard *
or individual BCP47 compliant
language return an ordered 'quality weighted' language priority list as
defined in RFC4647 2.3. The Language Priority List.
Where a range is given and range value has no quality, the highest quality 1 will be given as described in RFC2616 14.4 Accept-Language.
Note: If the list fails it will return a wildcard value.
Example:
; // value; tag: 'en-GB' quality: 1 // range; tag: '*' quality: 1 tag: 'en-GB' quality: 1 tag: 'fr-CA' quality: 08 tag: 'en-US' quality: 07 tag: 'en' quality: 05 // incorrect input;;;;;; tag: '*' quality: 1
basicFilter(range, languageTags)
range
String A single or quality weighted range stringlanguageTags
[String] Array of supported language strings to filter against
Basic filtering compares basic language ranges to language tags returning all matches in best match descending order.
Note: returns an empty array if no matches are found.
RFC4647 3.3.1 Each basic language range in the language priority list is considered in turn, according to priority. A language range matches a particular language tag if, in a case-insensitive comparison, it exactly equals the tag, or if it exactly equals a prefix of the tag such that the first character following the prefix is "-".
Example:
; const supportedLanguageTags = 'de-DE' 'de-DE-1996' 'de-Deva' 'de-Latn-DE' ; ; 'de-DE' 'de-DE-1996';
Example:
const supportedLanguageTags = 'en-GB' 'en-US' 'fr-CA' 'en' ; ; 'en-GB' 'fr-CA' 'en-US' 'en'
extendedFilter(range, languageTags)
range
String A single or quality weighted range stringlanguageTags
[String] Array of supported language strings to filter against
Extended filtering compares extended language ranges to language tags returning all matches in best match descending order.
Note: returns an empty array if no matches are found.
RFC4647 3.3.2 Each extended language range in the language priority list is considered in turn, according to priority. A language range matches a particular language tag if each respective list of subtags matches.
Example:
; const supportedLanguageTags = 'de' 'de-DE' 'de-de' 'de-Latn-DE' 'de-DE-x-goethe' 'de-Latn-DE-1996' 'de-Deva-DE' 'de-x-DE' 'de-Deva' ; ; 'de-DE' 'de-de' 'de-Latn-DE' 'de-DE-x-goethe' 'de-Latn-DE-1996' 'de-Deva-DE' ;
lookup(range, languageTags, defaultValue)
range
String A single or quality weighted range stringlanguageTags
[String] Array of supported language strings to filter againstdefaultValue
String Fallback value if no match can be made
RFC4647 3.4 Lookup is used to select the single language tag that best matches the language priority list for a given request. When performing lookup, each language range in the language priority list is considered in turn, according to priority. By contrast with filtering, each language range represents the most specific tag that is an acceptable match. The first matching tag found, according to the user's priority, is considered the closest match and is the item returned. For example, if the language range is "de-ch", a lookup operation can produce content with the tags "de" or "de-CH" but never content with the tag "de-CH-1996". If no language tag matches the request, the "default" value is returned.
Example:
; const supportedLanguageTags = 'zh-Hant' 'zh';const defaultLanguage = 'en-US'; // match; 'zh-Hant'; // return default; 'en-US';