Gathering detailed insights and metrics for @elementium/koa-locales
Gathering detailed insights and metrics for @elementium/koa-locales
Gathering detailed insights and metrics for @elementium/koa-locales
Gathering detailed insights and metrics for @elementium/koa-locales
npm install @elementium/koa-locales
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
NOASSERTION License
71 Commits
1 Branches
1 Contributors
Updated on Jul 01, 2020
Latest Version
1.13.0
Package Id
@elementium/koa-locales@1.13.0
Unpacked Size
18.04 kB
Size
6.47 kB
File Count
5
NPM Version
6.14.4
Node Version
10.21.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
koa locales, i18n solution for koa:
options.dirs
.*.js
, *.json
, *.yml
, *.yaml
and *.properties
, see examples.__(key[, value, ...])
.query
, cookie
and header: Accept-Language
.1$ npm install koa-locales --save
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);
locales(app, options)
Patch locales functions to koa app.
__
.['$PWD/locales']
.en-US
.locale
.locale
.''
.{}
.1y
, expired after one year.1locales({ 2 app: app, 3 dirs: [__dirname + '/app/locales'], 4 defaultLocale: 'zh-CN', 5});
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
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});
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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
no SAST tool detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2025-06-30
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