i18next-browser-languageDetector

Detects a visitor's language from cookies, storage, the URL, or the browser itself and wires it straight into i18next.

Library
npm
v8.2.2
972 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
63 /100 Good
Development Activity 76
Maintenance 28
Community 60
Maturity 60
Momentum 28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
77 /100 Good
Architecture 82
Code Quality 68
Innovation 80
Learning Curve 78

i18next-browser-languagedetector is the official i18next plugin for figuring out what language a browser visitor speaks, without you writing that logic yourself. It checks, in a configurable order, the query string, cookies, localStorage, sessionStorage, the navigator object, the <html lang> attribute, the URL path, subdomain, and URL hash, then hands the first (or best) match to i18next.

Register it once with i18next.use(LanguageDetector).init(...) and every subsequent page load or client-side navigation automatically resolves and caches the visitor’s language, with no server round-trip needed. Custom detection strategies can be added through the same addDetector interface the nine built-in detectors use.

What You Get

  • Nine built-in browser-language detectors (cookie, querystring, localStorage, sessionStorage, navigator, htmlTag, path, subdomain, hash), each independently configurable and orderable
  • Automatic caching of the resolved language back to cookies and/or localStorage so repeat visits skip re-detection
  • A tiny addDetector({name, lookup, cacheUserLanguage}) interface for plugging in fully custom detection sources
  • Dual CJS/ESM builds with hand-maintained TypeScript declarations (index.d.ts / index.d.mts) and an i18next module augmentation for typed detection options
  • A built-in hasXSS filter that strips obviously malicious strings out of detected language values before they reach i18next

Common Use Cases

  • Auto-selecting a locale on first visit to a client-rendered SPA based on the browser’s navigator.language
  • Persisting a user’s manually-chosen language across sessions via cookie or localStorage caching
  • Reading the language segment out of the URL path or subdomain for locale-prefixed routing (e.g. /fr/pricing or fr.example.com)
  • Letting ?lng=de query-string overrides win during QA/testing or shareable localized links

Under The Hood

Architecture The package exports a single Browser class (aliased LanguageDetector) implementing i18next’s LanguageDetectorModule interface (type: 'languageDetector'). On construction/init (src/index.js), it merges user options over sane defaults via a small utils.defaults helper, then registers nine built-in detector objects — cookie, querystring, localStorage, sessionStorage, navigator, htmlTag, path, subdomain, hash (each a standalone module in src/browserLookups/) — into a this.detectors map keyed by name. detect() walks the configured order array, calls each active detector’s lookup(options), flattens/dedupes results, filters out anything matching a small hasXSS regex blocklist, and applies an optional convertDetectedLanguage transform before returning the array to i18next (or a single string for i18next’s legacy synchronous API). cacheUserLanguage() mirrors this by fanning out to each cache-listed detector’s own cacheUserLanguage() method. Each detector module is self-contained with its own lookup/cacheUserLanguage pair, so adding a custom detector is just pushing another object with that shape via addDetector().

Tech Stack Plain ES module source with zero runtime dependencies beyond @babel/runtime (used only for helper injection during the build). Built via Rollup into CJS (dist/cjs) and ESM (dist/esm) targets, with a dual exports map in package.json wired for both require and import consumers, plus hand-written index.d.ts/index.d.mts declarations (the library is JS + Babel, not authored in TypeScript). Dev tooling is Mocha/expect.js for behavior tests and tsd for asserting the shipped .d.ts type-checks; ESLint (airbnb config) enforces style. i18next itself is a devDependency only, used to build a real i18next instance in tests.

Code Quality test/languageDetector.test.js (225 lines) exercises each detector individually against a real i18next instance, mocking global.document/global.window per test group — cookie, path, querystring, subdomain, storage, navigator, htmlTag, hash, cacheUserLanguage, and custom-detector registration. There’s no dedicated test for the hasXSS filter and no coverage reporting wired despite coveralls being a devDependency. Source style is consistent and each browserLookups/*.js file has a single clear responsibility; document.cookie access is guarded with try/catch at load time to survive non-browser environments, but individual lookups otherwise assume well-formed browser globals.

API Design The public surface is a single default-exported class matching i18next’s plugin contract exactly, so setup for anyone already on i18next is one line: i18next.use(LanguageDetector).init({...}). Options are a flat, well-documented object with sensible defaults, so most consumers never touch them. The addDetector interface for custom detectors mirrors the shape of the built-ins, making the extension model easy to learn from the source itself. A couple of legacy aliased options exist for backwards compatibility (e.g. lookupFromUrlIndex silently mapping to lookupFromPathIndex), adding minor cognitive overhead, but the README documents every option with inline defaults and a full custom-detector example.

Used by 53 apps in this directory

Go
74%
Apache 2.0

ntfy

Developer Tools · Marketing

35,022

Send push notifications to your phone or desktop from any script or service using a single HTTP PUT or POST—no sign-up required.

View details
90
Repo Health
89
Technical
75
Dependency
Built with
Go 74%
JavaScript 25%
Updated 1 weeks ago
TypeScript
96%
Apache 2.0

OneUptime

Monitoring

7,751

The complete open-source observability platform that replaces PagerDuty, Datadog, Sentry, and StatusPage with a single self-hostable system.

View details
91
Repo Health
81
Technical
65
Dependency
Built with
TypeScript 96%
Updated 6 days ago
TypeScript
56%
MIT

open-notebook

AI Assistants · Note Taking

40,513

A privacy-first, self-hosted AI research notebook with 18+ model providers, multi-speaker podcast generation, and full REST API—your open-source alternative to Google Notebook LM.

View details
89
Repo Health
84
Technical
71
Dependency
Built with
TypeScript 56%
Python 42%
Updated 1 weeks ago
Python
38%
Other

Open WebUI

AI Agents · AI Assistants

156,607

The extensible, privacy-first AI platform that runs Ollama, OpenAI, and any LLM backend behind a polished, feature-packed web interface.

View details
92
Repo Health
75
Technical
66
Dependency
Built with
Python 38%
Svelte 35%
JavaScript 19%
Updated 6 days ago
Go
52%
Apache 2.0

opencloud

File Storage

6,218

Open source file management and collaboration platform that keeps your data under your control, no database required.

View details
86
Repo Health
80
Technical
68
Dependency
Built with
Go 52%
Gherkin 33%
PHP 11%
Updated 1 weeks ago
TypeScript
92%
Other

OpenHands

AI Code Assistants · AI Development

91,695

The self-hosted developer control center for running AI coding agents — locally, in Docker, on VMs, or across cloud backends — with automation workflows for GitHub, Slack, and more.

View details
91
Repo Health
82
Technical
67
Dependency
Built with
TypeScript 92%
Updated 1 weeks ago
Go
36%

OpenPanel

Devops · Hosting Control Panel

764

Docker-powered web hosting control panel that gives every user a fully isolated environment with dedicated web server, database, and networking — VPS-grade security on shared hardware.

View details
87
Repo Health
75
Technical
63
Dependency
Built with
Go 36%
HTML 30%
TypeScript 24%
Updated 1 weeks ago
TypeScript
54%
Other

OpenReplay

Analytics

13,042

Self-hosted session replay and product analytics suite that lets you see exactly what users do on your web app — without sending data to third parties.

View details
91
Repo Health
77
Technical
66
Dependency
Built with
TypeScript 54%
Go 13%
Updated 1 weeks ago
JavaScript
90%
Other

OpenSign

Digital Signiture

7,163

Self-host a full-featured DocuSign alternative with unlimited e-signatures, multi-signer workflows, and cryptographic PDF signing.

View details
83
Repo Health
61
Technical
69
Dependency
Built with
JavaScript 90%
Updated 1 weeks ago

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