npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

koa-i18next-detector

v1.0.0

Published

An i18next language detection plugin for Koa 2 and Koa 3.

Downloads

22,583

Readme

Detects the request language from the querystring, URL path, cookie, Accept-Language header, or session — and can persist it back to a cookie or session. Zero runtime dependencies, ships ESM + CJS with TypeScript types.

Install

npm i koa-i18next-detector
# or
pnpm add koa-i18next-detector

Requires Node.js >= 18. Compatible with i18next v19.6+ (tested against v26) and Koa 2/3.

Usage

import i18next from 'i18next'
import LanguageDetector from 'koa-i18next-detector'
// CJS: const { LanguageDetector } = require('koa-i18next-detector')

await i18next.use(LanguageDetector).init({
  fallbackLng: 'en',
  supportedLngs: ['en', 'es'],
  resources: {
    en: { translation: { key: 'hello world' } },
    es: { translation: { key: 'hola mundo' } },
  },
  detection: {
    // all options are optional; these are the defaults
    order: ['querystring', 'path', 'cookie', 'header', 'session'],

    lookupQuerystring: 'lng',

    lookupParam: 'lng', // for routes like '/:lng/result'
    lookupFromPathIndex: 0,

    lookupCookie: 'i18next',
    // cookieExpirationDate: new Date(), // default: now + 1 year
    // cookieDomain: '',
    // cookiePath: '/',
    // cookieSecure: true,
    // cookieSameSite: 'lax',
    // cookieHttpOnly: false,

    lookupSession: 'lng',

    // persist the detected language, e.g. ['cookie', 'session']
    caches: false,

    // convert detected codes, e.g. 'Iso15897' turns 'en-US' into 'en_US',
    // or pass a function: (lng) => lng.toLowerCase()
    // convertDetectedLanguage: 'Iso15897',
  },
})

// inside a Koa middleware:
const lng = i18next.services.languageDetector.detect(ctx)
i18next.services.languageDetector.cacheUserLanguage(ctx, lng)

Pair it with koa-i18next-middleware, which wires detect/cacheUserLanguage into the request lifecycle for you.

Custom detectors

import { LanguageDetector } from 'koa-i18next-detector'

const lngDetector = new LanguageDetector()
lngDetector.addDetector({
  name: 'mySessionDetector',

  lookup(ctx, options) {
    return ctx.session?.[options.lookupMySession]
  },

  cacheUserLanguage(ctx, lng, options) {
    if (ctx.session) ctx.session[options.lookupMySession] = lng
  },
})

i18next.use(lngDetector).init({
  detection: {
    order: ['mySessionDetector', 'querystring'],
    lookupMySession: 'lang',
    caches: ['mySessionDetector'],
  },
})

Built-in lookups

| Name | Source | Can cache | | ------------- | -------------------------------------------------- | --------- | | querystring | ctx.query[lookupQuerystring] | – | | path | ctx.params[lookupParam] or path segment by index | – | | cookie | ctx.cookies.get(lookupCookie) | ✓ | | header | Accept-Language (ordered by q value) | – | | session | ctx.session[lookupSession] | ✓ |

Detection stops at the first lookup that yields a language i18next reports as supported (supportedLngs); otherwise fallbackLng is used. Detected values are screened against common XSS payloads before being passed to i18next.

Migrating from 0.x

  • Dual ESM/CJS package with TypeScript types. With require, use const { LanguageDetector } = require('koa-i18next-detector').
  • Requires Node.js >= 18 and i18next >= 19.6 (the removed isWhitelisted API is no longer used).
  • The cookies package dependency was dropped; non-Koa contexts now fall back to parsing the Cookie header directly.
  • New options: cookiePath, cookieSecure, cookieSameSite, cookieHttpOnly, convertDetectedLanguage.
  • addDetector now throws a TypeError for detectors without a name or lookup and returns the detector instance for chaining.
  • Wildcard (*) entries in Accept-Language are ignored.

License

MIT © lxzxl