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

  1. Module can implement its own configuration and i18n
  2. The configuration and i18n of project level can override the ones of module level

Configuration

1. Definition of Config

src/suite-vendor/test-party/modules/test-party/backend/src/config/config.js

  1. 1module.exports = appInfo => {
  2. 2 const config = {};
  3. 3
  4. 4 config.message = 'Hello World';
  5. 5
  6. 6 return config;
  7. 7};

2. Usage of Config

src/suite-vendor/test-party/modules/test-party/backend/src/controller/kitchen-sink/guide.js

  1. 1async echo2() {
  2. 2 const message = this.ctx.config.message;
  3. 3 this.ctx.success(message);
  4. 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. 1// modules
  2. 2config.modules = {
  3. 3 'test-party': {
  4. 4 message: 'Hello World!',
  5. 5 },
  6. 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

  1. 1module.exports = {
  2. 2 'Hello World': '世界,您好',
  3. 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

  1. 1async echo3() {
  2. 2 const message = this.ctx.text('Hello World');
  3. 3 this.ctx.success(message);
  4. 4}

How to change the locale configuration of the client?

  • Open the home page, entry Mine->Info,update the field of Locale, 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

  1. 1module.exports = {
  2. 2 'Hello World': '您好,世界',
  3. 3};