Files
esengine/packages/editor-runtime/src/i18n/createPluginLocale.ts

239 lines
7.3 KiB
TypeScript
Raw Normal View History

feat(i18n): 统一国际化系统架构,支持插件独立翻译 (#301) * feat(i18n): 统一国际化系统架构,支持插件独立翻译 ## 主要改动 ### 核心架构 - 增强 LocaleService,支持插件命名空间翻译扩展 - 新增 editor-runtime/i18n 模块,提供 createPluginLocale/createPluginTranslator - 新增 editor-core/tokens.ts,定义 LocaleServiceToken 等服务令牌 - 改进 PluginAPI 类型安全,使用 ServiceToken<T> 替代 any ### 编辑器本地化 - 扩展 en.ts/zh.ts 翻译文件,覆盖所有 UI 组件 - 新增 es.ts 西班牙语支持 - 重构 40+ 组件使用 useLocale() hook ### 插件本地化系统 - behavior-tree-editor: 新增 locales/ 和 useBTLocale hook - material-editor: 新增 locales/ 和 useMaterialLocale hook - particle-editor: 新增 locales/ 和 useParticleLocale hook - tilemap-editor: 新增 locales/ 和 useTilemapLocale hook - ui-editor: 新增 locales/ 和 useUILocale hook ### 类型安全改进 - 修复 Debug 工具使用公共接口替代 as any - 修复 ChunkStreamingSystem 添加 forEachChunk 公共方法 - 修复 blueprint-editor 移除不必要的向后兼容代码 * fix(behavior-tree-editor): 使用 ServiceToken 模式修复服务解析 - 创建 BehaviorTreeServiceToken 遵循"谁定义接口,谁导出Token"原则 - 使用 ServiceToken.id (symbol) 注册服务到 ServiceContainer - 更新 PluginSDKRegistry.resolveService 支持 ServiceToken 检测 - BehaviorTreeEditorPanel 现在使用类型安全的 PluginAPI.resolve * fix(behavior-tree-editor): 使用 ServiceContainer.resolve 获取类注册的服务 * fix: 修复多个包的依赖和类型问题 - core: EntityDataCollector.getEntityDetails 使用 HierarchySystem 获取父实体 - ui-editor: 添加 @esengine/editor-runtime 依赖 - tilemap-editor: 添加 @esengine/editor-runtime 依赖 - particle-editor: 添加 @esengine/editor-runtime 依赖
2025-12-09 18:04:03 +08:00
/**
* Plugin Locale Factory
*
*
* Provides utilities for plugins to create their own locale hooks
* that integrate with the central LocaleService.
*
* hook
* hook LocaleService
*/
import { useState, useEffect, useCallback } from 'react';
import { LocaleService, type Locale, type TranslationParams } from '@esengine/editor-core';
import { Core } from '@esengine/ecs-framework';
/**
* Translation object structure
*
*/
export type Translations = {
[key: string]: string | Translations;
};
/**
* Plugin translations bundle
*
*/
export interface PluginTranslationsBundle<T extends Translations = Translations> {
en: T;
zh: T;
es?: T;
}
/**
* Return type of usePluginLocale hook
* usePluginLocale hook
*/
export interface PluginLocaleHook {
/** Translation function | 翻译函数 */
t: (key: string, params?: TranslationParams) => string;
/** Current locale | 当前语言 */
locale: Locale;
}
/**
* Get nested value from object by dot-separated path
*
*/
function getNestedValue(obj: Translations, key: string): string | undefined {
const keys = key.split('.');
let current: unknown = obj;
for (const k of keys) {
if (current && typeof current === 'object' && k in current) {
current = (current as Record<string, unknown>)[k];
} else {
return undefined;
}
}
return typeof current === 'string' ? current : undefined;
}
/**
* Interpolate parameters into translation string
*
*/
function interpolate(text: string, params?: TranslationParams): string {
if (!params) return text;
return text.replace(/\{\{(\w+)\}\}/g, (_, key) => {
const value = params[key];
return value !== undefined ? String(value) : `{{${key}}}`;
});
}
/**
* Creates a locale hook for a plugin with its own translations
* locale hook
*
* This factory creates a React hook that:
* 1. Syncs with the central LocaleService for locale changes
* 2. Uses plugin-specific translations
* 3. Falls back to English if translation not found
*
* React hook
* 1. LocaleService
* 2. 使
* 3. 退
*
* @param translations - Plugin translations bundle |
* @returns A React hook for accessing translations | 访 React hook
*
* @example
* ```typescript
* // In your plugin's hooks folder:
* // 在你的插件 hooks 文件夹中:
* import { createPluginLocale } from '@esengine/editor-runtime';
* import { en, zh, es } from '../locales';
*
* export const useTilemapLocale = createPluginLocale({ en, zh, es });
*
* // In your components:
* // 在你的组件中:
* const { t, locale } = useTilemapLocale();
* return <button>{t('toolbar.save')}</button>;
* ```
*/
export function createPluginLocale<T extends Translations>(
translations: PluginTranslationsBundle<T>
): () => PluginLocaleHook {
const allTranslations = {
en: translations.en,
zh: translations.zh,
es: translations.es || translations.en // Fallback to English if no Spanish
};
return function usePluginLocale(): PluginLocaleHook {
const [locale, setLocale] = useState<Locale>('en');
useEffect(() => {
// Try to get LocaleService and sync with it
// 尝试获取 LocaleService 并与之同步
try {
const localeService = Core.services.tryResolve(LocaleService);
if (localeService) {
setLocale(localeService.getCurrentLocale());
// Subscribe to locale changes
// 订阅语言变化
return localeService.onChange((newLocale) => {
setLocale(newLocale);
});
}
} catch {
// LocaleService not available, use default
// LocaleService 不可用,使用默认值
}
}, []);
const t = useCallback((key: string, params?: TranslationParams): string => {
const currentTranslations = allTranslations[locale] || allTranslations.en;
const value = getNestedValue(currentTranslations as Translations, key);
if (value) {
return interpolate(value, params);
}
// Fallback to English if current locale doesn't have the key
// 如果当前语言没有该键,回退到英语
if (locale !== 'en') {
const enValue = getNestedValue(allTranslations.en as Translations, key);
if (enValue) {
return interpolate(enValue, params);
}
}
// Return key itself as last resort
// 最后返回键本身
return key;
}, [locale]);
return { t, locale };
};
}
/**
* Creates a non-React translation function for a plugin
* React
*
* Use this for translating in non-React contexts (services, utilities, etc.)
* React 使
*
* @param translations - Plugin translations bundle |
* @returns A translation function |
*
* @example
* ```typescript
* // Create translator
* // 创建翻译器
* const translate = createPluginTranslator({ en, zh, es });
*
* // Use in non-React code
* // 在非 React 代码中使用
* const message = translate('errors.notFound', 'en');
* ```
*/
export function createPluginTranslator<T extends Translations>(
translations: PluginTranslationsBundle<T>
): (key: string, locale?: Locale, params?: TranslationParams) => string {
const allTranslations = {
en: translations.en,
zh: translations.zh,
es: translations.es || translations.en
};
return function translate(
key: string,
locale: Locale = 'en',
params?: TranslationParams
): string {
const currentTranslations = allTranslations[locale] || allTranslations.en;
const value = getNestedValue(currentTranslations as Translations, key);
if (value) {
return interpolate(value, params);
}
if (locale !== 'en') {
const enValue = getNestedValue(allTranslations.en as Translations, key);
if (enValue) {
return interpolate(enValue, params);
}
}
return key;
};
}
/**
* Gets the current locale from LocaleService
* LocaleService
*
* Use this in non-React contexts where you need the current locale
* React 使
*
* @returns Current locale or 'en' as default | 'en'
*/
export function getCurrentLocale(): Locale {
try {
const localeService = Core.services.tryResolve(LocaleService);
if (localeService) {
return localeService.getCurrentLocale();
}
} catch {
// LocaleService not available
}
return 'en';
}