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
accept-language
parses HTTP Accept-Language header (BCP47 compliant) and returns a matched defined language.
Installation:
npm install accept-language --save
Usage:
import acceptLanguage from 'accept-language';
acceptLanguage.languages(['en-US', 'zh-CN']);
console.log(acceptLanguage.get('en-GB,en;q=0.8,sv'));
Usage with Express:
If you are using Express server please use the middleware express-request-language.
API
acceptLanguage.languages(Array languageTags);
Provide your language tags in order of priority. The language tags must comply with BCP47 standard.
acceptLanguage.languages(['en-US', 'zh-CN']);
acceptLanguage.get(String acceptLanguageString);
Returns the most likely language given an Accept-Language
string. At least 1 language tag must be provided.
acceptLanguage.get('en-GB,en;q=0.8,sv');
Maintainer
Tingan Ho @tingan87
License
MIT