UNPKG

macoolka-i18n

Version:

`macoolka-i18n` is a library for internationalization in TypeScript. It easily integrates some localization features to your Application.

52 lines (51 loc) 2.73 kB
import * as F from '../format'; export declare type IDFormat<K, A> = K; export declare type PartialAll<T> = T extends Record<any, any> ? { [P in keyof T]?: PartialAll<T[P]>; } : T; export declare type PrimitiveType = string | number | boolean | null | undefined | Date; export interface Message<K extends string = string, V extends string = string> { /** * The is a tag,It is unique string on the application * @desczh * 这是一个标志,在应用程序中不能重复 */ id: K; defaultMessage?: string; /** * The is a record object,will be used when parse message * @desczh * 在解析消息时用到的数据结构 */ value?: Record<V, PrimitiveType>; } export declare type ErrorMessage<K extends string = string, V extends string = string> = Message<K, V> & { error?: Error; title?: string; }; export declare type I18NFormat<MK extends string = string, DK extends string = string, TK extends string = string, NK extends string = string, PK extends string = string, RK extends string = string> = { formatMessage: (option: Message<MK>) => string; formatErrorMessage: (option: ErrorMessage<MK>) => string; formatRelativeTime: (option: IDFormat<RK, F.FormatRelativeTimeOptions>) => (value: F.FormatRelativeTimeValue) => string; formatDate: (option: IDFormat<DK, F.FormatDateTimeOptions>) => (value: F.FormatDateTimeValue) => string; formatTime: (option: IDFormat<TK, F.FormatDateTimeOptions>) => (value: F.FormatDateTimeValue) => string; formatNumber: (option: IDFormat<NK, F.FormatNumberOptions>) => (value: F.FormatNumberValue) => string; formatPlural: (option: IDFormat<PK, F.FormatPluralOptions>) => (value: F.FormatPluralValue) => string; formatMessageHtml: (option: Message) => string; getTemplate: () => I18NTemplate; getLocale: () => string; }; export interface I18NTemplate<MK extends string = string, DK extends string = string, TK extends string = string, NK extends string = string, PK extends string = string, RK extends string = string> { messages: Record<MK, string>; relative: Record<RK, F.FormatRelativeTimeOptions>; plural: Record<PK, F.FormatPluralOptions>; number: Record<NK, F.FormatNumberOptions>; date: Record<DK, F.FormatDateTimeOptions>; time: Record<TK, F.FormatDateTimeOptions>; } export interface I18NConfig<MK extends string = string, DK extends string = string, TK extends string = string, NK extends string = string, PK extends string = string, RK extends string = string> { defaultLocale: string; template: I18NTemplate<MK, DK, TK, NK, PK, RK>; templates: Record<string, PartialAll<I18NTemplate<MK, DK, TK, NK, PK, RK>>>; formatters: F.Formats; }