ctx.i18n

The i18n instance of the current context, used for reading or switching languages. Use ctx.t() for translating text; do not use ctx.i18n.t.

Scenarios

All RunJS execution environments can use ctx.i18n (e.g., JSBlock, JSField, JSItem, JSColumn, Workflow, Linkage Rules, etc.).

Type Definition

interface i18n: {
  language: string;
  changeLanguage(lng: string): Promise<any>;
}

Common Properties

PropertyTypeDescription
languagestringThe currently active language code (e.g., en-US, zh-CN)

Common Methods

changeLanguage(lng)

Switches the current language.

ParameterTypeDescription
lngstringTarget language code (e.g., 'en-US', 'zh-CN')

Returns: Promise<any>, resolves after the language switch is complete.

Examples

Reading the current language

const lang = ctx.i18n.language;
// 'en-US' | 'zh-CN' | ...
if (lang.startsWith('zh')) {
  ctx.render(ctx.t('Chinese UI'));
} else {
  ctx.render(ctx.t('English UI'));
}

Switching languages

// Switch to English
await ctx.i18n.changeLanguage('en-US');

// Switch to Chinese
await ctx.i18n.changeLanguage('zh-CN');

Language switch button

const { Button } = ctx.libs.antd;
const isZh = ctx.i18n.language.startsWith('zh');
ctx.render(
  <Button onClick={async () => {
    await ctx.i18n.changeLanguage(isZh ? 'en-US' : 'zh-CN');
  }}>
    {ctx.t(isZh ? 'Switch to English' : 'Switch to Chinese')}
  </Button>,
);

Notes

  • Translation text: Use ctx.t() consistently; do not use ctx.i18n.t.
  • ctx.t(): Translate text, use this method consistently.