docs/docs/en/runjs/context/t.md
An i18n shortcut function used in RunJS to translate text based on the current context's language settings. It is suitable for internationalizing inline copy such as buttons, titles, and prompts.
ctx.t() can be used in all RunJS execution environments.
t(key: string, options?: Record<string, any>): string
| Parameter | Type | Description |
|---|---|---|
key | string | Translation key or template with placeholders (e.g., Hello {{name}}, {{count}} rows). |
options | object | Optional. Interpolation variables (e.g., { name: 'John', count: 5 }), or i18n options (e.g., defaultValue, ns). |
defaultValue is provided, it may return the key itself or the interpolated string.The default namespace for the RunJS environment is runjs. When ns is not specified, ctx.t(key) will look up the key in the runjs namespace.
// Looks up key from the 'runjs' namespace by default
ctx.t('Submit'); // Equivalent to ctx.t('Submit', { ns: 'runjs' })
// Looks up key from a specific namespace
ctx.t('Submit', { ns: 'myModule' });
// Searches multiple namespaces sequentially (first 'runjs', then 'common')
ctx.t('Save', { ns: ['runjs', 'common'] });
ctx.t('Submit');
ctx.t('No data');
const text = ctx.t('Hello {{name}}', { name: ctx.user?.nickname || 'Guest' });
ctx.render(`<div>${text}</div>`);
ctx.message.success(ctx.t('Processed {{count}} rows', { count: rows.length }));
if (minutes < 60) return ctx.t('{{count}} minutes ago', { count: minutes });
if (hours < 24) return ctx.t('{{count}} hours ago', { count: hours });
ctx.t('Hello {{name}}', { name: 'Guest', ns: 'myModule' });
{{variableName}} in the key and pass the corresponding variable in options to replace it.ctx.i18n.language, user locale).