accept-language-parser

Parse HTTP Accept-Language headers and pick the best matching locale

Library
npm
v1.5.0
165stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
39/100Needs Attention
Development Activity0
Maintenance20
Community56
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
71/100Good
Architecture65
Code Quality60
Innovation75
Learning Curve85

accept-language-parser is a small, dependency-free Node.js library for handling the Accept-Language HTTP header. It exposes a parse() function that turns the raw header into an array of language objects (code, region, script, quality) sorted by quality, and a pick() function that matches those preferences against the locales your app actually supports, returning the best available choice.

Because it has zero runtime dependencies and a two-function API, it drops into any Express, Koa, or plain Node HTTP handler without adding build complexity. It has shipped in production at OpenTable and is widely reused as the locale-negotiation building block inside larger i18n middleware.

What You Get

  • A parse(header) function that returns quality-sorted {code, script, region, quality} objects
  • A pick(supportedLanguages, acceptLanguage, options) function that returns the best-matching supported locale or null
  • Optional loose matching mode for partial code-only matches when exact region/script matches aren’t available
  • Zero runtime dependencies, so it adds no transitive weight to a project’s dependency tree

Common Use Cases

  • Negotiating which locale to render for a first-time visitor based on their browser’s Accept-Language header
  • Building Express/Koa middleware that sets req.locale before routes or templates run
  • Filtering a fixed list of supported languages down to the single best match for API responses

Under The Hood

Architecture — The library is a single CommonJS module (index.js, ~65 lines) exposing two pure functions: parse() tokenizes the Accept-Language header with one regular expression and maps each token into a {code, script, region, quality} object sorted descending by quality; pick() normalizes a supported-languages array into the same shape and performs a linear best-match scan against the parsed (or pre-parsed) Accept-Language list, with an optional loose mode that relaxes region/script equality to a code-only match. There is no internal state — every call recomputes purely from its arguments, so the module is trivially safe to reuse across requests.

Tech Stack — Plain JavaScript (100% JS per GitHub’s language breakdown), CommonJS module.exports, no TypeScript and no build step; package.json declares no runtime dependencies at all, only jshint, mocha, and should as devDependencies for linting and testing. The package ships index.js directly as its main entry.

Code Qualitytests/tests.js (162 lines) uses Mocha + should to cover parse() quality sorting and pick()’s exact and loose matching paths, including region/script combinations. A .jshintrc config wires linting into npm test alongside Mocha. The code predates modern JS conventions (uses var, no JSDoc/type annotations), and error handling is minimal: parse() guards a missing header with al || "" and pick() returns null rather than throwing when inputs are absent.

API Design — The public surface is exactly two functions, both synchronous, with names and field labels (code/region/script/quality) that map directly onto HTTP Accept-Language semantics, so there’s effectively no ramp-up time. The README documents both functions with runnable examples, including the loose option’s ordering-sensitive behavior. There is no CLI and no async variant — it’s a pure, synchronous utility function pair.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search