Installations
npm install koa-locales
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=4.0.0
Node Version
10.15.1
NPM Version
5.8.0
Score
70
Supply Chain
99.3
Quality
79.6
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
koajs
Download Statistics
Total Downloads
9,531,219
Last Day
3,978
Last Week
25,818
Last Month
100,252
Last Year
1,280,630
GitHub Statistics
NOASSERTION License
69 Stars
70 Commits
30 Forks
13 Watchers
6 Branches
21 Contributors
Updated on Jan 11, 2025
Bundle Size
11.16 kB
Minified
4.58 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.12.0
Package Id
koa-locales@1.12.0
Size
6.43 kB
NPM Version
5.8.0
Node Version
10.15.1
Published on
Jun 16, 2019
Total Downloads
Cumulative downloads
Total Downloads
9,531,219
Last Day
5.5%
3,978
Compared to previous day
Last Week
14.6%
25,818
Compared to previous week
Last Month
5.5%
100,252
Compared to previous month
Last Year
-41.6%
1,280,630
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
4
koa-locales
koa locales, i18n solution for koa:
- All locales resources location on
options.dirs
. - resources file supports:
*.js
,*.json
and*.properties
, see examples. - One api:
__(key[, value, ...])
. - Auto detect request locale from
query
,cookie
andheader: Accept-Language
.
Installation
1$ npm install koa-locales --save
Quick start
1const koa = require('koa'); 2const locales = require('koa-locales'); 3 4const app = koa(); 5const options = { 6 dirs: [__dirname + '/locales', __dirname + '/foo/locales'], 7}; 8locales(app, options);
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
{}
. - {String|Number} cookieMaxAge: set locale cookie value max age. Optional, default is
1y
, expired after one year.
- {String} functionName: locale function name patch on koa context. Optional, default is
1locales({ 2 app: app, 3 dirs: [__dirname + '/app/locales'], 4 defaultLocale: 'zh-CN', 5});
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.
1locales({ 2 localeAlias: { 3 es: es_ES, 4 en: en_UK, 5 }, 6});
context.__(key[, value1[, value2, ...]])
Get current request locale text.
1async function home(ctx) { 2 ctx.body = { 3 message: ctx.__('Hello, %s', 'fengmk2'), 4 }; 5}
Examples:
1__('Hello, %s. %s', 'fengmk2', 'koa rock!') 2=> 3'Hello fengmk2. koa rock!' 4 5__('{0} {0} {1} {1} {1}', ['foo', 'bar']) 6=> 7'foo foo bar bar bar' 8 9__('{a} {a} {b} {b} {b}', {a: 'foo', b: 'bar'}) 10=> 11'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.
1console.log(app.__('zh', 'Hello')); 2// stdout 'ä½ å¥½' for Chinese
Usage on template
1this.state.__ = this.__.bind(this);
Nunjucks example:
1{{ __('Hello, %s', user.name) }}
Pug example:
1p= __('Hello, %s', user.name)
Koa-pug integration:
You can set the property locals on the KoaPug instance, where the default locals are stored.
1app.use(async (ctx, next) => { 2 koaPug.locals.__ = ctx.__.bind(ctx); 3 await next(); 4});
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:
1$ 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).
1$ DEBUG=koa-locales:silly node .
Use this level if something doesn't work as you expect. This is going to debug everything, including each translated line of text.
License

No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/koajs/.github/SECURITY.md:1
- Info: Found linked content: github.com/koajs/.github/SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/koajs/.github/SECURITY.md:1
- Info: Found text in security policy: github.com/koajs/.github/SECURITY.md:1
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Warn: project license file does not contain an FSF or OSI license.
Reason
Found 10/28 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 14 are checked with a SAST tool
Score
4.9
/10
Last Scanned on 2025-02-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More