
Product
Redesigned Repositories Page: A Faster Way to Prioritize Security Risk
Our redesigned Repositories page adds alert severity, filtering, and tabs for faster triage and clearer insights across all your projects.
accept-language-parser
Advanced tools
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.
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);
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.
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.
Parses the accept-language header from an HTTP request and produces an array of language objects sorted by quality.
dependencies: none
installation:
npm install accept-language-parser
parse usage:
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.
pick usage:
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"
Known issues
Running tests
npm install
grunt test
FAQs
Parse the accept-language header from a HTTP request
The npm package accept-language-parser receives a total of 431,131 weekly downloads. As such, accept-language-parser popularity was classified as popular.
We found that accept-language-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Our redesigned Repositories page adds alert severity, filtering, and tabs for faster triage and clearer insights across all your projects.
Security News
Slopsquatting is a new supply chain threat where AI-assisted code generators recommend hallucinated packages that attackers register and weaponize.
Security News
Multiple deserialization flaws in PyTorch Lightning could allow remote code execution when loading untrusted model files, affecting versions up to 2.4.0.