import type { IStore } from './store'; import type { Formatter, FormatterRegistry } from './utils'; import type { IlingoOptions } from './options'; export type PluralCategory = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other'; /** * CLDR-categorized translation options that live inside a [[PluralNode]] * — the `{ other, zero?, one?, two?, few?, many? }` shape carried as the * `data` of a `{ type: 'plural' }` node. `other` is required; every other * category is optional. Used by `Ilingo.selectPluralForm()` to pick a * string for a given `count` via `Intl.PluralRules`. */ export type PluralForms = { other: string; } & Partial, string>>; /** * A plural leaf at a catalog key position — a tagged `{ type: 'plural' }` * node carrying the CLDR-categorised [[PluralForms]]. The `type` * discriminator disambiguates a plural from a regular nested translations object * whose keys happen to be CLDR-category names. It is the only recognised * plural form: build it with `definePlural()`, or (in JSON, which can't * call functions) write the literal `{ "type": "plural", "data": { ... } }`. */ export type PluralNode = { type: 'plural'; data: PluralForms; }; /** * Value returned by `IStore.get` after the plural node has been unwrapped * — a plain string or the inner [[PluralForms]] shape the orchestrator can * index by CLDR category. Distinct from the catalog leaf shape * `string | PluralNode`, where plurals are still wrapped in their node. */ export type Leaf = string | PluralForms; export type ValueOrNestedValue = { [key: string]: ValueOrNestedValue | T; }; /** * The leaf content of a namespace — the normalized internal shape a store * holds after a catalog tree has been reduced. At a leaf position the * value is `string | PluralNode`; a nested object extends the dotted * **key** path (`{ nav: { home: 'Home' } }` → key `nav.home`). */ export type Translations = ValueOrNestedValue; export type Namespaces = Record; export type Locales = Record; /** Terminal leaf-group: a flat or key-nested map of translations. */ export type TranslationsNode = { type: 'translations'; data: Translations; }; /** A named child of a locale / namespace — a sub-namespace or a translations group. */ export type NamespaceChild = NamespaceNode | TranslationsNode; /** A (possibly nested) namespace. Nesting extends the dotted namespace path. */ export type NamespaceNode = { type: 'namespace'; name: string; data: NamespaceChild[]; }; /** * One locale's content. A `TranslationsNode` placed directly here (no enclosing * namespace) is routed to the default namespace (`''`) by `normalizeCatalog` * — the seam for a future optional-namespace API, whose ergonomics are still * provisional. */ export type LocaleNode = { type: 'locale'; name: string; data: NamespaceChild[]; }; /** Root of a catalog tree. */ export type CatalogNode = { type: 'catalog'; data: LocaleNode[]; }; /** * Accepted input for `MemoryStore({ data })` / `normalizeCatalog`: a full * catalog node, a bare array of locale nodes, or a single locale node. */ export type CatalogInput = CatalogNode | LocaleNode[] | LocaleNode; /** * Body returned by a store that already knows its `(locale, namespace)` — * a `LoaderStore` loader return value or an `@ilingo/fs` file. Reduced by * `normalizeNamespaceBody`. */ export type NamespaceBodyInput = TranslationsNode; export type DotKey = `${string}.${string}`; export type Data = Record; export type GetContext = { data?: Data; locale?: string; namespace: string; key: string; count?: number; }; export type MissingKeyContext = GetContext & { /** * The last locale that was tried — the end of the resolved fallback chain. */ resolvedLocale?: string; }; export type MissingKeyHandler = (context: MissingKeyContext) => string | undefined; export type FallbackResolver = (locale: string) => string[]; /** * - `string` — single fallback locale, applied after the requested one. * - `string[]` — ordered fallback locales. An *empty* array opts out * of fallback entirely (chain is just `[locale]`). * - `FallbackResolver` — computed per call. Returning `[]` also opts out for * that call. * - `false` — disable fallback entirely; equivalent to `[]` but * type-safe to spell out the intent. * * If none of the explicit forms above are given (i.e. `undefined`), the chain * is derived from BCP-47 parents of the requested locale and terminates at * the default locale. */ export type Fallback = string | string[] | FallbackResolver | false; /** * Public contract of the {@link Ilingo} orchestrator. Higher-layer * packages (`@ilingo/vue`, `@ilingo/vuelidate`, `@ilingo/validup`, …) * accept and return `IIlingo` so consumers can swap in alternative * implementations (test doubles, decorators) without depending on the * concrete class. */ export interface IIlingo { readonly stores: Map; formatters: FormatterRegistry; registerStore(store: IStore): void; registerFormatter(name: string, formatter: Formatter): void; clone(overrides?: IlingoOptions): IIlingo; merge(instance: IIlingo): void; setLocale(key: string): void; resetLocale(): void; getLocale(): string; getLocales(): Promise; getResolvedLocaleChain(ctx: Pick): string[]; getResolvedLocale(ctx: GetContext): Promise; get(ctx: GetContext): Promise; format(input: string, data: Record, locale?: string): string; }