accept-language-parser
Parse HTTP Accept-Language headers and pick the best matching locale
Repository Health
Technical Analysis
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
loosematching 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.localebefore 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 Quality — tests/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.
Used by 4 apps in this directory
Cal.diy
Scheduling
The 100% MIT-licensed, community-driven scheduling platform — self-host your own booking infrastructure with no enterprise strings attached.
Grist
Databases · No Code Platforms
A modern relational spreadsheet that combines Python-powered formulas, drag-and-drop dashboards, and granular access controls in a self-hostable, SQLite-backed data platform.
Openship
Devops · Hosting Control Panel
Openship is an open-source, self-hostable deployment platform that points at a repo and builds, ships, routes, and TLS-terminates the app — driven from a desktop app, web dashboard, or CLI.
Tianji
Analytics · Monitoring
Replace Google Analytics, UptimeKuma, and Prometheus with one self-hosted platform that tracks websites, monitors uptime, and reports server health.