UNPKG

@lingui/core

Version:

Internationalization (i18n) tools for JavaScript

173 lines (165 loc) 6.34 kB
import { CompiledMessage } from '@lingui/message-utils/compileMessage'; declare const defaultLocale = "en"; type DateTimeFormatSize = "short" | "default" | "long" | "full"; type DateTimeFormatValue = Parameters<Intl.DateTimeFormat["format"]>[0]; type NumberFormatValue = Parameters<Intl.NumberFormat["format"]>[0]; /** * @deprecated Use `Intl.DateTimeFormat` directly. This helper will be removed. */ declare function date(locales: Locales, value: string | DateTimeFormatValue, format?: Intl.DateTimeFormatOptions | DateTimeFormatSize): string; /** * @deprecated Use `Intl.DateTimeFormat` directly. This helper will be removed. */ declare function time(locales: Locales, value: string | DateTimeFormatValue, format?: Intl.DateTimeFormatOptions | DateTimeFormatSize): string; /** * @deprecated Use `Intl.NumberFormat` directly. This helper will be removed. */ declare function number(locales: Locales, value: NumberFormatValue, format?: Intl.NumberFormatOptions): string; type PluralOptions = { [key: string]: Intl.LDMLPluralRule; } & { offset: number; other: string; }; declare function plural(locales: Locales, ordinal: boolean, value: number, { offset, ...rules }: PluralOptions): string; type formats_DateTimeFormatSize = DateTimeFormatSize; type formats_DateTimeFormatValue = DateTimeFormatValue; type formats_NumberFormatValue = NumberFormatValue; type formats_PluralOptions = PluralOptions; declare const formats_date: typeof date; declare const formats_defaultLocale: typeof defaultLocale; declare const formats_number: typeof number; declare const formats_plural: typeof plural; declare const formats_time: typeof time; declare namespace formats { export { formats_date as date, formats_defaultLocale as defaultLocale, formats_number as number, formats_plural as plural, formats_time as time }; export type { formats_DateTimeFormatSize as DateTimeFormatSize, formats_DateTimeFormatValue as DateTimeFormatValue, formats_NumberFormatValue as NumberFormatValue, formats_PluralOptions as PluralOptions }; } declare class EventEmitter<Events extends { [name: string]: (...args: any[]) => void; }> { private readonly _events; on<E extends keyof Events>(event: E, listener: Events[E]): () => void; removeListener<E extends keyof Events>(event: E, listener: Events[E]): void; emit<E extends keyof Events>(event: E, ...args: Parameters<Events[E]>): void; } type MessageOptions = { message?: string; formats?: Formats; comment?: string; }; type Locale = string; type Locales = Locale | Locale[]; type Formats = Record<string, Intl.DateTimeFormatOptions | Intl.NumberFormatOptions>; type Values = Record<string, unknown>; type UncompiledMessage = string; type Messages = Record<string, UncompiledMessage | CompiledMessage>; type AllMessages = Record<Locale, Messages>; /** * Register interface for module augmentation. * Users can augment this interface to narrow MessageId to a specific union type. * * @example * ```ts * // src/lingui.d.ts * import type enMessages from "./locales/en/messages.json"; * * declare module "@lingui/core" { * interface Register { * messageIds: keyof typeof enMessages; * } * } * ``` */ interface Register { } /** * Resolves to the registered message ID union, or falls back to `string` * when no augmentation exists. */ type MessageId = Register extends { messageIds: infer TIds extends string; } ? TIds : string; type MessageDescriptor = { id: MessageId; comment?: string; message?: string; values?: Record<string, unknown>; }; type MissingMessageEvent = { locale: Locale; id: MessageId; }; type MissingHandler = string | ((locale: string, id: string) => string); type I18nProps = { locale?: Locale; locales?: Locales; messages?: AllMessages; missing?: MissingHandler; }; type Events = { change: () => void; missing: (event: MissingMessageEvent) => void; }; type LoadAndActivateOptions = { /** initial active locale */ locale: Locale; /** list of alternative locales (BCP 47 language tags) which are used for number and date formatting */ locales?: Locales; /** compiled message catalog */ messages: Messages; }; type MessageCompiler = (message: string) => CompiledMessage; declare class I18n extends EventEmitter<Events> { private _locale; private _locales?; private _messages; private _missing?; private _messageCompiler?; constructor(params: I18nProps); get locale(): string; get locales(): Locales | undefined; get messages(): Messages; /** * Registers a `MessageCompiler` to enable the use of uncompiled catalogs at runtime. * * In production builds, the `MessageCompiler` is typically excluded to reduce bundle size. * By default, message catalogs should be precompiled during the build process. However, * if you need to compile catalogs at runtime, you can use this method to set a message compiler. * * Example usage: * * ```ts * import { compileMessage } from "@lingui/message-utils/compileMessage"; * * i18n.setMessagesCompiler(compileMessage); * ``` */ setMessagesCompiler(compiler: MessageCompiler): this; private _load; load(allMessages: AllMessages): void; load(locale: Locale, messages: Messages): void; /** * @param options {@link LoadAndActivateOptions} */ loadAndActivate({ locale, locales, messages }: LoadAndActivateOptions): void; activate(locale: Locale, locales?: Locales): void; _(descriptor: MessageDescriptor): string; _(id: MessageId, values?: Values, options?: MessageOptions): string; /** * Alias for {@see I18n._} */ t: I18n["_"]; /** * @deprecated Use `Intl.DateTimeFormat` directly. This helper will be removed. */ date(value?: string | DateTimeFormatValue, format?: Intl.DateTimeFormatOptions): string; /** * @deprecated Use `Intl.NumberFormat` directly. This helper will be removed. */ number(value: NumberFormatValue, format?: Intl.NumberFormatOptions): string; } declare function setupI18n(params?: I18nProps): I18n; declare const i18n: I18n; export { I18n, formats, i18n, setupI18n }; export type { AllMessages, Locale, Locales, MessageDescriptor, MessageId, MessageOptions, Messages, Register };