From 0c15fea7a52c95621d5d54d5c17189f60b963a3b Mon Sep 17 00:00:00 2001 From: TZ Date: Fri, 24 Nov 2017 15:52:54 +0800 Subject: [PATCH] docs(i18n): use async --- docs/source/en/core/i18n.md | 43 ++++++++++++++++++---------------- docs/source/zh-cn/core/i18n.md | 39 ++++++++++++++++-------------- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/docs/source/en/core/i18n.md b/docs/source/en/core/i18n.md index 05218a64f5..980c85060c 100644 --- a/docs/source/en/core/i18n.md +++ b/docs/source/en/core/i18n.md @@ -1,9 +1,9 @@ title: I18n Internationalization --- -For developing the multi-language application, build-in I18n support by [egg-i18n](https://github.com/eggjs/egg-i18n) plugin +For developing the multi-language application, build-in I18n support by [egg-i18n](https://github.com/eggjs/egg-i18n) plugin -## Default Language +## Default Language Default `en-US`. Assume that we want to switch the default language to Simplified Chinese: @@ -24,7 +24,7 @@ Priority from high to low: 2. cookie: `locale=zh-TW` 3. header: `Accept-Language: zh-CN,zh;q=0.5` -If want to modify parameter name of query or cookie +If want to modify parameter name of query or cookie ```js // config/config.default.js @@ -38,23 +38,23 @@ exports.i18n = { ## Writing I18n Multi-language Documents -Configuration of multi-language are independent, stored in `config/locales/*.js` +Configuration of multi-language are independent, stored in `config/locale/*.js` ``` -- config/locales/ +- config/locale/ - en-US.js - zh-CN.js - zh-TW.js ``` -Not only take effects in the application directory, but also in the directory of framework or plugin `config/locales` +Not only take effects in the application directory, but also in the directory of framework or plugin `config/locale` - __Note: It's locales, not locals.__ + **Note: It's locale, not locals.** Example: ```js -// config/locales/zh-CN.js +// config/locale/zh-CN.js module.exports = { Email: '邮箱', }; @@ -63,7 +63,7 @@ module.exports = { Or using JSON file: ```json -// config/locales/zh-CN.json +// config/locale/zh-CN.json { "Email": "邮箱" } @@ -71,7 +71,7 @@ Or using JSON file: ## Getting Multi-language Texts -Use `__` (Alias: `gettext`) function to get the multi-language texts under locales directory +Use `__` (Alias: `gettext`) function to get the multi-language texts under locale directory __Note: `__` is two underscores__ @@ -86,7 +86,7 @@ ctx.__('Email') If texts contain format function like `%s`,`%j`, we can call by the way similar to [`util.format()`](https://nodejs.org/api/util.html#util_util_format_format_args) ```js -// config/locales/zh-CN.js +// config/locale/zh-CN.js module.exports = { 'Welcome back, %s!': '欢迎回来,%s!', }; @@ -99,7 +99,7 @@ ctx.__('Welcome back, %s!', 'Shawn'); Support array, subscript and placeholder, such as ```js -// config/locales/zh-CN.js +// config/locale/zh-CN.js module.exports = { 'Hello {0}! My name is {1}.': '你好 {0}! 我的名字叫 {1}。', }; @@ -112,14 +112,17 @@ ctx.__('Hello {0}! My name is {1}.', ['foo', 'bar']) ### Use in Controller ```js -module.exports = function* (ctx) { - ctx.body = { - message: ctx.__('Welcome back, %s!', ctx.user.name) - // or use gettext, it is a alias of __ function - // message: ctx.gettext('Welcome back', ctx.user.name) - user: ctx.user, - }; -}; +class HomeController extends Controller { + async index() { + const ctx = this.ctx; + ctx.body = { + message: ctx.__('Welcome back, %s!', ctx.user.name) + // or use gettext, it is a alias of __ function + // message: ctx.gettext('Welcome back', ctx.user.name) + user: ctx.user, + }; + } +} ``` ### Use in View diff --git a/docs/source/zh-cn/core/i18n.md b/docs/source/zh-cn/core/i18n.md index b62dd22bc6..49d0893fe5 100644 --- a/docs/source/zh-cn/core/i18n.md +++ b/docs/source/zh-cn/core/i18n.md @@ -38,23 +38,23 @@ exports.i18n = { ## 编写 I18n 多语言文件 -多种语言的配置是独立的,统一存放在 `config/locales/*.js` 下。 +多种语言的配置是独立的,统一存放在 `config/locale/*.js` 下。 ``` -- config/locales/ +- config/locale/ - en-US.js - zh-CN.js - zh-TW.js ``` -不仅对于应用目录生效,在框架,插件的 `config/locales` 目录下同样生效。 +不仅对于应用目录生效,在框架,插件的 `config/locale` 目录下同样生效。 -__注意单词拼写,是 locales 不是 locals。__ +**注意单词拼写,是 locale 不是 locals。** 例如: ```js -// config/locales/zh-CN.js +// config/locale/zh-CN.js module.exports = { Email: '邮箱', }; @@ -63,7 +63,7 @@ module.exports = { 或者也可以用 JSON 格式的文件: ```json -// config/locales/zh-CN.json +// config/locale/zh-CN.json { "Email": "邮箱" } @@ -71,9 +71,9 @@ module.exports = { ## 获取多语言文本 -我们可以使用 `__` (Alias: `gettext`) 函数获取 locales 文件夹下面的多语言文本。 +我们可以使用 `__` (Alias: `gettext`) 函数获取 locale 文件夹下面的多语言文本。 -__注意: `__` 是两个下划线__ +**注意: `__` 是两个下划线** 以上面配置过的多语言为例: @@ -86,7 +86,7 @@ ctx.__('Email') 如果文本中含有 `%s`,`%j` 等 format 函数,可以按照 [`util.format()`](https://nodejs.org/api/util.html#util_util_format_format_args) 类似的方式调用: ```js -// config/locales/zh-CN.js +// config/locale/zh-CN.js module.exports = { 'Welcome back, %s!': '欢迎回来,%s!', }; @@ -99,7 +99,7 @@ ctx.__('Welcome back, %s!', 'Shawn'); 同时支持数组下标占位符方式,例如: ```js -// config/locales/zh-CN.js +// config/locale/zh-CN.js module.exports = { 'Hello {0}! My name is {1}.': '你好 {0}! 我的名字叫 {1}。', }; @@ -112,14 +112,17 @@ ctx.__('Hello {0}! My name is {1}.', ['foo', 'bar']) ### Controller 中使用 ```js -module.exports = function* (ctx) { - ctx.body = { - message: ctx.__('Welcome back, %s!', ctx.user.name) - // 或者使用 gettext,gettext 是 __ 函数的 alias - // message: ctx.gettext('Welcome back', ctx.user.name) - user: ctx.user, - }; -}; +class HomeController extends Controller { + async index() { + const ctx = this.ctx; + ctx.body = { + message: ctx.__('Welcome back, %s!', ctx.user.name) + // 或者使用 gettext,gettext 是 __ 函数的 alias + // message: ctx.gettext('Welcome back', ctx.user.name) + user: ctx.user, + }; + } +} ``` ### View 中使用