export type ModeTypes = "local" | "file"; export type Dict = Record; export type FileValueBase = { /** * - Regex string (will be compiled into RegExp). */ $pattern: string; /** * - Helper function name associated with this entry. */ $fn: string; /** * - Additional key-value pairs for interpolation or extra metadata. */ key?: string; }; export type FileValue = FileValueBase & { [key: string]: string; }; export type LocaleCode = string; export type TranslationValue = string | { $fn: string; args?: any; }; export type TranslateResult = string | null; export type PatternEntry = { /** * - Compiled regular expression used for matching. */ $pattern: RegExp; /** * - Translation value or resolver function associated with the pattern. */ value: TranslationValue; /** * - Translation value or resolver function associated with the pattern. */ elseValue: TranslationValue; }; export type TinyI18Options = { /** * - The operating mode of the instance. */ mode: ModeTypes; /** * - The fallback locale used when a key is missing. */ defaultLocale: LocaleCode; /** * - Required in file mode. Directory containing .json files. */ basePath?: string; /** * - Optional initial map { locale: dict } for local mode. */ localResources?: Dict; /** * - If true, throws on missing keys or invalid data; else returns fallback. */ strict?: boolean; /** * - If true, returns null instead of the key for missing translations. */ acceptNullResults?: boolean; }; export type ResolveOptions = { /** * - Force resolution using a specific locale first. */ locale?: LocaleCode; }; export type StatLocale = { /** * - The locale code (e.g., "en", "pt-BR"). */ locale: string; /** * - Total number of static string entries in this locale. */ strings: number; /** * - Total number of regex-based pattern entries in this locale. */ patterns: number; /** * - Whether this locale is the default fallback. */ isDefault: boolean; /** * - Whether this locale is currently loaded/active. */ isCurrent: boolean; }; export type HelpersReadonly = { /** * - Check if a helper with given name is registered. */ has?: (name: string) => boolean; /** * - * Invoke a helper by name with an argument and optional extras. */ call?: (name: string, arg: T, extras?: HelpersReadonly) => R; }; export type HelperCallback = (params: Dict, helpers: HelpersReadonly) => string; /** * Supported operating modes for TinyI18. * - `"local"` → All translations are managed entirely in memory (browser + Node.js). * - `"file"` → Translations are loaded from JSON files on disk (Node.js only). * @typedef {"local" | "file"} ModeTypes */ /** * Dictionary of translation keys mapped to values. * * @typedef {Record} Dict - A collection of key-value pairs for translations. */ /** * Represents a translation entry as it appears in JSON files. * * @typedef {Object} FileValueBase * @property {string} $pattern - Regex string (will be compiled into RegExp). * @property {string} $fn - Helper function name associated with this entry. * @property {string} [key] - Additional key-value pairs for interpolation or extra metadata. */ /** * Represents a translation entry as it appears in JSON files, extending * the base pattern/function fields with arbitrary string key-value pairs. * * @typedef {FileValueBase & { [key: string]: string }} FileValue */ /** * Represents a valid locale code string. * Example: "en", "pt-BR", "fr". * * @typedef {string} LocaleCode - A string representing a valid locale identifier. */ /** * Value types stored per locale. * * @typedef {string|{ $fn: string; args?: any }} TranslationValue - The type of a value used in a translation entry. */ /** * A resolved translation value. * * @typedef {string|null} TranslateResult - A resolved translation value, typically a string or null. */ /** * Represents a single regex-based translation entry. * * @typedef {Object} PatternEntry - A representation of a translation entry that matches via a regular expression. * @property {RegExp} $pattern - Compiled regular expression used for matching. * @property {TranslationValue} value - Translation value or resolver function associated with the pattern. * @property {TranslationValue} elseValue - Translation value or resolver function associated with the pattern. */ /** * Configuration options for the TinyI18 instance. * * @typedef {Object} TinyI18Options - Configuration options for the TinyI18 constructor. * @property {ModeTypes} mode - The operating mode of the instance. * @property {LocaleCode} defaultLocale - The fallback locale used when a key is missing. * @property {string} [basePath] - Required in file mode. Directory containing .json files. * @property {Dict} [localResources] - Optional initial map { locale: dict } for local mode. * @property {boolean} [strict=true] - If true, throws on missing keys or invalid data; else returns fallback. * @property {boolean} [acceptNullResults=false] - If true, returns null instead of the key for missing translations. */ /** * Options used to customize the translation resolution process. * * @typedef {Object} ResolveOptions - Options to control the translation resolution process. * @property {LocaleCode} [locale] - Force resolution using a specific locale first. */ /** * Statistics for a specific locale. * * @typedef {Object} StatLocale - Information about the contents and status of a specific locale. * @property {string} locale - The locale code (e.g., "en", "pt-BR"). * @property {number} strings - Total number of static string entries in this locale. * @property {number} patterns - Total number of regex-based pattern entries in this locale. * @property {boolean} isDefault - Whether this locale is the default fallback. * @property {boolean} isCurrent - Whether this locale is currently loaded/active. */ /** * Read-only view of registered helpers, exposed to function-based entries. * * Provides safe access to: * - Check if a helper exists by name. * - Call a helper by name, passing arguments. * * @template {any} T * @template {any} R * @typedef {Object} HelpersReadonly - A read-only interface for interacting with registered helpers. * @property {(name: string) => boolean} [has] - Check if a helper with given name is registered. * @property {(name: string, arg: T, extras?: HelpersReadonly) => R} [call] - * Invoke a helper by name with an argument and optional extras. */ /** * A helper callback function used in translations. * * - Receives user-supplied parameters and a read-only facade for calling other helpers. * - Must return a string (e.g. processed, interpolated, or formatted output). * * @template {any} T * @template {any} R * @callback HelperCallback * @param {Dict} params - Key-value parameters passed from the translation entry. * @param {HelpersReadonly} helpers - Read-only access to other registered helpers. * @returns {string} Processed string result. */ /** * TinyI18 — Professional and flexible i18n manager with dual mode (local/file), * regex-based keys, and function-based entries for advanced rendering (incl. HTML). * * - Mode "local": in-memory resources (Node + Browser). * - Mode "file": JSON files on disk via fs/path (Node only). * - Keeps only default + selected locale in memory. * - Selected locale overrides default; fallback resolves to default. * - Supports string entries, regex pattern entries, and function-backed entries. * - Safe: no dynamic code eval from files; functions in file mode are referenced by name ("$fn"). */ declare class TinyI18 { #private; /** * Sets the path join function used to resolve locale file paths. * Throws if a non-function value is provided or if the function has already been set. * @param {(...paths: string[]) => string} value - The path join function to cache. */ static set _join(value: (...paths: string[]) => string); /** * Sets the file existence check function. * Throws if a non-function value is provided or if the function has already been set. * @param {((path: string) => boolean)|false} value - The existence check function, or false to disable. */ static set _existsSync(value: ((path: string) => boolean) | false); /** * Sets the asynchronous file read function. * Throws if a non-function value is provided or if the function has already been set. * @param {(path: string, options: BufferEncoding) => Promise} value - The file read function to cache. */ static set _readFile(value: (path: string, options: BufferEncoding) => Promise); /** * Sets the asynchronous file write function. * Throws if a non-function value is provided or if the function has already been set. * @param {(path: string, data: string, options: BufferEncoding) => Promise} value - The file write function to cache. */ static set _writeFile(value: (path: string, data: string, options: BufferEncoding) => Promise); /** * Merges multiple JSON locale files into a single file for TinyI18 usage. * * @param {Object} options - Configuration for the merge operation. * @param {string[]} options.files - List of JSON file paths to merge. * @param {string} options.output - Path where the merged JSON file will be written. * @param {number} [options.spaces=0] - Number of spaces to use for indentation in the output file. * @throws {TypeError} If arguments are invalid. * @throws {Error} If file reading or writing fails. */ static mergeLocaleFiles({ files, output, spaces }: { files: string[]; output: string; spaces?: number; }): Promise; /** * Gets the current destruction status of the instance. * * @returns {boolean} True if the instance is destroyed, false otherwise. */ get destroyed(): boolean; /** * Gets the currently selected locale, or null if only default is active. * @returns {LocaleCode|null} The currently active locale code, or null. */ get currentLocale(): LocaleCode | null; /** * The default locale code chosen at construction time. * This locale is always kept in memory as a fallback. * @type {LocaleCode} */ get defaultLocale(): LocaleCode; /** * The current operating mode of this instance. * Determines whether translations are managed in memory ("local") * or loaded from JSON files ("file"). * @type {ModeTypes} */ get mode(): ModeTypes; /** * Whether strict mode is enabled. * - `true` → Missing keys, invalid regex, or helper errors throw exceptions. * - `false` → Failures are ignored silently, returning fallback values. * @type {boolean} */ get strict(): boolean; /** * Whether null results are accepted when a translation is missing. * @type {boolean} */ get acceptNullResults(): boolean; /** * Base directory path used in `"file"` mode to locate locale JSON files. * - In `"local"` mode this will always be `null`. * - In `"file"` mode this is the root folder passed to the constructor. * @type {string|null} */ get basePath(): string | null; /** * Returns basic stats for debugging/memory insights. * @returns {StatLocale[]} Array of statistics for each registered locale. */ get stats(): StatLocale[]; /** * Deep-cloned view of string tables (Map → Object). * Preserves strings, $fn objects, and functions. * @returns {Record} A deep clone of the string tables. */ get stringTables(): Record; /** * Deep-cloned view of pattern tables (Map → Object). * Recreates RegExp objects to avoid mutation. * @returns {Record} A deep clone of the pattern tables. */ get patternTables(): Record; /** * Deep-cloned view of helpers (Map → Object). * Functions are referenced (cannot deep clone functions). * @returns {Record>} A copy of the helpers registry. */ get helpers(): Record>; /** * Deep-cloned view of regex cache (Map → Object). * Recreates RegExp objects to avoid mutation. * @returns {Record} A copy of the regex cache. */ get regexCache(): Record; /** * Creates a new TinyI18 instance for managing localized strings and patterns. * * Supports two modes: * - "local": loads translations directly from provided objects. * - "file": loads translations from JSON files on demand. * * Ensures the default locale is always initialized. In "file" mode, `basePath` is required. * * @param {TinyI18Options} options - Configuration options for the instance. */ constructor(options: TinyI18Options); /** * Clears the internal regex cache. * * The regex cache stores compiled {@link RegExp} objects to avoid * recompiling frequently used patterns. This wrapper ensures cache * management is always controlled via the API instead of direct access. */ clearRegexCache(): void; /** * Registers a helper function available to function-based entries and $fn references. * @template {any} T * @template {any} R * @param {string} name - The unique name of the helper. * @param {HelperCallback} fn - The callback function to register. */ registerHelper(name: string, fn: HelperCallback): void; /** * Unregisters a previously registered helper function. * * If the helper does not exist, this method silently does nothing. * * @param {string} name - The name of the helper to remove. * @returns {boolean} `true` if the helper was removed, `false` if it was not found. */ unregisterHelper(name: string): boolean; /** * Loads or updates a locale data in-memory (local mode only). * @param {LocaleCode} locale - The locale to load. * @param {Dict} data - The dictionary data for the locale. */ loadLocaleLocal(locale: LocaleCode, data: Dict): void; /** * Sets the current selected locale. In file mode, loads it from disk. * Keeps only the default and the selected locale in memory (unloads previous selected). * @param {LocaleCode|null} locale - The locale to set, or null to use only the default. * @returns {Promise} A promise that resolves when the locale has been set and loaded. */ setLocale(locale: LocaleCode | null): Promise; /** * Resolves a translation by exact key. * * Resolution order: * 1. Current locale (if set) * 2. Default locale (fallback) * * @param {string} key - Translation key (dot.notation). * @param {Dict} [params] - Parameters for string interpolation or helper functions. * @param {ResolveOptions} [options] - Override resolution options (e.g., force locale). * @returns {TranslateResult} - Usually string, but may be HTMLElement, DocumentFragment, or any return type from a helper. */ t(key: string, params?: Dict, options?: ResolveOptions): TranslateResult; /** * Alias of t() * @param {string} key - Translation key (dot.notation). * @param {Dict} [params] - Parameters for string interpolation or helper functions. * @param {ResolveOptions} [options] - Override resolution options (e.g., force locale). * @returns {TranslateResult} - Usually string, but may be HTMLElement, DocumentFragment, or any return type from a helper. */ get(key: string, params?: Dict, options?: ResolveOptions): TranslateResult; /** * Resolves a translation by regex pattern match. * * If multiple patterns exist, returns the first matching entry. * * @param {string} key - Input string to test against regex patterns. * @param {ResolveOptions} [options] - Override resolution options (e.g., force locale). * @returns {TranslateResult} - Translation value (string or custom return type). */ p(key: string, options?: ResolveOptions): TranslateResult; /** * Alias of p() * @param {string} key - Input string to test against regex patterns. * @param {ResolveOptions} [options] - Override resolution options (e.g., force locale). * @returns {TranslateResult} - Translation value (string or custom return type). */ resolveByPattern(key: string, options?: ResolveOptions): TranslateResult; /** * Clears everything except the default locale (keeps its data). * Selected locale becomes null. */ resetToDefaultOnly(): void; /** * Returns stats for a specific locale. * @param {LocaleCode} locale - The locale code to query. * @returns {StatLocale} An object containing the locale's current statistics. * @throws {Error} If the locale is not registered. */ getStatsForLocale(locale: LocaleCode): StatLocale; /** * Destroys the TinyI18 instance, clearing all internal maps and references * to free up memory. Once destroyed, the instance cannot be reused. * * @throws {Error} If the instance has already been destroyed. * @returns {void} This method does not return a value. */ destroy(): void; } export default TinyI18; //# sourceMappingURL=index.d.mts.map