'use strict'; const locales = require('koa-locales'); const fs = require('fs'); const path = require('path'); const debug = require('debug')('egg:plugin:i18n'); /** * I18n 国际化 * * 通过设置 Plugin 配置 `i18n: true`,开启多语言支持。 * * #### 语言文件存储路径 * * 统一存放在 `config/locale/*.js` 下( 兼容`config/locales/*.js` ),如包含英文,简体中文,繁体中文的语言文件: * * ``` * - config/locale/ * - en-US.js * - zh-CN.js * - zh-TW.js * ``` * @class I18n * @param {App} app Application object. * @example * * #### I18n 文件内容 * * ```js * // config/locale/zh-CN.js * module.exports = { * "Email": "邮箱", * "Welcome back, %s!": "欢迎回来, %s!", * "Hello %s, how are you today?": "你好 %s, 今天过得咋样?", * }; * ``` * * ```js * // config/locale/en-US.js * module.exports = { * "Email": "Email", * }; * ``` * 或者也可以用 JSON 格式的文件: * * ```js * // config/locale/zh-CN.json * { * "email": "邮箱", * "login": "帐号", * "createdAt": "注册时间" * } * ``` */ module.exports = app => { /** * 如果开启了 I18n 多语言功能,那么会出现此 API,通过它可以获取到当前请求对应的本地化数据。 * * 详细使用说明,请查看 {@link I18n} * - `ctx.__ = function (key, value[, value2, ...])`: 类似 `util.format` 接口 * - `ctx.__ = function (key, values)`: 支持数组下标占位符方式,如 * - `__` 的别名是 `gettext(key, value)` * * > NOTE: __ 是两个下划线哦! * @method Context#__ * @example * ```js * ctx.__('{0} {0} {1} {1}'), ['foo', 'bar']) * ctx.gettext('{0} {0} {1} {1}'), ['foo', 'bar']) * => * foo foo bar bar * ``` * ##### Controller 下的使用示例 * * ```js * 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 文件下的使用示例 * * ```html *