EggJS provides the features of configuration
and i18n
at project level
. CabloyJS extends the features provided by EggJS, realizing the configuration
and i18n
features at the module level
- Module can implement its own
configuration
andi18n
- The
configuration
andi18n
ofproject level
can override the ones ofmodule level
Configuration
1. Definition of Config
src/suite-vendor/test-party/modules/test-party/backend/src/config/config.js
- 1module.exports = appInfo => {
- 2 const config = {};
- 3
- 4 config.message = 'Hello World';
- 5
- 6 return config;
- 7};
2. Usage of Config
src/suite-vendor/test-party/modules/test-party/backend/src/controller/kitchen-sink/guide.js
- 1async echo2() {
- 2 const message = this.ctx.config.message;
- 3 this.ctx.success(message);
- 4}
3. Override Config
Use project level
config to override module level
config, thereby changing Hello World
to Hello World!
src/backend/config/config.default.js
- 1// modules
- 2config.modules = {
- 3 'test-party': {
- 4 message: 'Hello World!',
- 5 },
- 6};
i18n
1. 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};
2. Usage of Language Resources
CabloyJS 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
3. 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: