I18n
Midway provides a multi-language component that allows the business to quickly specify different languages and display different texts. It can also be used in HTTP scenarios with request parameters and request first-class methods.
Related information:
| Description | |
|---|---|
| Can be used for standard projects | ✅ |
| Can be used for Serverless | ✅ |
| Can be used for integration | ✅ |
| Contains independent main framework | ❌ |
| Contains independent logs | ❌ |
Installation Components
$ npm i @midwayjs/i18n@3 --save
Or reinstall the following dependencies in package.json.
{
"dependencies": {
"@midwayjs/i18n": "^3.0.0",
// ...
},
}
Use components
Configure the i18n component into the code.
import { Configuration } from '@midwayjs/core';
import * as i18n from '@midwayjs/i18n';
@Configuration({
imports: [
// ...
i18n
]
})
export class MainConfiguration {
//...
}
Use
The component provides MidwayI18nService services for translating multilingual text.
Using translate method, pass in different text keywords and parameters to return text content in different languages.
@Controller('/')
export class UserController {
@Inject()
i18nService: MidwayI18nService;
@Get('/')
async index(@Query('username') username: string) {
return this.i18nService.translate('HELLO_MESSAGE', {
args: {
username
},
});
}
}
Configure i18n messages
You can configure it directly in the configuration file, but in most cases, there will be a lot of copy, and sometimes it may even be on remote services. Direct configuration is not realistic at this time.
Generally speaking, we will put the copy into a copy configuration directory, such as src/locales.
Take the src/locale directory as an example, let's take an example, the structure is as follows:
.
├── src
│ ├── locales
| │ ├── en_US.json
| │ └── zh_CN.json
│ └── controller
│ └── home.controller.ts
├── package.json
└── tsconfig.json
Here we have created two multilingual files, en_US.json and zh_CN.json, representing English and Chinese respectively.
The contents of the documents are as follows:
// src/locales/en_US.json
{
"hello": "Hello {username} ",
"email": "email id ",
"login": "login account ",
"createdAt": "register date"
}
// src/locales/zh_CN.json
{
"hello": "你好 {username}",
"email": "邮箱",
"login": "帐号",
"createdAt": "注册时间"
}
Each line has a string pair, which is a standard JSON format. You can also use js/ts files. The curly brackets are filled with replaceable parameters.
At the same time, you need to add these two JSON to the configuration, and default property is a default group name.
// src/config/config.default.ts
export default {
// ...
i18n: {
// Put your translated text here
localeTable: {
en_US: {
default: require('../locale/en_US'),
},
zh_CN: {
default: require('../locale/zh_CN'),
}
},
}
}
In this way, it can be used. The output is as follows.
this.i18nService.translate('hello', {
args: {
username: 'harry',
},
locale: 'en_US',
});
// output: Hello harry.
this.i18nService.translate('hello', {
args: {
username: 'harry',
},
locale: 'zh_CN',
});
// output: 你好 harry.
I18n message group
In the following configuration, the multi-language message configured by the user is in the default group.
// src/config/config.default.ts
export default {
// ...
i18n: {
// Put your translated text here
localeTable: {
en_US: {
default: require('../locale/en_US'),
},
zh_CN: {
default: require('../locale/zh_CN'),
}
},
}
}
The advantage of this is that in other components or business codes, we can also use different group names to add other multilingual texts.
For example:
// src/config/config.default.ts
export default {
// ...
i18n: {
// Put your translated text here
localeTable: {
en_US: {
default: require('../locale/en_US'),
user: require('../locale/user_en_US'),
},
zh_CN: {
default: require('../locale/zh_CN'),
user: require('../locale/user_zh_CN'),
}
},
}
}
In the code, if you call a different group, you need to specify the group parameters.
this.i18nService.translate('user.hello', {
args: {
username: 'harry',
},
group: 'user', // Specify other groups
locale: 'en_US',
});
I18n message format
Parameters can be added to multilingual text, and parameters can have two forms: object and array.
The object form is as follows, using curly braces as placeholders.
Hello {username}
When used, passed by configuration, overriding variables by object key.
async index(@Query('username') username: string) {
return this.i18nService.translate('hello', {
args: {
username
},
});
}
The array form is as follows, using numbers as placeholders.
Hello {0}
When used, it is passed through configuration, and the format is in the form of an array, overwriting the numeric variables in the order of the array.
async index(@Query('username') username: string) {
return this.i18nService.translate('hello', {
args: [username]
});
}
Dynamically add i18n message
Sometimes, i18n message may be placed remotely, such as databases, etc., and we can add them dynamically through addLocale methods.
For example, after the configuration is loaded, before the code is used.
// configuration.ts
// ...
@Configuration({
imports: [
koa
i18n
]
})
export class MainConfiguration {
@Inject()
i18nService: MidwayI18nService;
async onReady() {
this.i18nService.addLocale('zh_TW', {
hello: '你好,{username} 美麗的世界'
});
}
// ...
}
It can be used in code.
async index(@Query('username') username: string) {
return this.i18nService.translate('hello', {
args: [username]
locale: 'zh_TW'
});
}
Specify the current language through parameters
In general, the default language is en_US, and the user's browser will usually have an Accept-Language header, so the language will be correctly identified. For example, if you use a Chinese browser to access, Chinese can be displayed normally.
In addition, in HTTP scenarios, you can specify the language through URL Query, Cookie, and Header.
By default, you can specify URL Query,Cookie, and Header.
Priority from top to bottom:
- query: /?locale=en-US
- cookie: locale=zh-TW
- header: Accept-Language: zh-CN,zh;q=0.5
After these parameters are passed, the multilingual data will be automatically saved to the current user's Cookie, and the next request will directly use the set language.
Set language manually
The current language can be set by calling the saveRequestLocale.
async index() {
// ...
this.i18nService.saveRequestLocale('zh_CN');
}
If the writeCookie configuration is turned on, the settings will be saved to the current user's Cookie and will be used in the next request.
Language selection priority
These multiple ways of setting up languages have different priorities, from high to low:
-
- The language explicitly specified by the
i18nService.translatemethod
- The language explicitly specified by the
-
- Languages set by other decorators, such as
@Validatethe parameters of the decorator (essentially calling thei18nService.translatemethod)
- Languages set by other decorators, such as
-
- The current language directly set through the
saveRequestLocaleAPI
- The current language directly set through the
-
- Language set by browser Query,Cookie and Header (essentially,
saveRequestLocaleis called)
- Language set by browser Query,Cookie and Header (essentially,
-
- default language in i18n component configuration