Gathering detailed insights and metrics for koa-adam-locale
Gathering detailed insights and metrics for koa-adam-locale
Gathering detailed insights and metrics for koa-adam-locale
Gathering detailed insights and metrics for koa-adam-locale
npm install koa-adam-locale
Typescript
Module System
Node Version
NPM Version
JavaScript (95.45%)
HTML (4.55%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
50 Commits
2 Forks
2 Watchers
1 Branches
2 Contributors
Updated on Sep 13, 2019
Latest Version
2.4.0
Package Id
koa-adam-locale@2.4.0
Size
3.99 kB
NPM Version
3.10.8
Node Version
7.0.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
2
Simple Multi Language Middleware for Koa.
1$ npm install koa-adam-locale
app.js:
1var Path = require('path'); 2var View = require('koa-views') 3var KAL = require('koa-adam-locale'); 4var koa = require('koa'); 5var app = koa(); 6 7/* i18n */ 8app.use(KAL({ 9 path: Path.resolve(__dirname, 'i18n'), 10 // reference: require('./i18n_ref'), 11 cookieKey: 'current_lang', 12 cookies: { 13 maxAge: 365*24*60*60*1000, 14 domain: 'hello.com', 15 signed: false 16 }, 17 default: 'en_US', 18 supported: [{code:'en_US', lang:'English'},{code:'zh_CN', lang:'简体中文'}] 19}, app)); 20 21/* templating */ 22app.use(View(__dirname + '/views', { 23 default: 'ejs' 24})); 25 26app.use(function * () { 27 if(!this.path == '/') return; 28 yield this.render('locale') 29}) 30 31app.listen(process.argv[2])
i18n/en_US.json
1{ 2 "hello": "hello", 3 "world": "world" 4}
views/locale.ejs
1<p><%= _i18n_.hello %>, <%= _i18n_.world %>.</p> 2<p>current:<%= _i18n_current_ %>.</p> 3<% for(var i in _i18n_supported_) { %> 4<a href='/set_locale?lang=<%= _i18n_supported_[i].code %>'> 5 switch to <%= _i18n_supported_[i].lang %> 6</a> 7<% } %>
The folder path of i18n files
The reference of i18n Object, key of this reference should match the code, like 'zh_CN', 'en_US' (Case sensitive),
and this parameter has a higher priority than path
.
The supported languages. default value: [{code:'en_US', lang:'English'},{code:'zh_CN', lang:'简体中文'}]
1app.use(KAL({ 2 path: Path.resolve(__dirname, 'i18n'), 3 // reference: require('./i18n_ref'), 4 supported: [{code:'en_US', lang:'English'},{code:'zh_CN', lang:'简体中文'}] 5}, app));
Custom template data params namespace. Default value is 'locals'
1// for koa-views 3.x 2app.use(KAL({ 3 path: Path.resolve(__dirname, 'i18n'), 4 // reference: require('./i18n_ref'), 5 supported: [{code:'en_US', lang:'English'},{code:'zh_CN', lang:'简体中文'}], 6 namespace: 'state' 7}, app));
Store the selected language in the cookie. see koa cookie for more information.
1app.use(KAL({ 2 path: Path.resolve(__dirname, 'i18n'), 3 cookies: { 4 domain: 'hello.com', 5 maxAge: 365*24*60*60*1000 6 } 7}, app));
Default language
1app.use(KAL({ 2 path: Path.resolve(__dirname, 'i18n'), 3 // reference: require('./i18n_ref'), 4 supported: [{code:'en_US', lang:'English'},{code:'zh_CN', lang:'简体中文'}], 5 default: 'en_US' 6}, app)); 7
The url to switch current language. default value: /set_locale
1app.use(KAL({ 2 path: Path.resolve(__dirname, 'i18n'), 3 // reference: require('./i18n_ref'), 4 set_url: '/set_locale' 5}, app)); 6
Then you can set current language by request url like this '/set_locale?lang=en_US'
The following three params are set to ctx.namespace, default ctx.locals
. For koa-views 3.x version, you can set namespace to ctx.state
this.locals._i18n_
{Object}this.locals._i18n_current_
{String}this.locals._i18n_supported_
{Array}添加dust模板的helper,用于展示语言名称
var dust = require('dustjs-helpers');
KAL.addLanguageHelper(dust)
在模板里使用@language
{@language code="ru_RU" current="zh_CN" /} // 输出"俄语"
{@language code="ru_RU" current="en_US" /} // 输出"Russian"
{@language code="ru_RU" /} // 根据系统设置的自动判断语言
Set the DEBUG
environment variable to koa-adam-locale
when starting your server.
1$ DEBUG=koa-adam-locale
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
Found 1/28 approved changesets -- score normalized to 0
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
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
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