Gathering detailed insights and metrics for koa-locales
Gathering detailed insights and metrics for koa-locales
npm install koa-locales
Typescript
Module System
Min. Node Version
Node Version
NPM Version
70
Supply Chain
99.3
Quality
79.6
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
9,531,219
Last Day
3,978
Last Week
25,818
Last Month
100,252
Last Year
1,280,630
NOASSERTION License
69 Stars
70 Commits
30 Forks
13 Watchers
6 Branches
21 Contributors
Updated on Jan 11, 2025
Minified
Minified + Gzipped
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
Cumulative downloads
Total Downloads
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
4
koa locales, i18n solution for koa:
options.dirs
.*.js
, *.json
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
security policy file detected
Details
Reason
license file detected
Details
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
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
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