
Security News
Risky Biz Podcast: Making Reachability Analysis Work in Real-World Codebases
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
koa-adam-locale
Advanced tools
Simple Multi Language Middleware for Koa.
$ npm install koa-adam-locale
app.js:
var Path = require('path');
var View = require('koa-views')
var KAL = require('koa-adam-locale');
var koa = require('koa');
var app = koa();
/* i18n */
app.use(KAL({
path: Path.resolve(__dirname, 'i18n'),
// reference: require('./i18n_ref'),
cookieKey: 'current_lang',
cookies: {
maxAge: 365*24*60*60*1000,
domain: 'hello.com',
signed: false
},
default: 'en_US',
supported: [{code:'en_US', lang:'English'},{code:'zh_CN', lang:'简体中文'}]
}, app));
/* templating */
app.use(View(__dirname + '/views', {
default: 'ejs'
}));
app.use(function * () {
if(!this.path == '/') return;
yield this.render('locale')
})
app.listen(process.argv[2])
i18n/en_US.json
{
"hello": "hello",
"world": "world"
}
views/locale.ejs
<p><%= _i18n_.hello %>, <%= _i18n_.world %>.</p>
<p>current:<%= _i18n_current_ %>.</p>
<% for(var i in _i18n_supported_) { %>
<a href='/set_locale?lang=<%= _i18n_supported_[i].code %>'>
switch to <%= _i18n_supported_[i].lang %>
</a>
<% } %>
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:'简体中文'}]
app.use(KAL({
path: Path.resolve(__dirname, 'i18n'),
// reference: require('./i18n_ref'),
supported: [{code:'en_US', lang:'English'},{code:'zh_CN', lang:'简体中文'}]
}, app));
Custom template data params namespace. Default value is 'locals'
// for koa-views 3.x
app.use(KAL({
path: Path.resolve(__dirname, 'i18n'),
// reference: require('./i18n_ref'),
supported: [{code:'en_US', lang:'English'},{code:'zh_CN', lang:'简体中文'}],
namespace: 'state'
}, app));
Store the selected language in the cookie. see koa cookie for more information.
app.use(KAL({
path: Path.resolve(__dirname, 'i18n'),
cookies: {
domain: 'hello.com',
maxAge: 365*24*60*60*1000
}
}, app));
Default language
app.use(KAL({
path: Path.resolve(__dirname, 'i18n'),
// reference: require('./i18n_ref'),
supported: [{code:'en_US', lang:'English'},{code:'zh_CN', lang:'简体中文'}],
default: 'en_US'
}, app));
The url to switch current language. default value: /set_locale
app.use(KAL({
path: Path.resolve(__dirname, 'i18n'),
// reference: require('./i18n_ref'),
set_url: '/set_locale'
}, app));
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.
$ DEBUG=koa-adam-locale
MIT
FAQs
Simple Multi Language Middleware for Koa.
We found that koa-adam-locale demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.