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 and returns the most likely language or a consumable array of languages.
Installation:
npm install accept-language --save
Usage:
var acceptLanguage = require('accept-language');
acceptLanguage.languages(['en-US', 'zh-CN']);
console.log(acceptLanguage.get('en-GB,en;q=0.8,sv'));
var language = acceptLanguage.parse('en-GB,en;q=0.8,sv');
console.log(language);
Recommended usage with L10ns:
L10ns is an internationalization workflow and formatting tool. This library was specifically built for L10ns. L10ns is a very good alternative to Gettext and all of it's tooling support–XGettext, PoEdit, custom libraries etc.
API
acceptLanguage.languages(Array languageTags);
Define your language tags ordered in highest priority comes first fashion. The language tags must comply with BCP47 standard. The BCP47 language tag consist of at least the following subtags:
- A language subtag (
en
, zh
). - A script subtag (
Hant
, Latn
). - A region subtag (
US
, CN
).
Then language tag has the following syntax:
language[-script][-region]
Which makes the following language tags en
, en-US
and zh-Hant-TW
all BCP47 compliant. Please note that the script tag refers to language script. Some languages use two character sets instead of one. Chinese is a good example of having two character sets instead of one–it has both traditional characters and simplified characters. And for popular languages that uses two or more scripts please specify the script subtag, because it can make an i18n library fetch more specific locale data.
acceptLanguage.languages(['en-US', 'zh-CN']);
acceptLanguage.get(String acceptLanguageString);
Get the most likely language given an Accept-Language
string. In order for it to work you must set all your languages first.
acceptLanguage.get('en-GB,en;q=0.8,sv');
acceptLanguage.parse(String acceptLanguageString);
Parse an Accept-Language
string and get a consumable array of languages. In order for it to work you must set all your language tags first.
acceptLanguage.parse('en-GB,en;q=0.8,sv');
Maintainer
Tingan Ho @tingan87
License
MIT