Update i18n.md

This commit is contained in:
Zhou 2022-11-06 14:15:00 +08:00 committed by GitHub
parent 6f2a993299
commit bf9a2e2647
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
export class MyPlugin extends Plugin {
@ -19,19 +19,19 @@ export class MyPlugin extends Plugin {
}
```
## 两个 i18n 实例
## Two i18n instances
### app.i18n
全局 i18n 实例,一般用于 CLI 中国。
Global i18n instance, typically used in the CLI.
```ts
app.i18n.t('World') // “世界”或“World”
app.i18n.t('World') // "世界" or "World"
```
### 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
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
GET /?locale=en-US HTTP/1.1
Host: localhost:13000
```
或放在 request headers 里
or in the request headers
```bash
GET / HTTP/1.1
@ -55,9 +55,9 @@ Host: localhost:13000
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
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
|- /my-plugin
|- /src
|- /server
|- locale # 多语言文件夹
|- zh-CN.ts
|- locale # Multi-language folder
|- en-cn.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
const namespace = 'shop';
@ -101,7 +101,7 @@ export default class ShopPlugin extends Plugin {
const productRepo = ctx.db.getRepository('products');
const product = await productRepo.findOne({
filterByTk: ctx.action.params.values.productId
});
productId });
if (!product) {
return ctx.throw(404, ctx.t('No such product'));