演示

useLazyLocaleTranslation

已登录 ERP 页面的推荐 i18n hook。按 setting.locale 按需加载 JSON。从 @quan-erp/shared-ui 导入。

何时使用

  • 能读取 setting.locale 的已登录页面
  • 新代码优先于同步 useLocaleTranslation
  • 公开 / 登录前页面改用 usePublicLazyLocaleTranslation

签名

传入 LazyLocaleType 动态导入映射,得到 translation 与 isLoading。

TSsignature
1function useLazyLocaleTranslation( 2 locales: LazyLocaleType, 3): { 4 translation: { 5 get: (key: string, fallback?: string, data?: Record<string, unknown>) => string; 6 }; 7 isLoading: boolean; 8 // … 9};

用法

同目录放 locales/*.json 与 LazyLocaleType,可见文案一律 translation.get()。

TSXbranch.locale.ts
1import type { LazyLocaleType } from "@quan-erp/shared-ui"; 2 3export const BranchLocaleLazy: LazyLocaleType = { 4 "en-US": () => import("./locales/en-US.json"), 5 "my-MM": () => import("./locales/my-MM.json"), 6 "zh-CN": () => import("./locales/zh-CN.json"), 7};
TSXbranch.page.tsx
1import { useLazyLocaleTranslation } from "@quan-erp/shared-ui"; 2import { BranchLocaleLazy } from "./branch.locale"; 3 4export function BranchPage() { 5 const { translation, isLoading } = useLazyLocaleTranslation( 6 BranchLocaleLazy, 7 ); 8 9 if (isLoading) return null; 10 11 return ( 12 <h1>{translation.get("branches", "Branches")}</h1> 13 ); 14}

注意

  • 文件结构见 Core → Localization
  • 不要在 get() 外硬编码可见文案