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

Definition of Config

src/module/test-party/backend/src/config/config.js

module.exports = appInfo => {
  const config = {};

  config.message = 'Hello World';

  return config;
};

Usage of Config

src/module/test-party/backend/src/controller/kitchen-sink/guide.js

async echo2() {
  const message = this.ctx.config.message;
  this.ctx.success(message);
}

Override Config

Use project level config to override module level config, thereby changing Hello World to Hello World!

src/backend/config/config.default.js

// modules
config.modules = {
  'test-party': {
    message: 'Hello World!',
  },
};

i18n

Definition of Language Resources

The default language for CabloyJS is en-us. If support Chinese, you need to add zh-cn resource file

src/module/test-party/backend/src/config/locale/zh-cn.js

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

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/module/test-party/backend/src/controller/kitchen-sink/guide.js

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

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

Override Language Resources

Use project level language resources to override module level language resources, thereby changing 世界,您好 to 您好,世界

src/backend/config/locale/zh-cn.js

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