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

@to-kn/koa-locales

v2.1.2

Published

koa locales, i18n solution for koa

Readme

koa-locales

A modern i18n solution for Koa: @to-kn/koa-locales

Now TypeScript-first, ESM, and Node.js 18+!

  • Uses ESM (import/export), outputs to dist/esm/, and is CI-tested on GitHub Actions.
  • Use the built output (dist/esm/index.js) as the main entry point for consumers, but tests and development use the source (src/).
  • Modern test suite with Vitest.

[![NPM version][npm-image]][npm-url]

koa locales, i18n solution for koa:

  1. All locales resources location on options.dirs.
  2. resources file supports: *.js, *.json, *.yml, *.yaml and *.properties, see examples.
  3. One api: __(key[, value, ...]).
  4. Auto detect request locale from query, cookie and header: Accept-Language.

Requirements

  • Node.js >= 18
  • ESM ("type": "module" in package.json)
  • TypeScript (for development)

Installation

npm install @to-kn/koa-locales

Quick start (ESM/TypeScript)

import locales from '@to-kn/koa-locales';
import Koa from 'koa';

const app = new Koa();
const options = {
  dirs: [__dirname + '/locales', __dirname + '/foo/locales'],
};
locales(app, options);

Development & Testing

  • Run tests with Vitest:
    npm run test
  • Lint and format with Biome:
    npm run lint
  • Build TypeScript to ESM:
    npm run build
  • CI runs on GitHub Actions (see .github/workflows/)

API Reference

locales(app, options)

Patch locales functions to koa app.

  • {Application} app: koa app instance.
  • {Object} options: optional params.
    • {String} functionName: locale function name patch on koa context. Optional, default is __.
    • {String} dirs: locales resources store directories. Optional, default is ['$PWD/locales'].
    • {String} defaultLocale: default locale. Optional, default is en-US.
    • {String} queryField: locale field name on query. Optional, default is locale.
    • {String} cookieField: locale field name on cookie. Optional, default is locale.
    • {String} cookieDomain: domain on cookie. Optional, default is ''.
    • {Object} localeAlias: locale value map. Optional, default is {}.
    • {Boolean} writeCookie: set cookie if header not sent. Optional, default is true.
    • {String|Number} cookieMaxAge: set locale cookie value max age. Optional, default is 1y, expired after one year.
locales({
  app: app,
  dirs: [__dirname + '/app/locales'],
  defaultLocale: 'zh-CN',
});

Aliases

The key options.localeAlias allows to not repeat dictionary files, as you can configure to use the same file for es_ES for es, or en_UK for en.

locales({
  localeAlias: {
    es: es_ES,
    en: en_UK,
  },
});

context.__(key[, value1[, value2, ...]])

Get current request locale text.

async function home(ctx) {
  ctx.body = {
    message: ctx.__('Hello, %s', 'fengmk2'),
  };
}

Examples:

__('Hello, %s. %s', 'fengmk2', 'koa rock!')
=>
'Hello fengmk2. koa rock!'

__('{0} {0} {1} {1} {1}', ['foo', 'bar'])
=>
'foo foo bar bar bar'

__('{a} {a} {b} {b} {b}', {a: 'foo', b: 'bar'})
=>
'foo foo bar bar bar'

context.__getLocale()

Get locale from query / cookie and header.

context.__setLocale()

Set locale and cookie.

context.__getLocaleOrigin()

Where does locale come from, could be query, cookie, header and default.

app.__(locale, key[, value1[, value2, ...]])

Get the given locale text on application level.

console.log(app.__('zh', 'Hello'));
// stdout '你好' for Chinese

Usage on template

this.state.__ = this.__.bind(this);

[Nunjucks] example:

{{ __('Hello, %s', user.name) }}

[Pug] example:

p= __('Hello, %s', user.name)

[Koa-pug] integration:

You can set the property locals on the KoaPug instance, where the default locals are stored.

app.use(async (ctx, next) => {
  koaPug.locals.__ = ctx.__.bind(ctx);
  await next();
});

Debugging

If you are interested on knowing what locale was chosen and why you can enable the debug messages from [debug].

There is two level of verbosity:

$ DEBUG=koa-locales node .

With this line it only will show one line per request, with the chosen language and the origin where the locale come from (queryString, header or cookie).

$ DEBUG=koa-locales:silly node .

Locale File Formats

You can provide locale files in the following formats:

  • .json (recommended for most use cases)
  • .yml or .yaml
  • .properties
  • .cjs (CommonJS JavaScript, e.g. module.exports = { ... })

Note: .js ESM modules are not supported for synchronous loading. Use .cjs for JavaScript locale files if you need sync loading.