UNPKG

@gulibs/react-vintl

Version:

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

83 lines 2.72 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) */ export type TranslationFunction = (key: I18nKeys, params?: Record<string, unknown>) => string; /** * 翻译函数类型(带 namespace) */ export type NamespacedTranslationFunction<N extends string> = (key: ExtractNamespaceKeys<I18nKeys, N>, params?: Record<string, unknown>) => string; /** * 翻译上下文类型 */ export interface I18nContextType { /** 当前语言 */ locale: string; /** 支持的语言列表 */ supportedLocales: string[]; /** 翻译函数 */ t: TranslationFunction; /** 切换语言 */ setLocale: (locale: string) => void; /** 翻译资源 */ resources: I18nResources; } /** * 翻译组件属性(不带 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