MCPcopy Create free account
hub / github.com/esengine/esengine / useLocale

Function useLocale

packages/editor-app/src/hooks/useLocale.ts:34–86  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

32 * ```
33 */
34export function useLocale() {
35 const localeService = useMemo(() => Core.services.resolve(LocaleService), []);
36 const [locale, setLocale] = useState<Locale>(() => localeService.getCurrentLocale());
37
38 useEffect(() => {
39 const unsubscribe = localeService.onChange((newLocale) => {
40 setLocale(newLocale);
41 });
42
43 return unsubscribe;
44 }, [localeService]);
45
46 /**
47 * 翻译函数
48 * Translation function
49 *
50 * @param key - 翻译键 | Translation key
51 * @param params - 可选参数,用于替换 {{key}} | Optional params for {{key}} substitution
52 * @param fallback - 回退文本 | Fallback text
53 */
54 const t = useCallback(
55 (key: string, params?: TranslationParams, fallback?: string) => {
56 return localeService.t(key, params, fallback);
57 },
58 [localeService]
59 );
60
61 /**
62 * 切换语言
63 * Change locale
64 */
65 const changeLocale = useCallback(
66 (newLocale: Locale) => {
67 localeService.setLocale(newLocale);
68 },
69 [localeService]
70 );
71
72 /**
73 * 获取支持的语言列表
74 * Get supported locales
75 */
76 const supportedLocales: readonly LocaleInfo[] = useMemo(() => {
77 return localeService.getSupportedLocales();
78 }, [localeService]);
79
80 return {
81 locale,
82 t,
83 changeLocale,
84 supportedLocales
85 };
86}

Callers 15

AppFunction · 0.90
CompileDialogFunction · 0.90
ExportRuntimeDialogFunction · 0.90
SceneHierarchyFunction · 0.90
TitleBarFunction · 0.90
AboutDialogFunction · 0.90
ArrayFieldFunction · 0.90
BuildErrorDisplayFunction · 0.90
BuildSettingsPanelFunction · 0.90
SettingsWindowFunction · 0.90
StartupPageFunction · 0.90
MainToolbarFunction · 0.90

Calls 6

getCurrentLocaleMethod · 0.80
getSupportedLocalesMethod · 0.80
resolveMethod · 0.65
onChangeMethod · 0.65
tMethod · 0.65
setLocaleMethod · 0.65

Tested by

no test coverage detected