# ctx.i18n

The i18n instance of the current context, used for reading or switching languages. **Use [ctx.t()](./t.md) 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

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

## Common Properties

| Property | Type | Description |
|------|------|------|
| `language` | `string` | The currently active language code (e.g., `en-US`, `zh-CN`) |

## Common Methods

### changeLanguage(lng)

Switches the current language.

| Parameter | Type | Description |
|------|------|------|
| `lng` | `string` | Target language code (e.g., `'en-US'`, `'zh-CN'`) |

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

## Examples

### Reading the current language

```ts
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

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

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

### Language switch button

```ts
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`.

## Related

- [ctx.t()](./t.md): Translate text, use this method consistently.