Registry / http-networking / accept-language-parser

accept-language-parser

JSON →
library1.5.0jsnpmunverified

The `accept-language-parser` package provides utilities for parsing the `Accept-Language` HTTP header, which clients send to indicate their preferred human languages. It processes the header string and produces an array of language objects, each containing a `code`, optional `region`, and `quality` value, sorted in descending order of quality. Additionally, it offers a `pick` function to determine the best matching language from a list of supported languages, with an optional 'loose' matching mode. The current stable version is 1.5.0. This package appears to be in a maintenance state, as its last release was over eight years ago, suggesting infrequent updates but continued functionality. Its primary differentiation lies in its focused API for HTTP `Accept-Language` header processing, rather than a broader internationalization library.

npm install accept-language-parser
INSTALL
IMPORT
SIG · ACCEPT-LANGUAGE-PA
A
accept-language-parser
http-networkingjavascriptv1.5.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parser
const parser = require('accept-language-parser');
import parser from 'accept-language-parser';
This package is primarily a CommonJS module. The 'require' syntax is the intended and most reliable way to import it in Node.js environments. In ESM, a default import pattern as shown in 'wrong' would likely work, but direct named imports will not.
parser.parse
const parser = require('accept-language-parser'); const languages = parser.parse('en-GB,en;q=0.8');
import { parse } from 'accept-language-parser';
The `parse` function is a method on the default exported `parser` object. Direct named imports are not supported as it's a CommonJS module that doesn't provide named exports for ESM.
parser.pick
const parser = require('accept-language-parser'); const language = parser.pick(['fr-CA', 'fr-FR', 'fr'], 'en-GB,en-US;q=0.9,fr-CA;q=0.7,en;q=0.8');
import { pick } from 'accept-language-parser';
Similar to `parse`, `pick` is a method of the default exported `parser` object. Attempting to destructure it directly from the package will result in an error in ESM contexts.

Demonstrates parsing an `Accept-Language` header and picking the best matching language from a supported list, including an example of loose matching.

const parser = require('accept-language-parser'); // Example 1: Parsing an Accept-Language header const acceptLanguageHeader = 'en-GB,en-US;q=0.9,fr-CA;q=0.7,en;q=0.8'; console.log(`\n--- Parsing Header: ${acceptLanguageHeader} ---\n`); const parsedLanguages = parser.parse(acceptLanguageHeader); console.log('Parsed Languages (sorted by quality):', parsedLanguages); /* Expected Output: [ { code: 'en', region: 'GB', quality: 1 }, { code: 'en', region: 'US', quality: 0.9 }, { code: 'en', region: undefined, quality: 0.8 }, { code: 'fr', region: 'CA', quality: 0.7 } ] */ // Example 2: Picking the best language from supported list const supportedLanguages = ['fr-CA', 'fr-FR', 'fr', 'en-US', 'en']; console.log(`\n--- Picking from Supported: [${supportedLanguages.join(', ')}] ---\n`); const bestMatch = parser.pick(supportedLanguages, acceptLanguageHeader); console.log('Best match (strict):', bestMatch); // Expected Output: fr-CA // Example 3: Picking with loose matching const supportedLanguagesLoose = ['fr', 'en']; console.log(`\n--- Picking with Loose Matching from Supported: [${supportedLanguagesLoose.join(', ')}] ---\n`); const bestMatchLoose = parser.pick(supportedLanguagesLoose, acceptLanguageHeader, { loose: true }); console.log('Best match (loose):', bestMatchLoose); // Expected Output: fr
Debug
Known issues
gotchaWhen using the `loose` option in `parser.pick`, the order of the `supportedLanguagesArray` is crucial. The first partially matching language in the array will be returned, potentially leading to less specific matches if not ordered carefully.
fix
Ensure `supportedLanguagesArray` is ordered from most specific to least specific (e.g., `['fr-CA', 'fr']`) if prioritizing specific regional variants.
affects: >=1.0.0
gotchaThis package is predominantly a CommonJS module and does not natively support ES Module (ESM) named imports. Attempting `import { parse } from 'accept-language-parser'` will likely result in an error in ESM contexts.
fix
In ESM environments, use `import parser from 'accept-language-parser';` and then access methods like `parser.parse()` or `parser.pick()`.
affects: >=1.0.0
gotchaThe package's last release (v1.5.0) was published over eight years ago. While it remains functional for its intended purpose, users should be aware of its inactive development status, meaning new features, bug fixes, or compatibility updates for newer Node.js versions or standards are unlikely.
fix
Consider reviewing the source code for any potential issues if integrating into a critical system, or evaluate alternatives if active maintenance and modern features are a high priority.
affects: >=1.5.0
Errors
Common errors & fixes
TypeError: parser.parse is not a function
Attempting to use `import { parse } from 'accept-language-parser';` in an ES Module context, while the package exports its functionality as properties of a default CommonJS object.
fix
Change the import to `import parser from 'accept-language-parser';` and access `parser.parse()` or `parser.pick()`.
Cannot find module 'accept-language-parser'
The package has not been installed, or the environment's module resolution paths are not correctly configured.
fix
Run `npm install accept-language-parser` or `yarn add accept-language-parser` in your project directory.
Upgrade
Version history
1.5.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
33 hits · last 30 days
node
26
Amazon
1
Resources
accept-language-parser — npm install accept-language-parser · libregistry