{"id":12791,"library":"accept-language","title":"Accept-Language Parser","description":"accept-language is a Node.js library designed to parse the HTTP Accept-Language header, enabling applications to determine the user's preferred language based on the browser's request. It adheres strictly to the BCP47 standard for language tag compliance, ensuring robust internationalization (i18n) support. The current stable version is 3.0.20, which was last updated over two years ago, indicating a mature and stable codebase with a slow release cadence focused on maintenance rather than frequent feature additions. A key differentiator of this library is its streamlined approach: it focuses on providing a single best-matched language tag from a pre-defined list, simplifying the process for common i18n use cases where a direct match is preferred over a ranked list of possibilities. This contrasts with other parsers that might return an array of parsed language objects or offer extensive, complex matching options.","status":"maintenance","version":"3.0.20","language":"javascript","source_language":"en","source_url":"git://github.com/tinganho/node-accept-language","tags":["javascript","accept-language","i18n","http","typescript"],"install":[{"cmd":"npm install accept-language","lang":"bash","label":"npm"},{"cmd":"yarn add accept-language","lang":"bash","label":"yarn"},{"cmd":"pnpm add accept-language","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package exports a default, stateful instance for convenience. Named imports are not available.","wrong":"import { acceptLanguage } from 'accept-language';","symbol":"acceptLanguage","correct":"import acceptLanguage from 'accept-language';"},{"note":"CommonJS `require` is fully supported for Node.js environments alongside ESM.","symbol":"acceptLanguage","correct":"const acceptLanguage = require('accept-language');"},{"note":"The `languages` method is accessed directly from the default-imported `acceptLanguage` instance.","wrong":"import { languages } from 'accept-language';","symbol":"languages","correct":"acceptLanguage.languages(['en-US', 'zh-CN']);"}],"quickstart":{"code":"import acceptLanguage from 'accept-language';\n\n// Configure the supported languages for the parser instance.\nacceptLanguage.languages(['en-US', 'es-MX', 'fr-FR', 'en']);\n\n// Simulate an Accept-Language header from an incoming HTTP request.\nconst clientAcceptLanguageHeader = 'fr-CA,fr;q=0.8,en-US;q=0.6,en;q=0.4,sv;q=0.2';\n\n// Get the best-matched language based on the configured list.\nconst matchedLanguage = acceptLanguage.get(clientAcceptLanguageHeader);\n\nconsole.log(`Client Accept-Language header: ${clientAcceptLanguageHeader}`);\nconsole.log(`Best matched language: ${matchedLanguage}`);\n\n// Example with a different header\nconst anotherHeader = 'de,en;q=0.5';\nconst anotherMatched = acceptLanguage.get(anotherHeader);\nconsole.log(`\\nAnother header: ${anotherHeader}`);\nconsole.log(`Best matched language: ${anotherMatched}`);","lang":"typescript","description":"Demonstrates how to initialize the parser with supported languages and then use it to find the best-matching language from an HTTP Accept-Language header."},"warnings":[{"fix":"Always call `acceptLanguage.languages(yourSupportedLanguageArray)` once during application setup or before the first call to `get()`.","message":"The `languages()` method must be called to set the supported language tags before `get()` can be used to perform matching. Failing to do so will result in no matches or unexpected behavior.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"If using Express, install and use `express-request-language` (`npm install express-request-language`) instead of trying to integrate `accept-language` directly into your middleware stack.","message":"This library is not designed to be used directly as an Express.js (or similar framework) middleware. For Express integration, the `express-request-language` package is specifically recommended as a dedicated middleware.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure all language tags (e.g., 'en-US', 'fr-CA', 'zh-Hans') adhere to BCP47 specifications for optimal performance and correct language negotiation.","message":"Language tags provided to `acceptLanguage.languages()` and those in the `Accept-Language` header string should comply with the BCP47 standard for accurate matching. Invalid or malformed tags may lead to incorrect results.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Initialize the `acceptLanguage` instance with a list of supported language tags using `acceptLanguage.languages(['en-US', 'es-MX'])` before calling `get()`.","cause":"The `acceptLanguage.languages()` method was not called prior to attempting to retrieve a matched language.","error":"TypeError: acceptLanguage.get is not a function"},{"fix":"Double-check the array of language tags provided to `acceptLanguage.languages()` for correct BCP47 formatting. Also, inspect the incoming `Accept-Language` header string for any deviations from standard formatting that might hinder matching.","cause":"The list of supported languages passed to `acceptLanguage.languages()` might not contain the expected BCP47 tags, or the incoming `Accept-Language` header is malformed.","error":"Unexpected language returned or no language matched."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null}