Skip to content

I18n

Modules can individually provide their own I18n language resources

Define language resources

Taking the module a-demo as an example, define the I18n language resources of the module:

English: src/suite/a-demo/modules/a-demo/src/config/locale/en-us.ts

typescript
export default {
  HelloWorld: 'Hello World',
};

Chinese: src/suite/a-demo/modules/a-demo/src/config/locale/zh-cn.ts

typescript
export default {
  HelloWorld: '您好世界',
};

Use language resources

The I18n language resources of the module can be obtained through the locale object of the Scope instance

typescript
export class TestA {
  protected async __init__() {
    // use current locale
    const message1 = this.scope.locale.HelloWorld();
    // use locale en-us
    const message2 = this.scope.locale.HelloWorld.locale('en-us');
    // use locale zh-cn
    const message3 = this.scope.locale.HelloWorld.locale('zh-cn');
    console.log(message1, message2, message3);
  }
}
  • Gif Demonstration scope-locale

Use language resources cross-module

typescript
import type { ScopeModuleADemo } from 'zova-module-a-demo';

export class TestA {
  @UseScope('a-demo')
  scopeModuleADemo: ScopeModuleADemo;

  protected async __init__() {
    // use current locale
    const message1 = this.scopeModuleADemo.locale.HelloWorld();
    // use locale en-us
    const message2 = this.scopeModuleADemo.locale.HelloWorld.locale('en-us');
    // use locale zh-cn
    const message3 = this.scopeModuleADemo.locale.HelloWorld.locale('zh-cn');
    console.log(message1, message2, message3);
  }
}

Override language resources

You can use project-level language resources to override module-level language resources

English: src/front/config/locale/en-us.ts

typescript
export default {
  modules: {
    'a-demo': {
      HelloWorld: 'Hello World!!!',
    },
  },
};

Chinese: src/front/config/locale/zh-cn.ts

typescript
export default {
  modules: {
    'a-demo': {
      HelloWorld: '您好世界!!!',
    },
  },
};

Released under the MIT License.