/* eslint-disable @typescript-eslint/no-empty-object-type, @typescript-eslint/no-explicit-any */ import { DeferredParseError, destringify, type CompiledMessage, } from "./msgfmt.ts"; import { type EvalOption, evaluateMessage } from "./msgfmt-eval.ts"; import { parseMessage } from "./msgfmt-parser.ts"; import type { InferredMessageType } from "./msgfmt-parser-types.ts"; import { MessageError, MissingLocaleError, MissingTranslationError, NoLocaleError, ParseError, } from "./errors.ts"; import { defaultErrorHandler, type ErrorHandler, type ErrorLevel, } from "./error-handling.ts"; import { unwrap, wrap, type ComponentPlaceholder, type Message, } from "./opaque.ts"; import { msg as msgTag } from "./msg.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< M extends Message, C, > = InstantiateComponentTypes< InjectAdditionalParams>, C >; // It if uses Date, we need timeZone as well. 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< Vocabulary extends VocabularyBase, Args = {}, > = 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< Vocabulary extends VocabularyBase, K extends string & keyof Vocabulary = string & keyof Vocabulary, > = K extends unknown ? {} extends MessageArguments ? K : never : never; // TODO: remove `msg` compat function in 0.3.0 // TODO: deprecate the function once the ESLint part is ready. /** * 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 function msg(s: S): InferredMessageType; // TODO: deprecate the function once the ESLint part is ready. /** * 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 function msg[]>( strings: TemplateStringsArray, ...exprs: Exprs ): Message[] ? { [K in keyof T]: T[K] } : never>; export function msg( x: string | TemplateStringsArray, ...exprs: Message[] ): Message { if (Array.isArray(x)) { const strings = x as TemplateStringsArray; if (Array.isArray(strings.raw)) { return msgTag(strings, ...exprs); } } const s = x as string; try { return wrap(destringify(parseMessage(s))); } catch (e) { if (e instanceof ParseError) { return wrap(DeferredParseError(s, e)); } throw e; } } // eslint-disable-next-line @typescript-eslint/no-namespace 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}!"), * }); * ``` */ export function todo(s: S): InferredMessageType; } msg.todo = function todo(s: S): InferredMessageType { try { return wrap(destringify(parseMessage(s))) as InferredMessageType; } catch (e) { if (e instanceof ParseError) { return wrap(DeferredParseError(s, e)) as InferredMessageType; } throw e; } } satisfies unknown; /** * 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 function translationId< Vocabulary extends VocabularyBase, K extends string & keyof Vocabulary, >( book: Book, id: K, ): TranslationId> { const _book = book; return id as string as TranslationId< Vocabulary, AbstractMessageArguments >; } /** * 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 class Book { readonly catalogs: Record>; readonly _loaders: Readonly< Record | CatalogLoader> >; _handleError?: ErrorHandler | undefined; _implicitLocale?: string | undefined; constructor( catalogs: Readonly< Record | CatalogLoader> >, options: BookOptions = {}, ) { this.catalogs = {}; this._loaders = catalogs; this._handleError = options.handleError; this._implicitLocale = options.implicitLocale; for (const [locale, catalog] of Object.entries(catalogs)) { // Skip lazy-loaded catalogs if (typeof catalog === "function") continue; this.catalogs[locale] = catalog; if (catalog.locale !== locale) { throw new Error( `Locale mismatch: expected ${locale}, got ${catalog.locale}`, ); } } if ( this._implicitLocale != null && !hasOwn(catalogs, this._implicitLocale) ) { throw new Error(`Invalid implicitLocale: ${this._implicitLocale}`); } } /** * Load a catalog for specific locale. * * Consider using {@link preloadCatalogs} instead. * * @param locale locale to load * * @since 0.1.9 (`@hi18n/core`) */ public async loadCatalog(locale: string): Promise { const loader = this._loaders[locale]; if (typeof loader !== "function") return; const { default: catalog } = await loader(); if (catalog.locale !== locale) { throw new Error( `Locale mismatch: expected ${locale}, got ${catalog.locale}`, ); } if (this.catalogs[locale] != null) return; this.catalogs[locale] = catalog; } public handleError(e: Error, level: ErrorLevel): void { (this._handleError ?? defaultErrorHandler)(e, level); } } /** * @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 class Catalog { public readonly locale: string; public readonly data: Readonly; /** * @since 0.1.6 (`@hi18n/core`) */ constructor(locale: string, data: Readonly) { if (typeof locale !== "string") { throw new TypeError("locale must be a string"); } this.locale = locale; this.data = data; // Migration work enabled in v0.2.0. // TODO: remove it in 0.3.0 and interpret string messages as raw messages. for (const [key, message] of Object.entries(this.data)) { if (typeof message === "string") { throw new TypeError( `Translated message must be created with msg() (key: ${key})`, ); } } } getCompiledMessage(id: string & keyof Vocabulary): CompiledMessage { if (!hasOwn(this.data, id)) { throw new MissingTranslationError(); } return unwrap(this.data[id]); } } /** * 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 function getTranslator( book: Book, locales: string | string[], options: GetTranslatorOptions = {}, ): TranslatorObject { const locale = selectLocale(book, locales); const catalog = book.catalogs[locale]; if (catalog == null) { if (options.throwPromise) { // eslint-disable-next-line @typescript-eslint/only-throw-error throw book.loadCatalog(locale); } else { throw new Error(`Catalog not loaded: ${locale}`); } } const t = (id: string, options: Record = {}): string => { try { return compileAndEvaluateMessage( catalog, locale, id, { timeZone: options["timeZone"] as string | undefined, params: options, handleError: book._handleError, }, ); } catch (e) { if (!(e instanceof Error)) throw e; book.handleError(e, "error"); return `[${id}]`; } }; t.dynamic = t; t.todo = (id: string) => { return `[TODO: ${id}]`; }; return { t: t as CompoundTranslatorFunction, translateWithComponents: ( id: K, interpolator: ComponentInterpolator, options: MessageArguments, ): T | string => { try { return compileAndEvaluateMessage(catalog, locale, id, { timeZone: options["timeZone"] as string | undefined, params: options, handleError: book._handleError, collect: interpolator.collect, wrap: interpolator.wrap as ComponentInterpolator["wrap"], }); } catch (e) { if (!(e instanceof Error)) throw e; book.handleError(e, "error"); return `[${id}]`; } }, }; } /** * 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 async function preloadCatalogs( book: Book, locales: string | string[], ): Promise { const locale = selectLocale(book, locales); const catalog = book.catalogs[locale]; if (catalog == null) { await book.loadCatalog(locale); } } // To ensure deterministic behavior, // this function picks the locale whether the catalog has already been loaded or not. function selectLocale( book: Book, locales: string | string[], ): string { const localesArray = Array.isArray(locales) ? locales : [locales]; const filteredLocales: string[] = []; for (const locale of localesArray) { if (hasOwn(book._loaders, locale)) { filteredLocales.push(locale); } } if (filteredLocales.length === 0) { const error = localesArray.length === 0 ? new NoLocaleError() : new MissingLocaleError({ locale: localesArray[0]!, availableLocales: Object.keys(book._loaders), }); if (book._implicitLocale != null) { book.handleError(error, "error"); filteredLocales.push(book._implicitLocale); } else { throw error; } } return filteredLocales[0]!; } function compileAndEvaluateMessage( catalog: Catalog, locale: string, id: string & keyof Vocabulary, options: Omit, "id" | "locale">, ) { try { return evaluateMessage(catalog.getCompiledMessage(id), { id, locale, ...options, }); } catch (e) { if (!(e instanceof Error)) throw e; throw new MessageError({ cause: e, id, locale, }); } } /** * 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 function getDefaultTimeZone(): string { if (typeof Intl !== "undefined" && Intl.DateTimeFormat) { const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone; if (typeof timeZone === "string") return timeZone; } return "UTC"; } function hasOwn(o: object, s: PropertyKey): boolean { return Object.prototype.hasOwnProperty.call(o, s); }