npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@webaifei/egg-i18n

v2.1.14

Published

i18n plugin for egg

Downloads

3

Readme

egg-i18n

NPM version build status Test coverage David deps Known Vulnerabilities npm download

可以为你的应用提供多语言的特性

功能

  • 支持动态更新多语言 app.updatei18nResources
  • 支持多种语言独立配置,统一存放在 config/locale/*.js 下( 兼容 config/locales/*.js );
  • 提供 Middleware 为 View 提供 \_\_, gettext 函数获取多语言文案;
  • 基于 URL 参数 locale 修改语言显示,同时会记录到 Cookie,下次请求会用 Cookie 里面的语言方案。

配置

默认处于关闭状态,你需要在 config/plugin.js 开启它:

// config/plugin.js
exports.i18n = {
  enable: true,
  package: 'egg-i18n',
};

你可以修改 config/config.default.js 来设定 i18n 的配置项:

// config/config.default.js
exports.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 设定默认的语言。

编写你的 I18n 多语言文件

// config/locale/zh-CN.js
module.exports = {
  "Email": "邮箱",
  "Welcome back, %s!": "欢迎回来,%s!",
  "Hello %s, how are you today?": "你好 %s, 今天过得咋样?",
};
// config/locale/en-US.js
module.exports = {
  "Email": "Email",
};

或者也可以用 JSON 格式的文件:

// config/locale/zh-CN.json
{
  "email": "邮箱",
  "login": "帐号",
  "createdAt": "注册时间"
}

使用 I18n 函数获取语言文本

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

Controllers 下的使用示例

module.exports = function* () {
  this.body = {
    message: this.__('Welcome back, %s!', this.user.name)
    // 或者使用 gettext,如果觉得 __ 不好看的话
    // message: this.gettext('Welcome back, %s!', this.user.name)
    user: this.user,
  };
};

View 文件下的使用示例

<li>{{ __('Email') }}: {{ user.email }}</li>
<li>
  {{ __('Hello %s, how are you today?', user.name) }}
</li>
<li>
  {{ __('{0} {0} {1} {1}'), ['foo', 'bar']) }}
</li>

修改应用的默认语言

你可以用下面几种方式修改应用的当前语言(修改或会记录到 Cookie),下次请求直接用设定好的语言。

优先级从上到下:

  • path: /en
  • query: /?locale=en-US
  • cookie: locale=zh-TW
  • header: Accept-Language: zh-CN,zh;q=0.5