Skip to content

Commit

Permalink
docs(i18n): use async (#1720)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and popomore committed Nov 29, 2017
1 parent 6741999 commit 80ab243
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
43 changes: 23 additions & 20 deletions docs/source/en/core/i18n.md
Original file line number Diff line number Diff line change
@@ -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:

Expand All @@ -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
Expand All @@ -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: '邮箱',
};
Expand All @@ -63,15 +63,15 @@ module.exports = {
Or using JSON file:

```json
// config/locales/zh-CN.json
// config/locale/zh-CN.json
{
"Email": "邮箱"
}
```

## 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__

Expand All @@ -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!',
};
Expand All @@ -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}。',
};
Expand All @@ -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
Expand Down
39 changes: 21 additions & 18 deletions docs/source/zh-cn/core/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: '邮箱',
};
Expand All @@ -63,17 +63,17 @@ module.exports = {
或者也可以用 JSON 格式的文件:

```json
// config/locales/zh-CN.json
// config/locale/zh-CN.json
{
"Email": "邮箱"
}
```

## 获取多语言文本

我们可以使用 `__` (Alias: `gettext`) 函数获取 locales 文件夹下面的多语言文本。
我们可以使用 `__` (Alias: `gettext`) 函数获取 locale 文件夹下面的多语言文本。

__注意: `__` 是两个下划线__
**注意: `__` 是两个下划线**

以上面配置过的多语言为例:

Expand All @@ -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!',
};
Expand All @@ -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}。',
};
Expand All @@ -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 中使用
Expand Down

0 comments on commit 80ab243

Please sign in to comment.