nocobase-app-plugin-i18n
它能做什么
这项技能指导在 NocoBase 3 应用或插件中添加和修改翻译文字。适合需要应用支持多种语言的开发者。
适用场景
- ✓当你新增一个需要以多种语言显示的标签、按钮或消息时。
- ✓当你需要按收件人的语言翻译服务器错误或邮件时。
- ✓当你想在已有语言文件的包中添加新语言时。
技能链接
https://github.com/nocobase/nocobase/blob/v3-develop/packages/plugins/app-plugin-i18n/skills/nocobase-app-plugin-i18n/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我把 NocoBase 应用中新的“保存”按钮和错误消息翻译成葡萄牙语和西班牙语。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 会把文字放在正确的语言文件和命名空间中,并以翻译键的形式引用。然后通过浏览器或服务器端的翻译辅助工具显示这些文字。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Goal
Put user-facing text behind a translation key correctly the first time: in the right file, under the right namespace, and in a way that survives a language switch.
Scope
- Wire locale files into a package that has none yet.
- Add a string to an application or a plugin and render it translated, in the browser or on the server.
- Throw an error that callers see in their own language, and translate mail or jobs for their recipient.
- Add a language to a package that already has locale files.
- Reword a plugin's copy from the application, without editing the plugin.
- Translate a menu or breadcrumb title.
Non-Goals
- Do not build a language picker.
useAppLocale()exists;app-template-defaultalready renders one inclient/layouts/components/language-switcher.tsx. - Do not add i18n machinery to an application. The plugin is registered by default, the client runtime is built by
createAppI18nRuntime, and the server's byI18nProvider. - Do not configure the list of languages. It is whatever the application's own
locales/index.tsdeclares; configuration names only the default,i18n.defaultLocale(overridden byAPP_DEFAULT_LOCALE). The@nocobase/app-plugin-i18nREADME covers that,useAppLocale(), the endpoints, anduseSyncServerLocale(), which the template shell already calls to keep the server's language in step with the browser's.
The one rule that decides everything
A namespace is a package name, and the namespace in scope follows the render tree.
An application's pages get the application's namespace; a plugin's pages get that plugin's, because the host wraps each contribution using the packageName it already records. So a component in its own package's routes translates with no namespace at all.
The exception is what most mistakes come from: a component a plugin exports for the application to render is in the application's scope, not its own. It has to name its namespace.
Procedure
Wiring locales into a package for the first time
A package has a client/locales/ and a server/locales/ tree, each with an en-US.ts, the other languages, and an index.ts mapping every language to a dynamic import:
// client/locales/index.ts, and server/locales/index.ts in the same shape
import type { LocaleLoaders } from '@nocobase/i18n';
const locales: LocaleLoaders = {
'en-US': () => import('./en-US.js'),
'zh-CN': () => import('./zh-CN.js'),
};
export default locales;
Import that module and pass it as locales, the same way on both sides:
// client/plugin.ts
import locales from './locales/index.js';
export default defineClientPlugin({
packageName: '@acme/app-plugin-orders',
locales,
routes,
});
// server/plugin.ts
import locales from './locales/index.js';
export default defineServerPlugin({
baseDir,
packageName: '@acme/app-plugin-orders',
locales,
});
Import it statically. Each language is already a separate dynamic import, so a language is loaded only when something needs it; deferring the map as well gains nothing. locales: () => import('./locales/index.js') is still accepted on both sides for code written that way.
Without this step every t() in the package renders its key. An application's own client/runtime.ts and server/runtime.ts pass their locales the same way.
Adding a string
- Find the package that owns the text. Application text goes in
client/locales/; a plugin's text goes in that plugin'sclient/locales/. - Add the key to
en-US.ts. The structure is written once: the type is derived from the value withLocaleResource<typeof enUS>, and other locales are annotated with it, which is what makes a typo a compile error. - Add the translation to every other locale file. A missing key falls back rather than breaking, so this can lag, and
typecheckreports it. - Render it with
useTranslationfrom@nocobase/i18n/client.
import { useTranslation } from '@nocobase/i18n/client';
const { t } = useTranslation();
t('actions.save');
Keys nest and are addressed with dots: t('trigger.types.schedule').
Naming translation keys
Use short semantic keys that describe the purpose of the text and stay valid when its wording changes. Use lower camelCase within each segment and dots for useful feature or context groups, such as files.uploadFailed or demo.pageDescription. A small namespace may use title directly. Do not use complete English sentences as keys or merely convert a sent
常见问题
它会添加语言选择器吗?
不会。应用模板已经包含语言选择器,技能不会再添加一个。
语言列表在哪里设置?
在应用自己的 locales 语言文件中。
这项技能主要关注什么?
把文字放在翻译键后面,并放入正确的文件中,使切换语言后仍然有效。
详情
作者的原始描述
Use when adding or changing user-facing text in a NocoBase application or plugin: writing locale files, wiring a package's locales for the first time, translating a component or a menu title, naming a namespace, translating server responses, errors and outbound mail, or adding a language. Not for building page UI around the text (nocobase-app-development) or scaffolding a new plugin (nocobase-app-plugin-development); those Skills hand translation work here.
repo: nocobase/nocobase
热度
- GitHub 星标
- 25k
- 排名
- #10099
- 许可证
- —
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
nocobase-app-development
25k帮你的智能体为完整的 NocoBase 3 业务应用构建页面、路由、数据、权限和测试。
shadcn
25k帮你的智能体在 Web 项目中查找、添加、修复和美化 shadcn/ui 组件,并使用其官方文档和注册表。
nocobase-app-plugin-development
25k帮你的智能体在现有应用中创建和维护 NocoBase 3 插件,包括服务端、客户端、数据库和注册部分。
nocobase-app-upgrade
25k帮你的智能体把 NocoBase 应用升级到较新的模板版本,引入模板的更改,同时保留你自己的工作。