Accept Language Parser

raw JSON →
1.5.0 verified Wed Apr 22 auth: no javascript maintenance

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.

error TypeError: parser.parse is not a function
cause 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().
error Cannot find module 'accept-language-parser'
cause 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.
gotcha When 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.
gotcha This 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()`.
gotcha The 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.
npm install accept-language-parser
yarn add accept-language-parser
pnpm add accept-language-parser

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