docs/docs/cn/runjs/context/t.md
在 RunJS 中用于翻译文案的 i18n 快捷函数,基于当前上下文的语言设置。适合按钮、标题、提示等内联文案的国际化。
所有 RunJS 执行环境均可使用 ctx.t()。
t(key: string, options?: Record<string, any>): string
| 参数 | 类型 | 说明 |
|---|---|---|
key | string | 翻译 key 或带占位符的模板(如 Hello {{name}}、{{count}} rows) |
options | object | 可选。插值变量(如 { name: '张三', count: 5 }),或 i18n 选项(如 defaultValue、ns) |
defaultValue,可能返回 key 本身或经插值后的字符串。RunJS 环境的默认命名空间为 runjs。在不指定 ns 时,ctx.t(key) 会从 runjs 命名空间查找 key。
// 默认从 runjs 命名空间取 key
ctx.t('Submit'); // 等价于 ctx.t('Submit', { ns: 'runjs' })
// 从指定命名空间取 key
ctx.t('Submit', { ns: 'myModule' });
// 从多个命名空间依次查找(先 runjs,再 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' });
{{变量名}},在 options 中传入同名变量即可替换。ctx.i18n.language、用户 locale)决定。