Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
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"
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 320,717 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.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.