Update i18n.md
This commit is contained in:
parent
6f2a993299
commit
bf9a2e2647
@ -1,8 +1,8 @@
|
|||||||
# 国际化
|
# Internationalization
|
||||||
|
|
||||||
NocoBase 国际化基于 [i18next](https://npmjs.com/package/i18next) 实现。
|
Internationalization in NocoBase is implemented based on [i18next](https://npmjs.com/package/i18next).
|
||||||
|
|
||||||
## 如何注册多语言包?
|
## How to register a multilingual package?
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
export class MyPlugin extends Plugin {
|
export class MyPlugin extends Plugin {
|
||||||
@ -19,19 +19,19 @@ export class MyPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## 两个 i18n 实例
|
## Two i18n instances
|
||||||
|
|
||||||
### app.i18n
|
### app.i18n
|
||||||
|
|
||||||
全局 i18n 实例,一般用于 CLI 中国。
|
Global i18n instance, typically used in the CLI.
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
app.i18n.t('World') // “世界”或“World”
|
app.i18n.t('World') // "世界" or "World"
|
||||||
```
|
```
|
||||||
|
|
||||||
### ctx.i18n
|
### ctx.i18n
|
||||||
|
|
||||||
全局 i18n 的 cloneInstance,每个请求的 context 完全独立,通常用于根据客户端语言响应多语言信息。
|
CloneInstance of global i18n with a completely independent context for each request, typically used to respond to multilingual messages based on the client language.
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
app.use(async (ctx, next) => {
|
app.use(async (ctx, next) => {
|
||||||
@ -40,14 +40,14 @@ app.use(async (ctx, next) => {
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
客户端请求参数可以放在 query string 里
|
The client request parameters can be placed in the query string
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
GET /?locale=en-US HTTP/1.1
|
GET /?locale=en-US HTTP/1.1
|
||||||
Host: localhost:13000
|
Host: localhost:13000
|
||||||
```
|
```
|
||||||
|
|
||||||
或放在 request headers 里
|
or in the request headers
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
GET / HTTP/1.1
|
GET / HTTP/1.1
|
||||||
@ -55,9 +55,9 @@ Host: localhost:13000
|
|||||||
X-Locale: en-US
|
X-Locale: en-US
|
||||||
```
|
```
|
||||||
|
|
||||||
## 建议配置
|
## Suggested configuration
|
||||||
|
|
||||||
以英文文案为 key,翻译为 value,这样的好处,即使多语言缺失,也会以英文显示,不会造成阅读障碍,如:
|
With English text as the key and translation as the value, this has the advantage that even if multiple languages are missing, it will be displayed in English and will not cause reading barriers, e.g.
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
i18n.addResources('zh-CN', 'your-namespace', {
|
i18n.addResources('zh-CN', 'your-namespace', {
|
||||||
@ -66,22 +66,22 @@ i18n.addResources('zh-CN', 'your-namespace', {
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
为了更方便管理多语言文件,推荐在插件中创建一个 `locale` 文件夹,并把对应语言文件都放置在其中方便管理:
|
To make it easier to manage multilingual files, it is recommended to create a `locale` folder in the plugin and place all the corresponding language files in it:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|- /my-plugin
|
|- /my-plugin
|
||||||
|- /src
|
|- /src
|
||||||
|- /server
|
|- /server
|
||||||
|- locale # 多语言文件夹
|
|- locale # Multi-language folder
|
||||||
|- zh-CN.ts
|
|- en-cn.ts
|
||||||
|- en-US.ts
|
|- en-US.ts
|
||||||
```
|
```
|
||||||
|
|
||||||
## 示例
|
## Example
|
||||||
|
|
||||||
### 服务端错误提示
|
### Server-side error alert
|
||||||
|
|
||||||
例如用户在店铺对某个商品下单时,如果商品的库存不够,或者未上架,那么下单接口被调用时,应该返回相应的错误。
|
For example, when a user places an order for a product in the store, if the product is not in stock, or not on the shelf, then the order interface should return the appropriate error when it is called.
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
const namespace = 'shop';
|
const namespace = 'shop';
|
||||||
@ -101,7 +101,7 @@ export default class ShopPlugin extends Plugin {
|
|||||||
const productRepo = ctx.db.getRepository('products');
|
const productRepo = ctx.db.getRepository('products');
|
||||||
const product = await productRepo.findOne({
|
const product = await productRepo.findOne({
|
||||||
filterByTk: ctx.action.params.values.productId
|
filterByTk: ctx.action.params.values.productId
|
||||||
});
|
productId });
|
||||||
|
|
||||||
if (!product) {
|
if (!product) {
|
||||||
return ctx.throw(404, ctx.t('No such product'));
|
return ctx.throw(404, ctx.t('No such product'));
|
||||||
|
Loading…
Reference in New Issue
Block a user