@musecanto/editor-i18n
轻量级实例绑定国际化方案、通知通道与消息词条系统。
让文档进入你的工作流
@musecanto/editor-i18n 是 MuseCanto Editor 的国际化与通知底座。它完全基于纯 TypeScript 实现,不依赖 React 或全局单例,支持在同一页面中为不同编辑器实例分配不同的语言区域(Locale)与定制词条。
安装
pnpm add @musecanto/editor-i18n核心设计特性
- 实例级作用域 (Instance-Scoped):避免在
window或模块全局维护单个currentLocale。微前端或多编辑器共存场景下,各个实例互不干扰。 - 纯逻辑可运行:在无 React 的 Node.js 或纯 Tiptap 扩展中直接调用翻译函数。
- 分层词条合并:词条按「内置英文兜底 $\to$ 内置当前语言 $\to$ 功能扩展注入 $\to$ 宿主自定义覆盖」的优先级合并。
- 统一通知通道:规范了错误提醒、上传状态等消息通知的抽象接口。
基础使用
创建翻译器 (createEditorI18n)
import {
createEditorI18n,
zhMessages,
enMessages,
} from "@musecanto/editor-i18n";
// 创建中文翻译器
const i18nZh = createEditorI18n("zh");
console.log(i18nZh.t("common.save")); // 输出:"保存"
// 创建英文翻译器
const i18nEn = createEditorI18n("en");
console.log(i18nEn.t("common.save")); // 输出:"Save"
// 支持插值参数
console.log(i18nZh.t("upload.fileTooLarge", { max: "10MB" }));覆盖与扩展自定义词条
import { createEditorI18n } from "@musecanto/editor-i18n";
const customI18n = createEditorI18n("zh", {
common: {
save: "立即发布", // 覆盖默认的 "保存"
},
myFeature: {
title: "自定义功能提示",
},
});
console.log(customI18n.t("common.save")); // 输出:"立即发布"通知通道规范 (EditorNotificationsOptions)
为保持核心与特定 Toast 库(如 sonner, react-hot-toast)解耦,通知协议定义了抽象接口:
interface EditorNotificationsOptions {
/** 错误通知派发 */
notifyError?: (message: string, error?: unknown) => void;
/** 成功通知派发 */
notifySuccess?: (message: string) => void;
/** 实例级翻译函数,供通知器生成本地化文案 */
t?: EditorTranslator;
}在 @musecanto/editor-kit 中,默认已接入 sonner 实现友好的浮层 Toast 提示,亦可在配置中无缝替换为业务自定义的通知弹窗。