
Security News
Deno Goes the Way of the Dinosaur
Cloudflare is acquiring Deno’s team, which will provide another year of bug fixes and security updates before ending support for the runtime.
@eggjs/i18n
Advanced tools
可以为你的应用提供多语言的特性
config/locales/*.js );\_\_, gettext 函数获取多语言文案;locale 修改语言显示,同时会记录到 Cookie,下次请求会用 Cookie 里面的语言方案。egg 内置插件 i18n 默认开启。
你可以修改 config/config.default.ts 来设定 i18n 的配置项:
// config/config.default.ts
export default {
i18n: {
// 默认语言,默认 "en_US"
defaultLocale: 'zh_CN',
// URL 参数,默认 "locale"
queryField: 'locale',
// Cookie 记录的 key, 默认:"locale"
cookieField: 'locale',
// Cookie 的 domain 配置,默认为空,代表当前域名有效
cookieDomain: '',
// Cookie 默认 `1y` 一年后过期, 如果设置为 Number,则单位为 ms
cookieMaxAge: '1y',
},
};
其实大部分时候,你只需要修改一下 defaultLocale 设定默认的语言。
// config/locale/zh-CN.ts
export default {
Email: '邮箱',
'Welcome back, %s!': '欢迎回来,%s!',
'Hello %s, how are you today?': '你好 %s, 今天过得咋样?',
};
// config/locale/en-US.ts
export default {
Email: 'Email',
};
或者也可以用 JSON 格式的文件:
// config/locale/zh-CN.json
{
"email": "邮箱",
"login": "帐号",
"createdAt": "注册时间"
}
I18n 为你提供 __ (Alias: gettext) 函数,让你可以轻松获得 locale 文件夹下面的多语言文本。
NOTE: __ 是两个下划线哦!
ctx.__ = function (key, value[, value2, ...]): 类似 util.format 接口ctx.__ = function (key, values): 支持数组下标占位符方式,如ctx.__('{0} {0} {1} {1}', ['foo', 'bar']);
ctx.gettext('{0} {0} {1} {1}', ['foo', 'bar']);
=>
foo foo bar bar
export default ctx => {
ctx.body = {
message: ctx.__('Welcome back, %s!', ctx.user.name)
// 或者使用 gettext,如果觉得 __ 不好看的话
// message: this.gettext('Welcome back, %s!', ctx.user.name)
user: ctx.user,
};
};
import { HTTPController, HTTPMethod, HTTPMethodEnum, HTTPContext, Context } from 'egg';
@HTTPController()
export default class I18nController {
@HTTPMethod({ method: HTTPMethodEnum.GET, path: '/i18n' })
async getI18n(@HTTPContext() ctx: Context) {
return { message: ctx.__('Welcome back, %s!', ctx.user.name) };
}
}
<li>{{ __('Email') }}: {{ user.email }}</li>
<li>{{ __('Hello %s, how are you today?', user.name) }}</li>
<li>{{ __('{0} {0} {1} {1}', ['foo', 'bar']) }}</li>
你可以用下面几种方式修改应用的当前语言(修改或会记录到 Cookie),下次请求直接用设定好的语言。
优先级从上到下:
/?locale=en-USlocale=zh-TWAccept-Language: zh-CN,zh;q=0.5Please open an issue here.
Made with contributors-img.
FAQs
i18n plugin for egg
The npm package @eggjs/i18n receives a total of 426 weekly downloads. As such, @eggjs/i18n popularity was classified as not popular.
We found that @eggjs/i18n demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 10 open source maintainers collaborating on the project.

Security News
Cloudflare is acquiring Deno’s team, which will provide another year of bug fixes and security updates before ending support for the runtime.

Security News
Anthropic’s free OSS Scanner uses Mythos and other Claude models to find open source vulnerabilities and send maintainers reports with proposed fixes.

Security News
On Front Lines’ Early Adopters: CISO podcast, Andrew Becherer shares lessons from Datadog, weighs the long-term costs of AI-built tools, and explains how founders can actually gain a CISO’s trust.