UNPKG

@gulibs/react-vintl

Version:

Type-safe i18n library for React with Vite plugin and automatic type inference

112 lines 4.3 kB
/** * 从虚拟模块重新导出类型 * 这样所有代码都会使用生成的联合类型 * * 注意:这些类型在运行时由虚拟模块提供,构建时从虚拟模块获取 */ export type { I18nKeys, I18nLocales, I18nResources, I18nNamespaces } from '@gulibs/react-vintl-locales'; import type { I18nKeys, I18nResources, I18nNamespaces } from '@gulibs/react-vintl-locales'; /** * 从完整键路径中提取指定 namespace 的相对路径 * * 例如: * I18nKeys = 'common.welcome' | 'common.buttons.save' | 'auth.login.title' * ExtractNamespaceKeys<I18nKeys, 'common'> = 'welcome' | 'buttons.save' */ export type ExtractNamespaceKeys<Keys extends string, Namespace extends string> = Keys extends `${Namespace}.${infer Rest}` ? Rest : string; /** * 翻译函数类型(不带 namespace) * 修复:当 I18nKeys 无法解析时(如其他项目),fallback 到 string */ export type TranslationFunction = (key: I18nKeys extends never ? string : I18nKeys, params?: Record<string, unknown>) => string; /** * 泛型版本的翻译函数类型,允许用户指定键类型 * 用于跨项目使用或动态键的场景 */ export type FlexibleTranslationFunction<K extends string = string> = (key: K, params?: Record<string, unknown>) => string; /** * 类型工具:确保翻译函数类型安全,即使 I18nKeys 无法解析 * 在其他项目中使用时,如果 I18nKeys 是 never,fallback 到 string */ export type SafeTranslationFunction = TranslationFunction extends (key: infer K, ...args: any[]) => any ? K extends never ? (key: string, ...args: any[]) => string : TranslationFunction : (key: string, ...args: any[]) => string; /** * 类型工具:将翻译函数转换为泛型版本 * 用于在其他项目中封装翻译函数 */ export type WrapTranslationFunction<T extends TranslationFunction> = T extends (key: infer K, ...args: any[]) => any ? K extends never ? FlexibleTranslationFunction<string> : FlexibleTranslationFunction<K extends string ? K : string> : FlexibleTranslationFunction<string>; /** * 翻译函数类型(带 namespace) */ export type NamespacedTranslationFunction<N extends string> = (key: ExtractNamespaceKeys<I18nKeys, N>, params?: Record<string, unknown>) => string; /** * 更新资源的选项 */ export interface UpdateResourcesOptions { /** 是否合并(默认 true),false 时完全替换 */ merge?: boolean; /** 如果指定,只合并到该 namespace */ namespace?: string; /** 如果指定,只合并该 locale */ locale?: string; } /** * 翻译上下文类型 */ export interface I18nContextType { /** 当前语言 */ locale: string; /** 支持的语言列表 */ supportedLocales: string[]; /** 翻译函数 */ t: TranslationFunction; /** 切换语言 */ setLocale: (locale: string) => void; /** 翻译资源 */ resources: I18nResources; /** 更新翻译资源 */ updateResources: (newResources: Partial<I18nResources>, options?: UpdateResourcesOptions) => void; } /** * 翻译组件属性(不带 namespace) */ export interface TranslationPropsBase { /** 翻译键路径 */ keyPath: I18nKeys; /** 参数 */ params?: Record<string, unknown>; /** 默认值 */ fallback?: string; /** 子组件渲染函数 */ children?: (translation: string) => React.ReactNode; } /** * 翻译组件属性(带 namespace) */ export interface NamespacedTranslationProps<N extends I18nNamespaces> { /** 命名空间 */ namespace: N; /** 翻译键路径(相对于 namespace) */ keyPath: ExtractNamespaceKeys<I18nKeys, N>; /** 参数 */ params?: Record<string, unknown>; /** 默认值 */ fallback?: string; /** 子组件渲染函数 */ children?: (translation: string) => React.ReactNode; } /** * 翻译组件属性联合类型 */ export type TranslationProps = TranslationPropsBase | NamespacedTranslationProps<I18nNamespaces>; /** * 翻译提供者属性 */ export interface I18nProviderProps { /** 翻译资源 */ resources: I18nResources; /** 默认语言 */ defaultLocale?: string; /** 支持的语言列表 */ supportedLocales?: string[]; /** 子组件 */ children: React.ReactNode; } //# sourceMappingURL=types.d.ts.map