EggJS provides the feature of i8n
at project level
. EggBornJS extends the feature provided by EggJS, realizing the i18n
at the module level
- Module can specify its own
i18n
- The
i18n
atproject level
can override the one atmodule level
Definition of Language Resources
The default language for CabloyJS is en-us
. If support Chinese
, you need to add zh-cn
resource file
src/suite-vendor/test-party/modules/test-party/backend/src/config/locale/zh-cn.js
- 1module.exports = {
- 2 'Hello World': '世界,您好',
- 3};
Usage of Language Resources
Get language resources of the current locale
EggBornJS uses the method ctx.text
to dynamically get the specified language resources according to the locale
configuration of the client
src/suite-vendor/test-party/modules/test-party/backend/src/controller/kitchen-sink/guide.js
- 1async echo3() {
- 2 const message = this.ctx.text('Hello World');
- 3 this.ctx.success(message);
- 4}
How to change the
locale
configuration of the client?
- Open the home page, entry
Mine
->Info
,update the field ofLocale
, and save, then refresh the page
Get language resources of the specified locale
- 1
- 2async echo3() {
- 3 const message = this.ctx.text.locale('zh-cn', 'Hello World');
- 4 this.ctx.success(message);
- 5}
Override Language Resources
Use project level
language resources to override module level
language resources, thereby changing 世界,您好
to 您好,世界
src/backend/config/locale/zh-cn.js
- 1module.exports = {
- 2 'Hello World': '您好,世界',
- 3};
Comments: