import { type CompiledMessage } from "./msgfmt.ts"; import type { InferredMessageType } from "./msgfmt-parser-types.ts"; import { type ErrorHandler, type ErrorLevel } from "./error-handling.ts"; import { type ComponentPlaceholder, type Message } from "./opaque.ts"; export type { ComponentPlaceholder, Message } from "./opaque.ts"; export * from "./errors.ts"; export * from "./error-handling.ts"; declare const translationIdBrandSymbol: unique symbol; /** * A base type for a vocabulary. * * A vocabulary here means a set of translation ids required for this book of translations. * * @since 0.1.0 (`@hi18n/core`) */ export type VocabularyBase = Record>; /** * Extracts parameters required by the translated message. * * @param M the message being instantiated. * @param C replacement for the component interpolation (like `<0>` or ``). * * @since 0.1.0 (`@hi18n/core`) */ export type MessageArguments, C> = InstantiateComponentTypes>, C>; export type InjectAdditionalParams = true extends HasDate ? Args & { timeZone: string; } : Args; export type HasDate = T extends Date ? true : never; export type AbstractMessageArguments> = M extends Message ? Args : never; export type InstantiateComponentTypes = { [K in keyof Args]: InstantiateComponentType; }; export type InstantiateComponentType = T extends ComponentPlaceholder ? C : T; /** * A subtype of `string` that represents a dynamically-managed translation id. * * @param Vocabulary the vocabulary type of the Book it refers to * @param Args parameters required by this message * * @since 0.1.1 (`@hi18n/core`) */ export type TranslationId = string & { [translationIdBrandSymbol]: (catalog: Vocabulary, args: Args) => void; }; /** * Extracts the translation ids that don't take parameters. * * @param Vocabulary the vocabulary, a set of translation ids we can use for this book of translations. * @param K a dummy parameter to do a union distribution * * @since 0.1.0 (`@hi18n/core`) */ export type SimpleMessageKeys = K extends unknown ? {} extends MessageArguments ? K : never : never; /** * Wraps a MessageFormat message string in a wrapper object. * * @param s the translated message * @returns the compiled message object * * @since 0.1.0 (`@hi18n/core`) * * You can also use the `mf1` function from `@hi18n/core/msg` instead. * * @example * ```ts * export default new Book({ * "example/greeting": msg("Hello, {name}!"), * }); * ``` */ export declare function msg(s: S): InferredMessageType; /** * A tagged template function to construct a type-safe message object. * * Note the difference from the `msg` function used in a functional way * (i.e. `msg("...")`): * * - The functional form takes a single string argument * and parses it as an ICU MessageFormat 1.0 message. * - This overload is a tagged template function that takes * template strings and embedded expressions. The string parts * are treated as literal strings, so MessageFormat escapes are not interpreted. * Instead, you can use other helper functions from `@hi18n/core/msg` * (e.g. `arg`) to construct complex messages. * * You can also use the `msg` template tag from `@hi18n/core/msg` instead. * * @since 0.2.1 (`@hi18n/core`) * * @example * ```ts * const catalogEn = new Catalog("en", { * greeting: msg`Hello, ${arg("name")}!`, * }); * ``` */ export declare function msg[]>(strings: TemplateStringsArray, ...exprs: Exprs): Message[] ? { [K in keyof T]: T[K]; } : never>; export declare namespace msg { /** * Same as {@link msg} but can be used to indicate an untranslated state. * * @param s the translated message * @returns the first argument * * @since 0.1.3 (`@hi18n/core`) * * @deprecated use the `todo` function from `@hi18n/core/msg` (which is a simple identity function) * combined with `mf1` from `@hi18n/core/msg` instead. * * @example * ```ts * export default new Book({ * "example/greeting": msg.todo("Hello, {name}!"), * }); * ``` */ function todo(s: S): InferredMessageType; } /** * Marks a translation id as dynamically used with {@link CompoundTranslatorFunction.dynamic t.dynamic}. * * At runtime, it just returns the second argument. * * @param book the book the id is linked to. Just discarded at runtime. * @param id the translation id. * @returns the second argument * * @since 0.1.1 (`@hi18n/core`) * * @example * ```ts * const menus = [ * { * url: "https://example.com/home", * titleId: translationId(book, "example/navigation/home"), * }, * { * url: "https://example.com/map", * titleId: translationId(book, "example/navigation/map"), * }, * ]; * * const { t } = getTranslator(book, "en"); * t.dynamic(menus[i].titleId); * ``` */ export declare function translationId(book: Book, id: K): TranslationId>; /** * A set of translated messages, containing translations for all supported locales. * * In other words, a book is a set of {@link Catalog}s for all languages. * * @since 0.1.0 (`@hi18n/core`) * * @example * ```ts * type Vocabulary = { * "example/greeting": Message<{ name: string }>; * }; * export const book = new Book({ * en: catalogEn, * ja: catalogJa, * }); * ``` * * @example You can use `import()` to lazy-load catalogs. * Note that you need to use extra setup to avoid the * "Catalog not loaded" error. * * ```ts * type Vocabulary = { * "example/greeting": Message<{ name: string }>; * }; * export const book = new Book({ * en: () => import("./en"), * ja: () => import("./ja"), * }); * ``` */ export declare class Book { readonly catalogs: Record>; readonly _loaders: Readonly | CatalogLoader>>; _handleError?: ErrorHandler | undefined; _implicitLocale?: string | undefined; constructor(catalogs: Readonly | CatalogLoader>>, options?: BookOptions); /** * Load a catalog for specific locale. * * Consider using {@link preloadCatalogs} instead. * * @param locale locale to load * * @since 0.1.9 (`@hi18n/core`) */ loadCatalog(locale: string): Promise; handleError(e: Error, level: ErrorLevel): void; } /** * @since 0.1.7 (`@hi18n/core`) */ export type BookOptions = { /** * Custom error handler. {@link defaultErrorHandler} is used by default. * * @example * ```ts * export const book = new Book({ * en: catalogEn, * ja: catalogJa, * }, { * handleError(error, level) { * if (level === "error") { * // Report to Sentry or somewhere * } else { * console.warn(error); * } * } * }); * ``` * * @since 0.1.7 (`@hi18n/core`) */ handleError?: ErrorHandler | undefined; /** * Locale fallback to use when no valid locale is specified. * * @example * ```ts * export const book = new Book({ * en: catalogEn, * ja: catalogJa, * }, { implicitLocale: "en" }); * ``` * * @since 0.1.7 (`@hi18n/core`) */ implicitLocale?: string | undefined; }; /** * A function to asynchronously load a catalog. * * It is usually provided as `() => import("...")`. * * @since 0.1.9 (`@hi18n/core`) */ export type CatalogLoader = () => Promise<{ default: Catalog; }>; /** * A set of translated messages for a specific locale. * * @since 0.1.0 (`@hi18n/core`) * * @example * ```ts * type Vocabulary = { * "example/greeting": Message<{ name: string }>; * }; * export default new Catalog("en", { * "example/greeting": msg("Hello, {name}!"), * }); * ``` */ export declare class Catalog { readonly locale: string; readonly data: Readonly; /** * @since 0.1.6 (`@hi18n/core`) */ constructor(locale: string, data: Readonly); getCompiledMessage(id: string & keyof Vocabulary): CompiledMessage; } /** * An object returned from {@link getTranslator}. * * @since 0.1.0 (`@hi18n/core`) */ export type TranslatorObject = { /** * Returns the translated message. * * @since 0.1.0 (`@hi18n/core`) * * @example * ```ts * const { t } = getTranslator(book, "en"); * t("example/greeting-simple"); // => "Hello!" * ``` */ t: CompoundTranslatorFunction; /** * Similar to {@link TranslatorObject.t} but allows component interpolation * (i.e. to interpret commands like `<0>foo` or `foo`) * * Users usually don't need to call it manually. * See the `@hi18n/react` package for its application to React. * * @param id the id of the translation * @param interpolator functions to customize the interpolation behavior * @param options the parameters of the translation. * * @since 0.1.0 (`@hi18n/core`) */ translateWithComponents(id: K, interpolator: ComponentInterpolator, options: MessageArguments): T | string; }; type CompoundTranslatorFunction = TranslatorFunction & { /** * Returns the translated message for a dynamic id. * * @since 0.1.1 (`@hi18n/core`) * * @example * ```ts * const { t } = getTranslator(book, "en"); * t.dynamic(menus[i].titleId); // => "Map" * ``` */ dynamic: DynamicTranslatorFunction; /** * Declares a translation to be made. * * At runtime, it returns the first argument. * * @param id the id of the translation * @param options the parameters of the translation. * * @since 0.1.1 (`@hi18n/core`) * * @example * ```ts * const { t } = getTranslator(book, "en"); * t.todo("example/greeting-simple"); // => "[TODO: example/greeting-simple]" * ``` */ todo(id: string, options?: Record): string; }; type TranslatorFunction = { /** * Returns the translated message for a simple one. * * @param id the id of the translation * * @since 0.1.0 (`@hi18n/core`) * * @example * ```ts * const { t } = getTranslator(book, "en"); * t("example/greeting-simple"); // => "Hello!" * ``` */ (id: SimpleMessageKeys): string; /** * Returns the translated message. * * @param id the id of the translation * @param options the parameters of the translation. * * @since 0.1.0 (`@hi18n/core`) * * @example * ```ts * const { t } = getTranslator(book, "en"); * t("example/greeting", { name: "John" }); // => "Hello, John!" * ``` */ (id: K, options: MessageArguments): string; }; type DynamicTranslatorFunction = { /** * Returns the translated message for a simple dynamic id. * * @param id the id of the translation * * @since 0.1.1 (`@hi18n/core`) * * @example * ```ts * const { t } = getTranslator(book, "en"); * t.dynamic(menus[i].titleId); // => "Map" * ``` */ (id: TranslationId): string; /** * Returns the translated message for a dynamic id. * * @param id the id of the translation * @param options the parameters of the translation. * * @since 0.1.1 (`@hi18n/core`) * * @example * ```ts * const { t } = getTranslator(book, "en"); * t.dynamic(greetings[i].translationId, { name: "John" }); // => "Hello, John!" * ``` */ (id: TranslationId, options: InstantiateComponentTypes, never>): string; }; /** * Used in {@link TranslatorObject.translateWithComponents} to customize * the behavior of component interpolation. * * @since 0.1.0 (`@hi18n/core`) */ export type ComponentInterpolator = { collect: (submessages: (T | string)[]) => T | string; wrap: (component: C, message: T | string | undefined) => T | string; }; /** * Retrieves the translation helpers from the book and the locales. * * @param book the "book" (i.e. the set of translations) containing the desired messages. * @param locales a locale or a list of locale in the order of preference (the latter being not supported yet) * @param options.throwPromise if true, it throws a Promise instance instead of an error. Used for React Suspense integration. * @returns A set of translation helpers * * @since 0.1.0 (`@hi18n/core`) * * @example * ```ts * const { t } = getTranslator(book, "en"); * t("example/greeting-simple"); // => "Hello!" * ``` */ export declare function getTranslator(book: Book, locales: string | string[], options?: GetTranslatorOptions): TranslatorObject; /** * options for {@link getTranslator} * * @since 0.1.9 (`@hi18n/core`) */ export type GetTranslatorOptions = { /** if true, it throws a Promise instance instead of an error. Used for React Suspense integration. */ throwPromise?: boolean | undefined; }; /** * Starts loading and waits for catalogs so that {@link getTranslator} does not error * with "Catalog not loaded" error. * * It is a wrapper for {@link Book.loadCatalog}. * * @param book same as {@link getTranslator}'s `book` parameter. * @param locales same as {@link getTranslator}'s `locales` parameter. * * @since 0.1.9 (`@hi18n/core`) */ export declare function preloadCatalogs(book: Book, locales: string | string[]): Promise; /** * A convenience helper to get the default time zone. * If you need more sophisticated guess for old browsers, * consider using other libraries like `moment.tz.guess`. * * @returns the default time zone, if anything is found. Otherwise the string "UTC" * * @since 0.1.3 (`@hi18n/core`) */ export declare function getDefaultTimeZone(): string; //# sourceMappingURL=index.d.ts.map