Accept Language Parser
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.
Common errors
-
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.fixChange the import to `import parser from 'accept-language-parser';` and access `parser.parse()` or `parser.pick()`. -
Cannot find module 'accept-language-parser'
cause The package has not been installed, or the environment's module resolution paths are not correctly configured.fixRun `npm install accept-language-parser` or `yarn add accept-language-parser` in your project directory.
Warnings
- 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.
- 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.
- 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.
Install
-
npm install accept-language-parser -
yarn add accept-language-parser -
pnpm add accept-language-parser
Imports
- parser
import parser from 'accept-language-parser';
const parser = require('accept-language-parser'); - parser.parse
import { parse } from 'accept-language-parser';const parser = require('accept-language-parser'); const languages = parser.parse('en-GB,en;q=0.8'); - parser.pick
import { pick } from 'accept-language-parser';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');
Quickstart
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