EggJS provides the feature of i8n at project level. EggBornJS extends the feature provided by EggJS, realizing the i18n at the module level

  1. Module can specify its own i18n
  2. The i18n at project level can override the one at module 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/module/test-party/backend/src/config/locale/zh-cn.js

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

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/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

Get language resources of the specified locale


async echo3() {
  const message = this.ctx.text.locale('zh-cn', 'Hello World');
  this.ctx.success(message);
}

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': '您好,世界',
};