What is accept-language-parser?
The accept-language-parser npm package is used to parse the Accept-Language header from HTTP requests. This header is used by browsers to indicate the user's language preferences. The package helps in extracting and interpreting these preferences, making it easier to serve content in the user's preferred language.
What are accept-language-parser's main functionalities?
Parse Accept-Language Header
This feature allows you to parse the Accept-Language header from an HTTP request. The code sample demonstrates how to parse a given Accept-Language header string and output the parsed languages as an array of objects.
const acceptLanguageParser = require('accept-language-parser');
const header = 'en-GB,en;q=0.9,fr;q=0.8,de;q=0.7';
const languages = acceptLanguageParser.parse(header);
console.log(languages);
Get Best Language Match
This feature allows you to find the best match for the user's language preferences from a list of supported languages. The code sample demonstrates how to use the 'pick' method to get the best matching language from the supported languages.
const acceptLanguageParser = require('accept-language-parser');
const header = 'en-GB,en;q=0.9,fr;q=0.8,de;q=0.7';
const supportedLanguages = ['en', 'fr', 'de'];
const bestMatch = acceptLanguageParser.pick(supportedLanguages, header);
console.log(bestMatch);
Other packages similar to accept-language-parser
negotiator
The negotiator package is used to handle content negotiation in HTTP requests. It can parse the Accept, Accept-Charset, Accept-Encoding, and Accept-Language headers. Compared to accept-language-parser, negotiator offers a broader range of content negotiation capabilities but may be more complex to use for language-specific parsing.
locale
The locale package is designed to handle locale-based content negotiation. It can parse the Accept-Language header and match it against a list of supported locales. While similar to accept-language-parser in terms of language parsing, locale focuses more on locale-specific functionalities and may offer additional features related to locale management.
accept-language-parser
Parses the accept-language header from an HTTP request and produces an array of language objects sorted by quality.
Installation:
npm install accept-language-parser
API
var parser = require('accept-language-parser');
var languages = parser.parse('en-GB,en;q=0.8');
console.log(languages);
Output will be:
[
{
code: "en",
region: "GB",
quality: 1.0
},
{
code: "en",
region: undefined,
quality: 0.8
}
];
Output is always sorted in quality order from highest -> lowest. as per the http spec, omitting the quality value implies 1.0.
Alias: parser.pick(supportedLanguagesArray, parsedAcceptLanguageHeader)
var parser = require('accept-language-parser');
var language = parser.pick(['fr-CA', 'fr-FR', 'fr'], 'en-GB,en-US;q=0.9,fr-CA;q=0.7,en;q=0.8');
console.log(language);
Output will be:
"fr-CA"
The options
currently supports only loose
option that allows partial matching on supported languages. For example:
parser.pick(['fr', 'en'], 'en-GB,en-US;q=0.9,fr-CA;q=0.7,en;q=0.8');
Would return:
"fr"
In loose mode the order of supportedLanguagesArray
matters, as it is the first partially matching language that is returned. It means that if you want to pick more specific langauge first, you should list it first as well, for example: ['fr-CA', 'fr']
.
Running test
npm install
npm test
License
MIT