locl-js 1.0.2 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,438 @@
1
+ /**
2
+ * The type of the default formatters object.
3
+ */
4
+ type DefaultFormatters = typeof defaultFormatters;
5
+ /**
6
+ * A collection of default formatting functions that can be used in translations.
7
+ * @example
8
+ * ```ts
9
+ * const translator = initLangPack({
10
+ * resources: {
11
+ * en: { greeting: "Hello, {name | upper}!" }
12
+ * },
13
+ * fallbackLanguage: "en"
14
+ * });
15
+ *
16
+ * translator.t("greeting", { name: "world" }); // "Hello, WORLD!"
17
+ * ```
18
+ */
19
+ declare const defaultFormatters: {
20
+ /**
21
+ * Converts a string to uppercase.
22
+ * @param val - The value to format.
23
+ * @returns The uppercased string.
24
+ */
25
+ upper: (val: any) => string;
26
+ /**
27
+ * Converts a string to lowercase.
28
+ * @param val - The value to format.
29
+ * @returns The lowercased string.
30
+ */
31
+ lower: (val: any) => string;
32
+ /**
33
+ * Capitalizes the first letter of a string.
34
+ * @param val - The value to format.
35
+ * @returns The capitalized string.
36
+ */
37
+ capitalize: (val: any) => string;
38
+ /**
39
+ * Trims whitespace from the beginning and end of a string.
40
+ * @param val - The value to format.
41
+ * @returns The trimmed string.
42
+ */
43
+ trim: (val: any) => string;
44
+ /**
45
+ * Truncates a string to a specified length.
46
+ * @param val - The value to format.
47
+ * @param args - An array of arguments.
48
+ * @param {number} [args[0]=10] - The maximum length of the string.
49
+ * @param {string} [args[1]="..."] - The suffix to append if the string is truncated.
50
+ * @returns The truncated string.
51
+ */
52
+ truncate: (val: any, args?: string[]) => string;
53
+ /**
54
+ * Formats a number using `Intl.NumberFormat`.
55
+ * @param val - The value to format.
56
+ * @param args - An array of arguments.
57
+ * @param {string} [args[0]] - The locale to use.
58
+ * @param {string} [args[1]] - The style of formatting to use (e.g., "decimal", "percent").
59
+ * @returns The formatted number.
60
+ */
61
+ number: (val: any, args?: string[]) => string;
62
+ /**
63
+ * Formats a number as a currency string using `Intl.NumberFormat`.
64
+ * @param val - The value to format.
65
+ * @param args - An array of arguments.
66
+ * @param {string} [args[0]="USD"] - The currency code.
67
+ * @param {string} [args[1]] - The locale to use.
68
+ * @returns The formatted currency string.
69
+ */
70
+ currency: (val: any, args?: string[]) => string;
71
+ /**
72
+ * Formats a date using `Intl.DateTimeFormat`.
73
+ * @param val - The value to format.
74
+ * @param args - An array of arguments.
75
+ * @param {string} [args[0]] - The locale to use.
76
+ * @param {string} [args[1]] - The date style to use (e.g., "short", "long").
77
+ * @returns The formatted date string.
78
+ */
79
+ date: (val: any, args?: string[]) => string;
80
+ /**
81
+ * Formats a date as a relative time string (e.g., "2 hours ago").
82
+ * @param val - The value to format.
83
+ * @param args - An array of arguments.
84
+ * @param {string} [args[0]] - The locale to use.
85
+ * @returns The relative time string.
86
+ */
87
+ relativeDate: (val: any, args?: string[]) => string;
88
+ /**
89
+ * Converts a value to a JSON string.
90
+ * @param val - The value to format.
91
+ * @returns The JSON string.
92
+ */
93
+ json: (val: any) => string;
94
+ /**
95
+ * Converts a boolean value to a "Yes" or "No" string.
96
+ * @param val - The value to format.
97
+ * @param args - An array of arguments.
98
+ * @param {string} [args[0]="Yes"] - The string to return for a truthy value.
99
+ * @param {string} [args[1]="No"] - The string to return for a falsy value.
100
+ * @returns "Yes" or "No".
101
+ */
102
+ yesNo: (val: any, args?: string[]) => string;
103
+ /**
104
+ * Converts a boolean value to a "true" or "false" string.
105
+ * @param val - The value to format.
106
+ * @param args - An array of arguments.
107
+ * @param {string} [args[0]="true"] - The string to return for a truthy value.
108
+ * @param {string} [args[1]="false"] - The string to return for a falsy value.
109
+ * @returns "true" or "false".
110
+ */
111
+ boolean: (val: any, args?: string[]) => string;
112
+ /**
113
+ * Pads the start of a string with another string.
114
+ * @param val - The value to format.
115
+ * @param args - An array of arguments.
116
+ * @param {number} [args[0]=0] - The target length of the string.
117
+ * @param {string} [args[1]=" "] - The string to pad with.
118
+ * @returns The padded string.
119
+ */
120
+ padStart: (val: any, args?: string[]) => string;
121
+ /**
122
+ * Pads the end of a string with another string.
123
+ * @param val - The value to format.
124
+ * @param args - An array of arguments.
125
+ * @param {number} [args[0]=0] - The target length of the string.
126
+ * @param {string} [args[1]=" "] - The string to pad with.
127
+ * @returns The padded string.
128
+ */
129
+ padEnd: (val: any, args?: string[]) => string;
130
+ };
131
+
132
+ type NestedKeyOf<ObjectType extends object, Depth extends number = 8, UsedDepth extends any[] = []> = UsedDepth['length'] extends Depth ? never : {
133
+ [Key in keyof ObjectType & (string | number)]: ObjectType[Key] extends any[] ? `${Key}` : ObjectType[Key] extends object ? `${Key}` | `${Key}.${NestedKeyOf<ObjectType[Key], Depth, [...UsedDepth, any]>}` : `${Key}`;
134
+ }[keyof ObjectType & (string | number)];
135
+ type NestedKeyOfObj<ObjectType extends object, SkipArrays extends boolean = false, Depth extends number = 8, UsedDepth extends any[] = []> = UsedDepth['length'] extends Depth ? never : {
136
+ [Key in keyof ObjectType & string]: ObjectType[Key] extends Array<any> ? SkipArrays extends true ? never : `${Key}` | `${Key}.${number}` : ObjectType[Key] extends object ? `${Key}` | `${Key}.${NestedKeyOfObj<ObjectType[Key], SkipArrays, Depth, [...UsedDepth, any]>}` : never;
137
+ }[keyof ObjectType & string];
138
+ type PathValue<T, P extends string> = P extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? PathValue<T[Key], Rest> : Key extends `${infer N extends number}` ? N extends keyof T ? PathValue<T[N], Rest> : never : never : P extends keyof T ? T[P] : P extends `${infer N extends number}` ? N extends keyof T ? T[N] : never : never;
139
+ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
140
+ type DeepPick<T, P extends string> = P extends `${infer K}.${infer Rest}` ? K extends keyof T ? {
141
+ [Key in K]: DeepPick<T[K], Rest>;
142
+ } : never : P extends keyof T ? {
143
+ [Key in P]: T[P];
144
+ } : never;
145
+ type Language<T extends Record<string, any>> = keyof T & string;
146
+ type Scope<T extends Record<string, any>, Fallback extends keyof T> = NestedKeyOfObj<T[Fallback], true>;
147
+ type ScopeType<T extends Record<string, any>, F extends keyof T> = Scope<T, F> | Scope<T, F>[] | undefined;
148
+ type EnsureObject<T> = T extends object ? T : {};
149
+ type TranslationObjectFor<S extends Scope<T, Fallback> | Scope<T, Fallback>[] | undefined, T extends Record<string, any>, Fallback extends keyof T> = [
150
+ S
151
+ ] extends [undefined] ? EnsureObject<T[Fallback]> : [S] extends [Scope<T, Fallback>] ? EnsureObject<PathValue<T[Fallback], S>> : [S] extends [Scope<T, Fallback>[]] ? EnsureObject<UnionToIntersection<DeepPick<T[Fallback], S[number]>>> : EnsureObject<T[Fallback]>;
152
+ type Translations<S extends ScopeType<T, F>, T extends Record<string, any>, F extends keyof T & string> = TranslationObjectFor<S, T, F>;
153
+ type Trim<T extends string> = T extends ` ${infer Rest}` ? Trim<Rest> : T extends `${infer Rest} ` ? Trim<Rest> : T;
154
+ type ExtractVarName<T extends string> = T extends `${infer Var}|${string}` ? Trim<Var> : T extends `${infer Var},${string}` ? Trim<Var> : Trim<T>;
155
+ type InterpolationValue = string | number | boolean | Date;
156
+ type Whitespace = " " | "\n" | "\t" | "\r";
157
+ type TrimStart<T extends string> = T extends `${Whitespace}${infer Rest}` ? TrimStart<Rest> : T;
158
+ type ReadBalanced<S extends string, Depth extends 1[] = [1], Acc extends string = ""> = S extends `${infer Before}}${infer After}` ? Before extends `${infer Head}{${infer Tail}` ? ReadBalanced<`${Tail}}${After}`, [...Depth, 1], `${Acc}${Head}{`> : Depth extends [1] ? [`${Acc}${Before}`, After] : Depth extends [1, ...infer Outer extends 1[]] ? ReadBalanced<After, Outer, `${Acc}${Before}}`> : null : null;
159
+ type Param<Name extends string, V> = Trim<Name> extends infer N extends string ? N extends "" ? {} : {
160
+ [K in N]: V;
161
+ } : {};
162
+ type VariableParams<Raw extends string> = Param<ExtractVarName<Raw>, InterpolationValue>;
163
+ type TokenParams<Inner extends string> = Inner extends `{${infer Unwrapped}}` ? TokenBody<Unwrapped> : TokenBody<Inner>;
164
+ type TokenBody<Inner extends string> = Inner extends `${infer Field},${infer Rest}` ? TrimStart<Rest> extends `select${infer AfterKeyword}` ? TrimStart<AfterKeyword> extends `,${infer Cases}` ? Param<Field, string> & CaseParams<Cases> : VariableParams<Inner> : VariableParams<Inner> : VariableParams<Inner>;
165
+ type CaseParams<Cases extends string, Acc = {}> = Cases extends `${string}{${infer Rest}` ? ReadBalanced<Rest> extends [infer Body extends string, infer After extends string] ? CaseParams<After, Acc & ScanParams<Body>> : Acc : Acc;
166
+ type ScanParams<S extends string, Acc = {}> = S extends `${string}{${infer Rest}` ? ReadBalanced<Rest> extends [infer Inner extends string, infer After extends string] ? ScanParams<After, Acc & TokenParams<Inner>> : Acc : Acc;
167
+ type ExtractInterpolationParams<T extends string> = string extends T ? {
168
+ [key: string]: string | number | boolean | Date | undefined;
169
+ } : ScanParams<T>;
170
+ type Simplify<T> = {
171
+ [K in keyof T]: T[K];
172
+ };
173
+ type PluralForms = "zero" | "one" | "two" | "few" | "many" | "other";
174
+ type IsPluralObject<V> = [
175
+ V
176
+ ] extends [object] ? [keyof V] extends [never] ? false : [keyof V] extends [PluralForms] ? true : false : false;
177
+ type ExtractPluralObjectParams<T> = [
178
+ T
179
+ ] extends [never] ? never : T extends Partial<Record<PluralForms, string>> ? Simplify<{
180
+ count: number;
181
+ } & UnionToIntersection<{
182
+ [K in keyof T]: T[K] extends string ? ExtractInterpolationParams<T[K]> : {};
183
+ }[keyof T]>> : never;
184
+ type SuffixParam<T, P extends string> = [
185
+ PathValue<T, P>
186
+ ] extends [never] ? {} : PathValue<T, P> extends string ? ExtractInterpolationParams<PathValue<T, P>> : {};
187
+ type ExtractSuffixPluralParams<Base extends string, T> = Simplify<{
188
+ count: number;
189
+ } & UnionToIntersection<{
190
+ [P in PluralForms]: SuffixParam<T, `${Base}_${P}`>;
191
+ }[PluralForms]>>;
192
+ type PluralParamsFor<K extends string, T> = string extends K ? {
193
+ count: number;
194
+ } & InterpolationOptions : string extends keyof T ? {
195
+ count: number;
196
+ } & InterpolationOptions : [PathValue<T, K>] extends [never] ? ExtractSuffixPluralParams<K, T> : IsPluralObject<PathValue<T, K>> extends true ? ExtractPluralObjectParams<PathValue<T, K>> : ExtractSuffixPluralParams<K, T>;
197
+ type ParamsFor<Val> = string extends Val ? InterpolationOptions : Val extends string ? Simplify<ExtractInterpolationParams<Val>> : IsPluralObject<Val> extends true ? ExtractPluralObjectParams<Val> : {};
198
+ type IsEmptyParams<P> = [
199
+ keyof P
200
+ ] extends [never] ? true : string extends keyof P ? true : false;
201
+ type PluralKeys<S extends ScopeType<T, F>, T extends Record<string, any>, F extends keyof T & string> = string extends keyof Translations<S, T, F> ? string : ExtractPluralKeys<Translations<S, T, F>>;
202
+ type ExtractPluralKeys<T, Path extends string = "", Depth extends any[] = []> = Depth['length'] extends 16 ? never : T extends object ? {
203
+ [K in keyof T & string]: (K extends `${infer Base}_${PluralForms}` ? Path extends "" ? Base : `${Path}.${Base}` : never) | (T[K] extends object ? IsPluralObject<T[K]> extends true ? Path extends "" ? K : `${Path}.${K}` : ExtractPluralKeys<T[K], Path extends "" ? K : `${Path}.${K}`, [...Depth, 1]> : never);
204
+ }[keyof T & string] : never;
205
+ interface InterpolationOptions {
206
+ [key: string]: string | number | Date | boolean | undefined;
207
+ count?: number;
208
+ }
209
+ interface FormatOptions<F extends Record<string, Formatter>, UseDefaultFormatter extends boolean> {
210
+ formatter: keyof EffectiveFormatters<F, UseDefaultFormatter>;
211
+ args?: string[];
212
+ }
213
+ type Formatter<TArgs extends unknown[] = string[]> = (value: any, args?: TArgs) => string;
214
+
215
+ type EffectiveFormatters<F extends Record<string, Formatter>, UseDefault extends boolean> = UseDefault extends true ? F & DefaultFormatters : F;
216
+ type Subscriber<T extends Record<string, any>> = (language: Language<T>, prevLanguage: Language<T>) => void;
217
+ type Unsubscribe = () => void;
218
+ type TagFormatter<T = any> = (content: string) => T;
219
+ type TagInterpolationOptions<T = any> = Record<string, TagFormatter<T> | string | number | Date | boolean | undefined>;
220
+ type TArgs<K extends string, Tr, PK extends string, F extends Record<string, Formatter>, U extends boolean> = unknown extends K ? [values?: InterpolationOptions, format?: FormatOptions<F, U>] : string extends PK ? [values?: InterpolationOptions, format?: FormatOptions<F, U>] : K extends PK ? [values: PluralParamsFor<K, Tr>, format?: FormatOptions<F, U>] : IsEmptyParams<ParamsFor<PathValue<Tr, K>>> extends true ? [values?: InterpolationOptions, format?: FormatOptions<F, U>] : [values: ParamsFor<PathValue<Tr, K>> & InterpolationOptions, format?: FormatOptions<F, U>];
221
+ type TResult<K extends string, Tr, PK extends string> = string extends PK ? PathValue<Tr, K> : K extends PK ? string : PathValue<Tr, K>;
222
+ type RequiredPlural = "one" | "other";
223
+ type OptionalPlural = Exclude<PluralForms, RequiredPlural>;
224
+ type ObjectPlural = {
225
+ [K in RequiredPlural]: string;
226
+ } & Partial<Record<OptionalPlural, string>>;
227
+ type Primitive = string | number | boolean | null | undefined;
228
+ type StrKey<T> = Extract<keyof T, string | number>;
229
+ type PluralObjectKeys<T> = {
230
+ [K in StrKey<T>]: T[K] extends object ? Extract<keyof T[K], PluralForms> extends never ? never : K : never;
231
+ }[StrKey<T>];
232
+ type SuffixKey<T> = Extract<StrKey<T>, `${string}_${PluralForms}`>;
233
+ type SuffixBaseUnion<T> = SuffixKey<T> extends `${infer B}_${PluralForms}` ? B : never;
234
+ type NamespaceKeys<T> = {
235
+ [K in StrKey<T>]: T[K] extends object ? Extract<keyof T[K], PluralForms> extends never ? K : never : never;
236
+ }[StrKey<T>];
237
+ type PlainStringKeys<T> = {
238
+ [K in StrKey<T>]: K extends SuffixKey<T> ? never : T[K] extends string ? K : never;
239
+ }[StrKey<T>];
240
+ type LangWithPlurals<T> = T extends Primitive ? T : T extends (infer U)[] ? LangWithPlurals<U>[] : {
241
+ [K in NamespaceKeys<T>]: LangWithPlurals<T[K]>;
242
+ } & {
243
+ [K in PluralObjectKeys<T>]: ObjectPlural;
244
+ } & {
245
+ [B in SuffixBaseUnion<T> as `${B}_${RequiredPlural}`]: string;
246
+ } & {
247
+ [B in SuffixBaseUnion<T> as `${B}_${OptionalPlural}`]?: string;
248
+ } & {
249
+ [K in PlainStringKeys<T>]: T[K];
250
+ } & {
251
+ [K in Exclude<StrKey<T>, NamespaceKeys<T> | PluralObjectKeys<T> | PlainStringKeys<T> | SuffixKey<T>>]: T[K];
252
+ };
253
+
254
+ /**
255
+ * Configuration for the LocL instance.
256
+ * @template T - The type of the resources object.
257
+ * @template Fallback - The fallback language.
258
+ * @template F - The type of the custom formatters.
259
+ */
260
+ interface LocLConfig<T extends Record<string, any>, Fallback extends keyof T & string, F extends Record<string, Formatter> = {}> {
261
+ /** An object containing all language translations. */
262
+ resources: T;
263
+ /** The initial language to use. */
264
+ language?: (keyof T & string) | string;
265
+ /** The default language to use if a translation is missing. */
266
+ fallbackLanguage: Fallback;
267
+ /** Narrows the translation object to a specific scope(s) (e.g., "common"). */
268
+ scope?: ScopeType<T, Fallback>;
269
+ /** A map of custom formatting functions. */
270
+ formatters?: F;
271
+ /** Whether to include the built-in formatters. Defaults to `true`. */
272
+ useDefaultFormatters?: boolean;
273
+ /** Enables warnings for missing keys and scopes. Defaults to `false`. */
274
+ devMode?: boolean;
275
+ /** Enables caching of scopes and proxies. Defaults to `true`. */
276
+ useCache?: boolean;
277
+ }
278
+ /**
279
+ * The main class for handling translations.
280
+ * It provides methods for translating keys, handling plurals, and formatting values.
281
+ * @template T - The type of the resources object.
282
+ * @template Fallback - The fallback language.
283
+ * @template S - The type of the scope.
284
+ * @template F - The type of the custom formatters.
285
+ * @template UseDefaultFormatter - Whether to use the default formatters.
286
+ */
287
+ declare class LocL<T extends Record<string, any>, Fallback extends keyof T & string, S extends ScopeType<T, Fallback> = undefined, F extends Record<string, Formatter> = {}, UseDefaultFormatter extends boolean = true> {
288
+ private config;
289
+ private resources;
290
+ private language;
291
+ private fallbackLanguage;
292
+ private scope?;
293
+ private formatters;
294
+ private cache;
295
+ private proxyCache;
296
+ private pluralRulesCache;
297
+ private subscribers;
298
+ private readonly rootInstance;
299
+ /**
300
+ * Creates a new LocL instance.
301
+ * @param config - The configuration object.
302
+ */
303
+ constructor(config: LocLConfig<T, Fallback> & {
304
+ fallbackLanguage: Fallback;
305
+ formatters?: F;
306
+ useDefaultFormatters?: UseDefaultFormatter;
307
+ scope?: S;
308
+ });
309
+ /**
310
+ * Creates a new proxy translator instance with a different configuration.
311
+ * This is a lightweight way to create a translator with a different scope or language
312
+ * without creating a completely new instance.
313
+ * @param config - The configuration object.
314
+ * @param {N} [config.scope] - The scope to load translations from.
315
+ * @param {Language<T>} [config.language] - The language to use.
316
+ * @returns A new proxy `LocL` instance.
317
+ */
318
+ withConfig<N extends ScopeType<T, Fallback>>(config: {
319
+ scope?: N;
320
+ language?: Language<T>;
321
+ }): LocL<T, Fallback, N>;
322
+ /**
323
+ * Creates a new `LocL` instance with a different language or scope.
324
+ * @param language - The language to use for the new instance. Defaults to the current language.
325
+ * @param scope - The scope to use for the new instance.
326
+ * @returns A new `LocL` instance.
327
+ */
328
+ clone<N extends ScopeType<T, Fallback> = undefined>(language?: Language<T>, scope?: N): LocL<T, Fallback, N>;
329
+ /**
330
+ * Subscribes a listener to language changes.
331
+ * Conforms to the standard reactive store contract (e.g. Svelte stores).
332
+ * Calls the listener immediately with the current language and returns an unsubscribe function.
333
+ * @param listener - The subscriber callback function.
334
+ * @returns An unsubscribe function.
335
+ */
336
+ subscribe(listener: Subscriber<T>): Unsubscribe;
337
+ /**
338
+ * Gets the current active language.
339
+ */
340
+ getLanguage(): Language<T>;
341
+ /**
342
+ * Adds or overrides a single translation key at runtime.
343
+ * @param lang - Target language code.
344
+ * @param key - Dotted path key.
345
+ * @param value - The translation value.
346
+ */
347
+ addResource(lang: Language<T> | string, key: string, value: any): void;
348
+ /**
349
+ * Deeply merges a resource bundle into the specified language at runtime.
350
+ * @param lang - Target language code.
351
+ * @param bundle - Object of translations to merge.
352
+ */
353
+ addResources(lang: Language<T> | string, bundle: Record<string, any>): void;
354
+ /**
355
+ * Changes the current language of the translator.
356
+ * @param lang - The new language to set.
357
+ */
358
+ changeLanguage(lang: Language<T>): void;
359
+ /**
360
+ * Translates a key.
361
+ * If no key is provided, it returns the entire translation object for the current scope.
362
+ * @param key - The key to translate.
363
+ * @param values - An object with values to interpolate into the translation.
364
+ * @param format - An object with formatting options.
365
+ * @returns The translated and formatted string, or the translation object.
366
+ */
367
+ t<K extends (NestedKeyOf<TranslationObjectFor<S, T, Fallback>> | PluralKeys<S, T, Fallback>) & string>(key: K, ...args: TArgs<K, TranslationObjectFor<S, T, Fallback>, PluralKeys<S, T, Fallback>, F, UseDefaultFormatter>): TResult<K, TranslationObjectFor<S, T, Fallback>, PluralKeys<S, T, Fallback>>;
368
+ /**
369
+ * Translates a key without strict type checking.
370
+ * This is useful for compatibility with other i18n libraries or dynamic keys.
371
+ * @param key - The key to translate.
372
+ * @param values - An object with values to interpolate into the translation.
373
+ * @param format - An object with formatting options.
374
+ * @returns The translated and formatted string.
375
+ */
376
+ tt(key?: any, values?: InterpolationOptions, format?: FormatOptions<F, UseDefaultFormatter>): any;
377
+ /**
378
+ * Formats a value using a specific formatter.
379
+ * @param value - The value to format.
380
+ * @param formatter - The name of the formatter to use.
381
+ * @param args - An array of arguments to pass to the formatter.
382
+ * @returns The formatted string.
383
+ */
384
+ format(value: string, formatter: keyof EffectiveFormatters<F, UseDefaultFormatter>, args?: string[]): string;
385
+ /**
386
+ * Translates a key with pluralization.
387
+ * It automatically selects the correct plural form based on the `count` value.
388
+ * @param key - The base key for the pluralization.
389
+ * @param values - An object with a `count` property and other values to interpolate.
390
+ * @param format - An object with formatting options.
391
+ * @returns The translated, pluralized, and formatted string.
392
+ */
393
+ plural<K extends PluralKeys<S, T, Fallback>>(key: K, values: PluralParamsFor<K, TranslationObjectFor<S, T, Fallback>>, format?: FormatOptions<F, UseDefaultFormatter>): string;
394
+ /**
395
+ * Translates a key and interpolates rich elements using tag functions.
396
+ * Matches `<tag>content</tag>` in translation templates.
397
+ * @param key - The key to translate.
398
+ * @param tags - An object mapping tag names to functions that transform their inner content.
399
+ * @param values - Values to interpolate into variable placeholders ({name}, {{name}}).
400
+ * @returns An array of chunks (strings and custom rendered tag values).
401
+ * @example
402
+ * ```ts
403
+ * translator.rich("terms", {
404
+ * link: (content) => `<a href="/terms">${content}</a>`
405
+ * }, { name: "Alice" });
406
+ * ```
407
+ */
408
+ rich<TOutput = any>(key: string, tags: TagInterpolationOptions<TOutput>, values?: InterpolationOptions): (string | TOutput)[];
409
+ /**
410
+ * Gets a translation object or a specific translation value.
411
+ * If no key is provided, it returns the entire translation object for the current language and scope.
412
+ * @param key - The key of the translation to get.
413
+ * @returns The translation object or value, or `undefined` if not found.
414
+ */
415
+ get(): TranslationObjectFor<S, T, Fallback>;
416
+ get<K extends NestedKeyOf<TranslationObjectFor<S, T, Fallback>>>(key: K): PathValue<TranslationObjectFor<S, T, Fallback>, K> | undefined;
417
+ /**
418
+ * Gets a nested object from the translations.
419
+ * This is a type-safe way to get a nested object.
420
+ * @param key - The key of the object to get.
421
+ * @returns The nested translation object, or `undefined` if not found.
422
+ */
423
+ getObj<K extends NestedKeyOfObj<TranslationObjectFor<S, T, Fallback>, false>>(key: K): PathValue<TranslationObjectFor<S, T, Fallback>, K> | undefined;
424
+ private isLanguage;
425
+ private getCacheKey;
426
+ private invalidateCacheForLang;
427
+ private lookupWithFallback;
428
+ private buildTranslationObject;
429
+ private findTranslation;
430
+ private getPluralRules;
431
+ private checkPlural;
432
+ private interpolate;
433
+ private applyFormat;
434
+ private makeReadOnly;
435
+ private isUnsafeObjectKey;
436
+ }
437
+
438
+ export { type Formatter as F, type InterpolationOptions as I, type LocLConfig as L, type NestedKeyOf as N, type PluralParamsFor as P, type ScopeType as S, type TranslationObjectFor as T, LocL as a, type LangWithPlurals as b, type ParamsFor as c, type PathValue as d, type IsEmptyParams as e, type PluralKeys as f, type Language as g, type Scope as h };
@@ -0,0 +1,54 @@
1
+ import { N as NestedKeyOf, I as InterpolationOptions, P as PluralParamsFor, c as ParamsFor, d as PathValue, e as IsEmptyParams, T as TranslationObjectFor, f as PluralKeys, a as LocL } from './LocL-v4yNIiKt.mjs';
2
+ import { DefaultResources, DefaultFallback } from './react.mjs';
3
+ import 'react';
4
+
5
+ type NormalizeKey<K extends string> = K extends `${infer NS}:${infer Rest}` ? `${NS}.${Rest}` : K;
6
+ type ColonKeys<K extends string> = K extends `${infer NS}.${infer Rest}` ? `${NS}:${Rest}` | `${NS}.${Rest}` : K;
7
+ type BaseKeys<Tr, PK extends string> = ((Tr extends object ? NestedKeyOf<Tr> : never) | PK) & string;
8
+ type I18nextKey<Tr, PK extends string> = ColonKeys<BaseKeys<Tr, PK>>;
9
+ interface I18nextOptions extends InterpolationOptions {
10
+ ns?: string;
11
+ defaultValue?: string;
12
+ count?: number;
13
+ }
14
+ type I18nextParamsFor<K extends string, Tr, PK extends string> = NormalizeKey<K> extends PK ? PluralParamsFor<NormalizeKey<K>, Tr> : ParamsFor<PathValue<Tr, NormalizeKey<K>>>;
15
+ type I18nextHasParams<K extends string, Tr, PK extends string> = NormalizeKey<K> extends PK ? true : IsEmptyParams<ParamsFor<PathValue<Tr, NormalizeKey<K>>>> extends true ? false : true;
16
+ type I18nextOptionsFor<K extends string, Tr, PK extends string> = I18nextParamsFor<K, Tr, PK> & I18nextOptions;
17
+ interface I18nextTranslation<T extends Record<string, any> = DefaultResources, Fallback extends keyof T & string = DefaultFallback<T>, Tr = TranslationObjectFor<undefined, T, Fallback>, PK extends string = PluralKeys<undefined, T, Fallback>> {
18
+ <K extends I18nextKey<Tr, PK>>(key: K, ...args: I18nextHasParams<K, Tr, PK> extends true ? [options: I18nextOptionsFor<K, Tr, PK>] : [options?: I18nextOptions]): string;
19
+ <K extends I18nextKey<Tr, PK>>(key: K, defaultValue: string, ...args: I18nextHasParams<K, Tr, PK> extends true ? [options: I18nextOptionsFor<K, Tr, PK>] : [options?: I18nextOptions]): string;
20
+ (key: string, defaultValue: string, options?: I18nextOptions): string;
21
+ (key: string, options: {
22
+ defaultValue: string;
23
+ } & I18nextOptions): string;
24
+ (key: string, options: {
25
+ ns: string;
26
+ } & I18nextOptions): string;
27
+ }
28
+ interface I18nextCompat<T extends Record<string, any> = DefaultResources, Fallback extends keyof T & string = DefaultFallback<T>, Tr = TranslationObjectFor<undefined, T, Fallback>, PK extends string = PluralKeys<undefined, T, Fallback>> {
29
+ t: I18nextTranslation<T, Fallback>;
30
+ exists<K extends I18nextKey<Tr, PK>>(key: K, options?: {
31
+ ns?: string;
32
+ [key: string]: any;
33
+ }): boolean;
34
+ exists(key: string, options?: {
35
+ ns?: string;
36
+ [key: string]: any;
37
+ }): boolean;
38
+ get language(): string;
39
+ get languages(): string[];
40
+ changeLanguage(lng: string, callback?: (err: any, t: any) => void): Promise<string>;
41
+ on(event: string, listener: (...args: any[]) => void): () => void;
42
+ off(event: string, listener: (...args: any[]) => void): void;
43
+ addResource(lng: string, ns: string, key: string, value: any): void;
44
+ addResources(lng: string, ns: string, resources: any): void;
45
+ addResourceBundle(lng: string, ns: string, resources: any, deep?: boolean, overwrite?: boolean): void;
46
+ hasResourceBundle(lng: string, ns: string): boolean;
47
+ getResourceBundle(lng: string, ns: string): any;
48
+ loadNamespaces(ns: string | string[], callback?: () => void): Promise<void>;
49
+ translator: LocL<T, Fallback, any, any, any>;
50
+ }
51
+ declare function toI18next<T extends Record<string, any> = DefaultResources, Fallback extends keyof T & string = DefaultFallback<T>>(translator: LocL<T, Fallback, any, any, any>): I18nextCompat<T, Fallback>;
52
+ declare const createI18nextCompat: typeof toI18next;
53
+
54
+ export { type BaseKeys, type ColonKeys, type I18nextCompat, type I18nextHasParams, type I18nextKey, type I18nextOptions, type I18nextOptionsFor, type I18nextParamsFor, type I18nextTranslation, type NormalizeKey, createI18nextCompat, toI18next };
@@ -0,0 +1,54 @@
1
+ import { N as NestedKeyOf, I as InterpolationOptions, P as PluralParamsFor, c as ParamsFor, d as PathValue, e as IsEmptyParams, T as TranslationObjectFor, f as PluralKeys, a as LocL } from './LocL-v4yNIiKt.js';
2
+ import { DefaultResources, DefaultFallback } from './react.js';
3
+ import 'react';
4
+
5
+ type NormalizeKey<K extends string> = K extends `${infer NS}:${infer Rest}` ? `${NS}.${Rest}` : K;
6
+ type ColonKeys<K extends string> = K extends `${infer NS}.${infer Rest}` ? `${NS}:${Rest}` | `${NS}.${Rest}` : K;
7
+ type BaseKeys<Tr, PK extends string> = ((Tr extends object ? NestedKeyOf<Tr> : never) | PK) & string;
8
+ type I18nextKey<Tr, PK extends string> = ColonKeys<BaseKeys<Tr, PK>>;
9
+ interface I18nextOptions extends InterpolationOptions {
10
+ ns?: string;
11
+ defaultValue?: string;
12
+ count?: number;
13
+ }
14
+ type I18nextParamsFor<K extends string, Tr, PK extends string> = NormalizeKey<K> extends PK ? PluralParamsFor<NormalizeKey<K>, Tr> : ParamsFor<PathValue<Tr, NormalizeKey<K>>>;
15
+ type I18nextHasParams<K extends string, Tr, PK extends string> = NormalizeKey<K> extends PK ? true : IsEmptyParams<ParamsFor<PathValue<Tr, NormalizeKey<K>>>> extends true ? false : true;
16
+ type I18nextOptionsFor<K extends string, Tr, PK extends string> = I18nextParamsFor<K, Tr, PK> & I18nextOptions;
17
+ interface I18nextTranslation<T extends Record<string, any> = DefaultResources, Fallback extends keyof T & string = DefaultFallback<T>, Tr = TranslationObjectFor<undefined, T, Fallback>, PK extends string = PluralKeys<undefined, T, Fallback>> {
18
+ <K extends I18nextKey<Tr, PK>>(key: K, ...args: I18nextHasParams<K, Tr, PK> extends true ? [options: I18nextOptionsFor<K, Tr, PK>] : [options?: I18nextOptions]): string;
19
+ <K extends I18nextKey<Tr, PK>>(key: K, defaultValue: string, ...args: I18nextHasParams<K, Tr, PK> extends true ? [options: I18nextOptionsFor<K, Tr, PK>] : [options?: I18nextOptions]): string;
20
+ (key: string, defaultValue: string, options?: I18nextOptions): string;
21
+ (key: string, options: {
22
+ defaultValue: string;
23
+ } & I18nextOptions): string;
24
+ (key: string, options: {
25
+ ns: string;
26
+ } & I18nextOptions): string;
27
+ }
28
+ interface I18nextCompat<T extends Record<string, any> = DefaultResources, Fallback extends keyof T & string = DefaultFallback<T>, Tr = TranslationObjectFor<undefined, T, Fallback>, PK extends string = PluralKeys<undefined, T, Fallback>> {
29
+ t: I18nextTranslation<T, Fallback>;
30
+ exists<K extends I18nextKey<Tr, PK>>(key: K, options?: {
31
+ ns?: string;
32
+ [key: string]: any;
33
+ }): boolean;
34
+ exists(key: string, options?: {
35
+ ns?: string;
36
+ [key: string]: any;
37
+ }): boolean;
38
+ get language(): string;
39
+ get languages(): string[];
40
+ changeLanguage(lng: string, callback?: (err: any, t: any) => void): Promise<string>;
41
+ on(event: string, listener: (...args: any[]) => void): () => void;
42
+ off(event: string, listener: (...args: any[]) => void): void;
43
+ addResource(lng: string, ns: string, key: string, value: any): void;
44
+ addResources(lng: string, ns: string, resources: any): void;
45
+ addResourceBundle(lng: string, ns: string, resources: any, deep?: boolean, overwrite?: boolean): void;
46
+ hasResourceBundle(lng: string, ns: string): boolean;
47
+ getResourceBundle(lng: string, ns: string): any;
48
+ loadNamespaces(ns: string | string[], callback?: () => void): Promise<void>;
49
+ translator: LocL<T, Fallback, any, any, any>;
50
+ }
51
+ declare function toI18next<T extends Record<string, any> = DefaultResources, Fallback extends keyof T & string = DefaultFallback<T>>(translator: LocL<T, Fallback, any, any, any>): I18nextCompat<T, Fallback>;
52
+ declare const createI18nextCompat: typeof toI18next;
53
+
54
+ export { type BaseKeys, type ColonKeys, type I18nextCompat, type I18nextHasParams, type I18nextKey, type I18nextOptions, type I18nextOptionsFor, type I18nextParamsFor, type I18nextTranslation, type NormalizeKey, createI18nextCompat, toI18next };