declare type Alpha = AlphaLower | Uppercase; declare type AlphaLower = CharactersOf<'abcdefghijklmnopqrstuvwxyz'>; /** Extractor for ``. */ export declare interface AngleBracketExtractor extends PlaceholderExtractor { readonly placeholders: ExtractAngleBracketPlaceholders; } /** Applies a {@link PlaceholderExtractor} to a concrete translation string. */ export declare type ApplyPlaceholderExtractor = (Extractor & { readonly template: S; })['placeholders']; /** * Placeholder parameter inference. * ================================ * * Turns a translation string with placeholders into a typed parameters object. * Extractors live in {@link ./placeholders-extractors} — reuse a built-in or * plug in your own via {@link PlaceholderExtractor}. * * Nothing here is enforced at runtime: replacement happens in the tag * (`processPlaceholders` / your `transform`). These types only drive * autocomplete and compile-time checks. */ /** * Controls how strictly inferred placeholder parameters are enforced via two * independent booleans: * * - **`required`**: `false` — placeholders may be omitted; `true` — all must be provided. * - **`allowExtras`**: `true` — additional, non-inferred keys are accepted; `false` — only known placeholders. * * | `required` | `allowExtras` | Placeholders | Extra keys | Notes | * | ---------- | ------------- | ------------ | ---------- | ---------------------------------- | * | `false` | `true` | optional | allowed | Default. Autocomplete, permissive. | * | `false` | `false` | optional | rejected | Autocomplete, only placeholders. | * | `true` | `true` | required | allowed | Must provide placeholders; extras ok. | * | `true` | `false` | required | rejected | Strictest: exactly the placeholders. | * * Strings without placeholders always fall back to {@link InterpolationParams} * (permissive) regardless of these flags, since there is nothing to infer or enforce. */ /** Builds the placeholder-values record for the given axes. */ declare type BuildPlaceholderValues = Required extends true ? AllowExtras extends true ? Record & Record : Record : AllowExtras extends true ? Partial> & Record : Partial>; /** * Transforms a static translation object into an object where each * translation string or nested object is converted into a callable function * or a nested structure of callable functions. * * String leaves have their parameters inferred from template placeholders * (`{{name}}` by default), enabling autocomplete. How those parameters behave — * `required` / `allowExtras`, accepted value type and placeholder syntax — is configured via * the {@link PlaceholderParamsOptions} `Params` bundle (see `./placeholder-params`), * which defaults to the built-in behaviour and can be overridden per tag without * patching core. * @template T - The structure of the input translations. * @template PPO - The {@link PlaceholderParamsOptions} bundle for inferred parameters. Defaults to all defaults. */ export declare type CallableTranslations = { [P in keyof T]: NonNullable extends ParameterizedTranslation ? ParameterizedTranslation : NonNullable extends (...args: any[]) => string ? NonNullable : NonNullable extends string ? ResolvedPlaceholderTranslation, PPO> : NonNullable extends Record ? CallableTranslations, PPO> : ParameterizedTranslation; }; /** * Character union from a string literal (type-level). * @example CharactersOf<'ab'> // 'a' | 'b' */ declare type CharactersOf = S extends `${infer First}${infer Rest}` ? First | CharactersOf : never; /** Extractor for `:name`. */ export declare interface ColonExtractor extends PlaceholderExtractor { readonly placeholders: ExtractColonPlaceholders; } /** * Creates a callable translations object from a static translations object. * This function initializes the transformation process. * @template T - The type of the input translations object. * @template Config - The LangTag translations configuration type. * @param translations - The static translations object. * @param config - The LangTag configuration object. * @param strategy - The translation mapping strategy. * @returns A callable translations object. */ export declare function createCallableTranslations(translations: T, config: Config | undefined, strategy: TranslationMappingStrategy): CallableTranslations; /** * Identity helper that keeps the narrow literals you write while constraining keys * to {@link PlaceholderParamsOptions}, so editors suggest `required`, `allowExtras`, * `value` and `extractor` as you type. * * @example * type PlaceholderParams = DefinePlaceholderParams<{ * required: false; * allowExtras: true; * }>; */ export declare type DefinePlaceholderParams = O; /** Extractor for `${ name }`. */ export declare interface DollarBraceExtractor extends PlaceholderExtractor { readonly placeholders: ExtractDollarBracePlaceholders; } /** Extractor for `$name` (not `${ name }`). */ export declare interface DollarIdentExtractor extends PlaceholderExtractor { readonly placeholders: ExtractDollarIdentPlaceholders; } /** Default extractor: `{{ name }}`. */ export declare interface DoubleBraceExtractor extends PlaceholderExtractor { readonly placeholders: ExtractDoubleBracePlaceholders; } /** Extractor for `[[ name ]]`. */ export declare interface DoubleSquareExtractor extends PlaceholderExtractor { readonly placeholders: ExtractDoubleSquarePlaceholders; } /** * `` — simple angle-bracket placeholders (not HTML tags with `/` or spaces). * @example ExtractAngleBracketPlaceholders<'Hello '> // 'name' */ export declare type ExtractAngleBracketPlaceholders = S extends `${string}<${infer Placeholder}>${infer Rest}` ? Placeholder extends `/${string}` | `${string} ${string}` | `${string}/${string}` ? ExtractAngleBracketPlaceholders : Trim | ExtractAngleBracketPlaceholders : never; /** * `:name` — Express / route-style placeholders. * Name shape: `^[a-zA-Z_][a-zA-Z0-9_-]*$` (ASCII allow-list). * @example ExtractColonPlaceholders<'Hello :name!'> // 'name' * @example ExtractColonPlaceholders<'Hi :user_id / :order-id'> // 'user_id' | 'order-id' */ export declare type ExtractColonPlaceholders = S extends `${string}:${infer Rest}` ? TakeIdent extends infer Name extends string ? Name extends '' ? ExtractColonPlaceholders : Rest extends `${Name}${infer After}` ? Name | ExtractColonPlaceholders : Name : never : never; /** * `${ name }` — JS template-literal style. * @example ExtractDollarBracePlaceholders<'Hello ${name}'> // 'name' */ export declare type ExtractDollarBracePlaceholders = S extends `${string}${'${'}${infer Placeholder}}${infer Rest}` ? Trim | ExtractDollarBracePlaceholders : never; /** * `$name` — dollar-prefixed identifiers (not `${ name }`). * Name shape: `^[a-zA-Z_][a-zA-Z0-9_-]*$` (ASCII allow-list). * @example ExtractDollarIdentPlaceholders<'Hello $name'> // 'name' * @example ExtractDollarIdentPlaceholders<'Hi $user_id'> // 'user_id' */ export declare type ExtractDollarIdentPlaceholders = S extends `${string}$${infer Rest}` ? Rest extends `{${string}` ? ExtractDollarIdentPlaceholders : TakeIdent extends infer Name extends string ? Name extends '' ? ExtractDollarIdentPlaceholders : Rest extends `${Name}${infer After}` ? Name | ExtractDollarIdentPlaceholders : Name : never : never; /** * `{{ name }}` — Handlebars / i18next-style (lang-tag default). * @example ExtractDoubleBracePlaceholders<'Hello {{name}} from {{ sender }}'> // 'name' | 'sender' */ export declare type ExtractDoubleBracePlaceholders = S extends `${string}{{${infer Placeholder}}}${infer Rest}` ? Trim | ExtractDoubleBracePlaceholders : never; /** * `[[ name ]]` — double square brackets. * @example ExtractDoubleSquarePlaceholders<'Hello [[name]]'> // 'name' */ export declare type ExtractDoubleSquarePlaceholders = S extends `${string}[[${infer Placeholder}]]${infer Rest}` ? Trim | ExtractDoubleSquarePlaceholders : never; /** * `%{ name }` — Ruby / some i18n libs. * @example ExtractPercentBracePlaceholders<'Hello %{name}'> // 'name' */ export declare type ExtractPercentBracePlaceholders = S extends `${string}%{${infer Placeholder}}${infer Rest}` ? Trim | ExtractPercentBracePlaceholders : never; /** * `%name%` — percent-wrapped names. * @example ExtractPercentPercentPlaceholders<'Hello %name%'> // 'name' */ export declare type ExtractPercentPercentPlaceholders = S extends `${string}%${infer Placeholder}%${infer Rest}` ? Placeholder extends `{${string}` ? ExtractPercentPercentPlaceholders<`${Placeholder}%${Rest}`> : Trim | ExtractPercentPercentPlaceholders : never; /** * `{ name }` — single curly braces (Python `str.format`, many i18n libs). * Strips `{{ ... }}` pairs first so they are not misread as single-brace names. * @example ExtractSingleBracePlaceholders<'Hi {name}, ignore {{raw}}'> // 'name' */ export declare type ExtractSingleBracePlaceholders = S extends `${infer Head}{{${string}}}${infer Tail}` ? ExtractSingleBracePlaceholders<`${Head}${Tail}`> : S extends `${string}{${infer Placeholder}}${infer Rest}` ? Trim | ExtractSingleBracePlaceholders : never; /** * `[ name ]` — single square brackets. * Strips `[[ ... ]]` pairs first so they are not misread as single-bracket names. * @example ExtractSingleSquarePlaceholders<'Hi [name], ignore [[raw]]'> // 'name' */ export declare type ExtractSingleSquarePlaceholders = S extends `${infer Head}[[${string}]]${infer Tail}` ? ExtractSingleSquarePlaceholders<`${Head}${Tail}`> : S extends `${string}[${infer Placeholder}]${infer Rest}` ? Trim | ExtractSingleSquarePlaceholders : never; /** * Core type for flexible translations, allowing properties to be optional recursively. * This type serves as the foundation for `FlexibleTranslations` and `PartialFlexibleTranslations`. * It transforms a given translation structure `T` into a flexible version where each property * can be its original type, a string, or a `ParameterizedTranslation` function. * If `IsPartial` is true, all properties at all levels of nesting become optional. * * Every node carries an optional {@link LangTagSpecialBrand}, so a recursive * dynamic caller on a weak named object (`delete: {}` → `{ [caller]: Special }`) * assigns regardless of `callerName`. String-index (`Record`) schemas * accept {@link LangTagSpecialFn} on the index **and** as the node itself * (a callable `t` is a function–object; TS will not assign that to a * `Record` index unless the source also has one). * * @template T The original, un-transformed, structure of the translations. * @template IsPartial A boolean indicating whether properties should be optional. * If true, all properties at all levels become optional (e.g., `string | undefined`). * If false, properties are required (e.g., `string`). */ declare type FlexibleObject = IsPartial extends true ? { [P in keyof T]?: FlexibleValue | (string extends keyof T ? LangTagSpecialFn : never); } : { [P in keyof T]: FlexibleValue | (string extends keyof T ? LangTagSpecialFn : never); }; /** * Represents a flexible structure for translations where all properties are required, based on an original type `T`. * Allows for strings, `ParameterizedTranslation` functions, or other compatible functions * at any level of the translation object. This provides flexibility in how translations * are initially defined. * This type is an alias for `RecursiveFlexibleTranslations`. * @template T - The original structure of the translations. */ export declare type FlexibleTranslations = RecursiveFlexibleTranslations; /** * Helper type to determine the flexible value of a translation property. * If `T` is a function returning a string, it can be `T` or `string`. * If `T` is a record, it recursively applies `RecursiveFlexibleTranslations`. * Otherwise, it can be `ParameterizedTranslation`, `T`, or `string`. * @template T - The type of the property value. * @template IsPartial - A boolean indicating whether properties should be optional. */ declare type FlexibleValue = T extends (...args: any[]) => string ? T | string : T extends Record ? RecursiveFlexibleTranslations : ParameterizedTranslation | T | string; /** * Continuation characters: `^[a-zA-Z0-9_-]*` after {@link IdentStart}. * Full shape: `^[a-zA-Z_][a-zA-Z0-9_-]*$`. */ declare type IdentContinue = IdentStart | Numeric | '-'; /** * First character of a `:name` / `$name` identifier: `^[a-zA-Z_]`. * Deliberate allow-list (ASCII) — not a deny-list of separators. */ declare type IdentStart = Alpha | '_'; /** * Defines the structure for parameters used in interpolation. * It's a record where keys are placeholders and values are their replacements. */ export declare type InterpolationParams = Record; export declare function isLangTagSpecial(value: unknown): value is LangTagSpecialFn; /** * Represents a collection of optional translations. * Keys are strings, and values can be either strings (translations) * or nested LangTagTranslations objects for hierarchical translations. */ export declare type LangTagOptionalTranslations = { [key in string]?: string | LangTagOptionalTranslations; }; /** * Lang-tag specials. * ================= * * {@link LangTagSpecial} brands a value with a {@link LangTagSpecialKind}. * Functions (`$`, a custom `callerName`, a callable `t('key')` object, * future `plural` / `count`) are not translation leaves — Record * `InputType`s accept them and reject plain extra functions. * * The same brand on a translations object is the overlap for a weak named * node (`delete: {}` → `{ [caller]: Special }`) so it assigns into * `PartialFlexibleTranslations` regardless of the special's property name. */ export declare const LangTagSpecial: unique symbol; /** * A value branded with {@link LangTagSpecial} and a {@link LangTagSpecialKind}. */ export declare type LangTagSpecialBrand = { readonly [LangTagSpecial]: Kind; }; /** * A function branded with {@link LangTagSpecial}. * @template Kind - Discriminant stored on the symbol (e.g. `'dynamic-caller'`). * @template Fn - The underlying call signature. */ export declare type LangTagSpecialFn any = (...args: never[]) => string> = Fn & LangTagSpecialBrand; /** * Well-known kinds. The type is open (`string & {}`) so a preset can * introduce a new kind without a core release. */ export declare type LangTagSpecialKind = 'dynamic-caller' | (string & {}); /** * Represents a collection of translations. * Keys are strings, and values can be either strings (translations) * or nested LangTagTranslations objects for hierarchical translations. */ export declare type LangTagTranslations = { [key: string]: string | LangTagTranslations; }; /** * Configuration for LangTag translations. * @template Namespaces - The type used for namespaces, defaults to string. */ export declare interface LangTagTranslationsConfig { /** Optional base path for translation keys. */ path?: string; /** The namespace for the translations. */ namespace?: Namespaces; } /** * Retrieves a translation function from a nested translation object. * Prefer passing path segments from `unprefixedPath` (a `string[]`) so keys that * contain `.` are not incorrectly split. A dotted string is still accepted for * backward compatibility, but cannot represent keys that themselves contain `.`. * @template T - The type of the translations object. * @param translations The object containing translation functions. * @param path Unprefixed path segments, or a dotted string (e.g. `"user.profile.greeting"`). * @returns The translation function, or null if not found or invalid. */ export declare function lookupTranslation(translations: CallableTranslations, path: string | string[]): ParameterizedTranslation | null; /** * Brands `value` as a lang-tag special. The symbol is non-enumerable. * Works on functions (callers) and on objects that carry them. */ export declare function markLangTagSpecial(value: T, kind: Kind): T & LangTagSpecialBrand; /** * Normalizes a `FlexibleTranslations` or `PartialFlexibleTranslations` object into a `CallableTranslations` object. * Converts plain strings into `ParameterizedTranslation` functions and ensures * that all callable elements conform to the `ParameterizedTranslation` signature. * Only properties present in the input `translations` object will be processed and included in the result. * @template T - The structure of the original translations. * @param translations - The flexible or partial flexible translations object to normalize. * @returns A `CallableTranslations` object. The returned object will only contain callable translations for properties that were present in the input `translations` object. */ export declare function normalizeTranslations(translations: RecursiveFlexibleTranslations): CallableTranslations; declare type Numeric = CharactersOf<'0123456789'>; /** * Represents a function that takes optional interpolation parameters * and returns a translated string. * @template P - The shape of the accepted interpolation parameters. * Defaults to {@link InterpolationParams}; can be narrowed via the generic * (typically inferred from a translation string through `PlaceholderValues`). */ export declare type ParameterizedTranslation

= InterpolationParams> = (params?: P) => string; /** * Represents a deeply partial version of the structure that `FlexibleTranslations` would produce, based on an original type `T`. * All properties at all levels of nesting are made optional. * The transformation rules for property types mirror those in `FlexibleTranslations`. * This type is an alias for `RecursiveFlexibleTranslations`. * @template T - The original, un-transformed, structure of the translations. This is the same kind of type argument that `FlexibleTranslations` expects. */ export declare type PartialFlexibleTranslations = RecursiveFlexibleTranslations; /** Extractor for `%{ name }`. */ export declare interface PercentBraceExtractor extends PlaceholderExtractor { readonly placeholders: ExtractPercentBracePlaceholders; } /** Extractor for `%name%`. */ export declare interface PercentPercentExtractor extends PlaceholderExtractor { readonly placeholders: ExtractPercentPercentPlaceholders; } /** * Higher-kinded interface describing a placeholder extractor. * * An implementation extends this interface and computes * {@link PlaceholderExtractor.placeholders} from * {@link PlaceholderExtractor.template}. The template is injected by * {@link ApplyPlaceholderExtractor}; implementations read it via * `this['template']`. */ export declare interface PlaceholderExtractor { /** The translation string to analyse (injected by {@link ApplyPlaceholderExtractor}). */ readonly template: string; /** The resulting union of placeholder names. */ readonly placeholders: string; } /** * Bundles the options that control how placeholder parameters are inferred for a * translations tree. Grouping them keeps {@link CallableTranslations} at two type * arguments (``) and leaves room for future options without changing arity. * Every field is optional and falls back to the default noted below. * * Prefer building concrete bundles through {@link DefinePlaceholderParams} so editors * autocomplete the option keys. * * @example * type PlaceholderParams = DefinePlaceholderParams<{ * required: false; * allowExtras: true; * }>; */ export declare interface PlaceholderParamsOptions { /** * Whether all inferred placeholders must be provided. * Defaults to `false` (placeholders may be omitted). */ required?: boolean; /** * Whether additional, non-inferred keys are accepted on the params object. * Defaults to `true` (extras allowed). */ allowExtras?: boolean; /** Value type accepted for each placeholder. Defaults to `any`. */ value?: unknown; /** Placeholder extractor. See {@link PlaceholderExtractor}. Defaults to {@link DoubleBraceExtractor} (`{{...}}`). */ extractor?: PlaceholderExtractor; } /** * Builds the callable translation function type for a single translation-string leaf, * inferring its parameters from placeholders and applying the `required` / `allowExtras` axes. * When `Required` is `true` the parameters argument is mandatory; otherwise it is optional. * @template S - The literal translation string. * @template Required - Whether all placeholders must be provided. Defaults to `false`. * @template AllowExtras - Whether extra, non-inferred keys are accepted. Defaults to `true`. * @template Value - The value type accepted for each placeholder. Defaults to `any`. * @template Extractor - The placeholder extractor. Defaults to {@link DoubleBraceExtractor} (`{{...}}`). */ export declare type PlaceholderTranslation = [ApplyPlaceholderExtractor] extends [never] ? ParameterizedTranslation : Required extends true ? (params: PlaceholderValues) => string : (params?: PlaceholderValues) => string; /** * Derives the placeholder-values object shape from a translation string, according to * the `required` / `allowExtras` axes and a {@link PlaceholderExtractor}. * When the string has no placeholders it falls back to {@link InterpolationParams}. * @template S - The literal translation string. * @template Required - Whether all placeholders must be provided. Defaults to `false`. * @template AllowExtras - Whether extra, non-inferred keys are accepted. Defaults to `true`. * @template Value - The value type accepted for each placeholder. Defaults to `any`. * @template Extractor - The placeholder extractor. Defaults to {@link DoubleBraceExtractor} (`{{...}}`). */ export declare type PlaceholderValues = [ApplyPlaceholderExtractor] extends [never] ? InterpolationParams : BuildPlaceholderValues, Value, Required, AllowExtras>; export declare type RecursiveFlexibleTranslations = string extends keyof T ? (FlexibleObject & Partial) | LangTagSpecialFn : FlexibleObject & Partial; declare type ResolveAllowExtras = O extends { allowExtras: infer A extends boolean; } ? A : true; /** * Builds the callable translation function for a single translation-string leaf from a * {@link PlaceholderParamsOptions} bundle, resolving `required`, `allowExtras`, value type * and extractor (each with its default). This is the option-bundle counterpart of * {@link PlaceholderTranslation}. * @template S - The literal translation string. * @template PPO - The {@link PlaceholderParamsOptions} bundle. Defaults to all defaults. */ export declare type ResolvedPlaceholderTranslation = PlaceholderTranslation, ResolveAllowExtras, ResolveValue, ResolveExtractor>; declare type ResolveExtractor = O extends { extractor: infer E extends PlaceholderExtractor; } ? E : DoubleBraceExtractor; declare type ResolveRequired = O extends { required: infer R extends boolean; } ? R : false; declare type ResolveValue = O extends { value: infer V; } ? V : any; /** Extractor for `{ name }`. */ export declare interface SingleBraceExtractor extends PlaceholderExtractor { readonly placeholders: ExtractSingleBracePlaceholders; } /** Extractor for `[ name ]`. */ export declare interface SingleSquareExtractor extends PlaceholderExtractor { readonly placeholders: ExtractSingleSquarePlaceholders; } /** * Consumes an identifier prefix from `S` matching * `^[a-zA-Z_][a-zA-Z0-9_-]*$`. Stops at the first non-matching character. */ declare type TakeIdent = Acc extends '' ? S extends `${infer C}${infer Rest}` ? C extends IdentStart ? TakeIdent : '' : '' : S extends `${infer C}${infer Rest}` ? C extends IdentContinue ? TakeIdent : Acc : Acc; /** * Defines the signature for a function that processes translation keys. * This allows for modifying or generating new keys based on the original key and value. * @template Config - The LangTag translations configuration type. */ export declare type TranslationKeyProcessor = ( /** Context for processing the key. */ context: TranslationKeyProcessorContext, /** * Callback to add a processed key. * @param newKey - The new key to be added to the result. * @param originalValue - The original string value associated with the key being processed. */ addProcessedKey: (newKey: string, originalValue: string) => void) => void; /** * Context provided to a translation key processor function. * It omits the 'params' field from `TranslationTransformContext`. * @template Config - The LangTag translations configuration type. */ export declare type TranslationKeyProcessorContext = Omit, 'params'>; /** * Defines the strategy for mapping and transforming translations. * @template Config - The LangTag translations configuration type. */ export declare interface TranslationMappingStrategy { /** The function used to transform raw translation strings. */ transform: TranslationTransformer; /** Optional function to process translation keys. */ processKey?: TranslationKeyProcessor; } /** * Context provided to a translation transformer function. * @template Config - The LangTag translations configuration type. */ export declare interface TranslationTransformContext { /** The LangTag configuration object. */ config: Config | undefined; /** The path of the direct parent object of the current translation key, including the base path from config. */ parentPath: string; /** The full path to the current translation key, including the base path from config. */ path: string; /** * Path segments to the current translation key, relative to the root of the * translations object (excluding the base path from config). * Kept as an array so object keys that contain `.` are not split during lookup. */ unprefixedPath: string[]; /** The current translation key. */ key: string; /** The raw string value of the translation. */ value: string; /** Optional interpolation parameters for the translation. */ params?: InterpolationParams; } /** * Defines the signature for a function that transforms a raw translation string. * @template Config - The LangTag translations configuration type. * @param transformContext - The context for the transformation. * @returns The transformed translation string. */ export declare type TranslationTransformer = (transformContext: TranslationTransformContext) => string; /** Removes leading/trailing whitespace from a string literal type. */ export declare type Trim = TrimLeft>; declare type TrimLeft = S extends `${Whitespace}${infer R}` ? TrimLeft : S; declare type TrimRight = S extends `${infer R}${Whitespace}` ? TrimRight : S; /** * Placeholder extractors. * ======================= * * Ready-made type-level parsers for common placeholder syntaxes, plus the * {@link PlaceholderExtractor} interface for writing your own. * * These types only drive autocomplete and compile-time checks. Runtime * replacement stays in your tag `transform` (or a preset). * * @example Reuse a built-in * ```ts * import type { DefinePlaceholderParams, PercentBraceExtractor } from 'lang-tag'; * * type PlaceholderParams = DefinePlaceholderParams<{ * extractor: PercentBraceExtractor; // `%{ name }` * }>; * ``` * * @example Write your own * ```ts * import type { PlaceholderExtractor, Trim } from 'lang-tag'; * * type ExtractBang = * S extends `${string}!${infer Name}!${infer Rest}` * ? Trim | ExtractBang * : never; * * interface BangExtractor extends PlaceholderExtractor { * placeholders: ExtractBang; * } * ``` */ /** Whitespace characters trimmed from placeholder names. */ declare type Whitespace = ' ' | '\n' | '\t' | '\r'; export { }