What is accept-language?
The 'accept-language' npm package is a utility for parsing and handling the 'Accept-Language' HTTP header. It helps in determining the preferred language(s) of the user based on the header values, which is useful for internationalization (i18n) and localization (l10n) in web applications.
What are accept-language's main functionalities?
Parsing Accept-Language Header
This feature allows you to parse the 'Accept-Language' header and get an array of language codes with their respective quality values.
const acceptLanguage = require('accept-language');
const header = 'en-US,en;q=0.9,fr;q=0.8,de;q=0.7';
const languages = acceptLanguage.parse(header);
console.log(languages); // Output: [ { code: 'en-US', quality: 1 }, { code: 'en', quality: 0.9 }, { code: 'fr', quality: 0.8 }, { code: 'de', quality: 0.7 } ]
Getting Best Language Match
This feature allows you to determine the best matching language from a list of supported languages based on the 'Accept-Language' header.
const acceptLanguage = require('accept-language');
acceptLanguage.languages(['en', 'fr', 'de']);
const header = 'en-US,en;q=0.9,fr;q=0.8,de;q=0.7';
const bestMatch = acceptLanguage.get(header);
console.log(bestMatch); // Output: 'en'
Setting Default Language
This feature allows you to set a default language that will be used if none of the languages in the 'Accept-Language' header match the supported languages.
const acceptLanguage = require('accept-language');
acceptLanguage.defaultLanguage = 'en';
const header = 'es-ES,es;q=0.9';
const bestMatch = acceptLanguage.get(header);
console.log(bestMatch); // Output: 'en'
Other packages similar to accept-language
negotiator
The 'negotiator' package is a library for HTTP content negotiation. It can handle 'Accept', 'Accept-Charset', 'Accept-Encoding', and 'Accept-Language' headers. Compared to 'accept-language', 'negotiator' offers broader functionality for content negotiation but may be more complex to use if you only need to handle language preferences.
locale
The 'locale' package is another library for parsing and handling the 'Accept-Language' header. It provides similar functionality to 'accept-language' but also includes additional features for working with locales and regions. It may be a better choice if you need more comprehensive locale handling.
accept-language
Deprecated
Because languages
is a subset of locales. I have decided to rename this package to accept-locale
instead. https://github.com/tinganho/node-accept-locale. So please use accept-locale
instead.
accept-language
parses HTTP Accept-Language header and returns a consumable array of language codes.
Installation:
npm install accept-language --save
Usage:
var acceptLanguage = require('accept-language');
var language = acceptLanguage.parse('en-GB,en;q=0.8,sv');
console.log(language);
Output:
[
{
code: "en",
region: "GB",
quality: 1.0
},
{
code: "sv",
region: undefined,
quality: 1.0
},
{
code: "en",
region: undefined,
quality: 0.8
}
];
Filter non-defined language codes:
var acceptLanguage = require('accept-language');
acceptLanguage.codes(['en', 'zh']);
var language = acceptLanguage.parse('en-GB,en;q=0.8,sv');
console.log(language);
Output:
[
{
code: "en",
region: "GB",
quality: 1.0
},
{
code: "en",
region: undefined,
quality: 0.8
}
];
Use default value:
var acceptLanguage = require('accept-language');
acceptLanguage.default({
code : 'en',
region : 'US'
// No need to specify quality
});
acceptLanguage.codes(['en', 'zh']);
var language = acceptLanguage.parse('fr-CA');
console.log(language);
Output:
[
{
code: "en",
region: "US",
quality: 1.0
}
];
The output is always sorted with the highest quality first.