intor-translator 1.4.7 → 1.4.8
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.
- package/dist/index.cjs +2 -2
- package/dist/index.d.cts +25 -52
- package/dist/index.d.ts +25 -52
- package/dist/index.js +2 -2
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -295,7 +295,7 @@ var CoreTranslator = class extends BaseTranslator {
|
|
|
295
295
|
});
|
|
296
296
|
};
|
|
297
297
|
/** Get the translated message for a key, with optional replacements. */
|
|
298
|
-
t = (key,
|
|
298
|
+
t = (key, replacements) => {
|
|
299
299
|
return translate({
|
|
300
300
|
hooks: this.hooks,
|
|
301
301
|
messages: this._messages,
|
|
@@ -303,7 +303,7 @@ var CoreTranslator = class extends BaseTranslator {
|
|
|
303
303
|
isLoading: this._isLoading,
|
|
304
304
|
translateConfig: this.translateConfig,
|
|
305
305
|
key,
|
|
306
|
-
replacements
|
|
306
|
+
replacements
|
|
307
307
|
});
|
|
308
308
|
};
|
|
309
309
|
};
|
package/dist/index.d.cts
CHANGED
|
@@ -82,6 +82,24 @@ type Locale<M = unknown> = M extends LocaleMessages ? keyof M & string : string;
|
|
|
82
82
|
*/
|
|
83
83
|
type FallbackLocalesMap<L extends string = string> = Partial<Record<L, L[]>>;
|
|
84
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Represents a replacement map used for interpolating values
|
|
87
|
+
* in message templates.
|
|
88
|
+
*
|
|
89
|
+
* Replacement values are treated as plain data and interpreted
|
|
90
|
+
* by the message formatter at runtime.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* const replacements: Replacement = {
|
|
94
|
+
* name: "Alice",
|
|
95
|
+
* count: 5,
|
|
96
|
+
* nested: {
|
|
97
|
+
* score: 100,
|
|
98
|
+
* },
|
|
99
|
+
* };
|
|
100
|
+
*/
|
|
101
|
+
type Replacement = Record<string, MessageValue>;
|
|
102
|
+
|
|
85
103
|
/**
|
|
86
104
|
* Default maximum recursive depth for nested key type computations,
|
|
87
105
|
* balancing type safety and compiler performance.
|
|
@@ -222,49 +240,6 @@ type ScopedLeafKeys<M, PK extends string, D extends number = DefaultDepth> = M e
|
|
|
222
240
|
*/
|
|
223
241
|
type ScopedLeafValue<M, PK extends string, K extends string> = M extends LocaleMessages ? M[Locale<M>] extends infer Messages ? Messages extends MessageValue ? AtPath<Messages, PK> extends infer Scoped ? Scoped extends MessageObject ? LeafValue<Scoped, K> : never : never : never : never : MessageValue;
|
|
224
242
|
|
|
225
|
-
/**
|
|
226
|
-
* Represents a replacement map used for interpolating values
|
|
227
|
-
* in message templates.
|
|
228
|
-
*
|
|
229
|
-
* Replacement values are treated as plain data and interpreted
|
|
230
|
-
* by the message formatter at runtime.
|
|
231
|
-
*
|
|
232
|
-
* @example
|
|
233
|
-
* const replacements: Replacement = {
|
|
234
|
-
* name: "Alice",
|
|
235
|
-
* count: 5,
|
|
236
|
-
* nested: {
|
|
237
|
-
* score: 100,
|
|
238
|
-
* },
|
|
239
|
-
* };
|
|
240
|
-
*/
|
|
241
|
-
type Replacement = Record<string, unknown>;
|
|
242
|
-
/**
|
|
243
|
-
* Resolves the expected replacement type for a localized message key.
|
|
244
|
-
*
|
|
245
|
-
* Uses the canonical (default-locale) replacement schema when available,
|
|
246
|
-
* otherwise falls back to `Replacement`.
|
|
247
|
-
*
|
|
248
|
-
* @example
|
|
249
|
-
* ```ts
|
|
250
|
-
* type RMap = {
|
|
251
|
-
* "{locale}": {
|
|
252
|
-
* welcome: { name: MessageValue };
|
|
253
|
-
* total: { count: MessageValue };
|
|
254
|
-
* };
|
|
255
|
-
* };
|
|
256
|
-
*
|
|
257
|
-
* type WelcomeReplacement = LocalizedReplacement<RMap, "welcome">;
|
|
258
|
-
* // => { name: MessageValue }
|
|
259
|
-
*
|
|
260
|
-
* type UnknownReplacement = LocalizedReplacement<RMap, "unknown">;
|
|
261
|
-
* // => Replacement
|
|
262
|
-
* ```
|
|
263
|
-
*/
|
|
264
|
-
type LocalizedReplacement<ReplacementSchema, K extends string> = ReplacementSchema extends {
|
|
265
|
-
"{locale}": infer LM;
|
|
266
|
-
} ? AtPath<LM, K> extends infer R ? R extends MessageObject ? R : Replacement : Replacement : Replacement;
|
|
267
|
-
|
|
268
243
|
/**
|
|
269
244
|
* Options for initializing a translator
|
|
270
245
|
*
|
|
@@ -414,7 +389,7 @@ interface TranslatorPlugin {
|
|
|
414
389
|
*
|
|
415
390
|
* @template M - Shape of the messages object.
|
|
416
391
|
*/
|
|
417
|
-
declare class CoreTranslator<M extends LocaleMessages | unknown = unknown
|
|
392
|
+
declare class CoreTranslator<M extends LocaleMessages | unknown = unknown> extends BaseTranslator<M> {
|
|
418
393
|
/** User-provided options including messages, locale, and config. */
|
|
419
394
|
protected translateConfig: TranslateConfig<M>;
|
|
420
395
|
/** Active pipeline hooks applied during translation. */
|
|
@@ -429,21 +404,19 @@ declare class CoreTranslator<M extends LocaleMessages | unknown = unknown, Repla
|
|
|
429
404
|
/** Check if a key exists in the specified locale or current locale. */
|
|
430
405
|
hasKey: <K extends LocalizedLeafKeys<M>>(key: K, targetLocale?: Locale<M>) => boolean;
|
|
431
406
|
/** Get the translated message for a key, with optional replacements. */
|
|
432
|
-
t: <K extends LocalizedLeafKeys<M> = LocalizedLeafKeys<M>>(key: K,
|
|
407
|
+
t: <K extends LocalizedLeafKeys<M> = LocalizedLeafKeys<M>>(key: K, replacements?: Replacement) => LocalizedLeafValue<M, K>;
|
|
433
408
|
}
|
|
434
409
|
|
|
435
410
|
type ScopeTranslatorOptions<M> = CoreTranslatorOptions<M>;
|
|
436
|
-
type ScopeTranslatorMethods<M extends LocaleMessages | unknown = unknown,
|
|
411
|
+
type ScopeTranslatorMethods<M extends LocaleMessages | unknown = unknown, PK extends string | undefined = undefined, K extends string = PK extends string ? ScopedLeafKeys<M, PK> : LocalizedLeafKeys<M>> = {
|
|
437
412
|
hasKey: (key?: K, targetLocale?: Locale<M>) => boolean;
|
|
438
|
-
t:
|
|
439
|
-
LocalizedReplacement<ReplacementSchema, PK extends string ? `${PK}.${Key & string}` : Key>?
|
|
440
|
-
]) => PK extends string ? ScopedLeafValue<M, PK, Key> : LocalizedLeafValue<M, Key>;
|
|
413
|
+
t: (key?: K, replacements?: Replacement) => PK extends string ? ScopedLeafValue<M, PK, K> : LocalizedLeafValue<M, K>;
|
|
441
414
|
};
|
|
442
415
|
|
|
443
|
-
declare class ScopeTranslator<M extends LocaleMessages | unknown = unknown
|
|
416
|
+
declare class ScopeTranslator<M extends LocaleMessages | unknown = unknown> extends CoreTranslator<M> {
|
|
444
417
|
constructor(options: ScopeTranslatorOptions<M>);
|
|
445
418
|
/** Create a scoped translator with a prefix key for resolving nested message paths. */
|
|
446
|
-
scoped<PK extends LocalizedNodeKeys<M> | undefined = undefined>(preKey?: PK): PK extends string ? ScopeTranslatorMethods<M,
|
|
419
|
+
scoped<PK extends LocalizedNodeKeys<M> | undefined = undefined>(preKey?: PK): PK extends string ? ScopeTranslatorMethods<M, PK> : ScopeTranslatorMethods<M>;
|
|
447
420
|
}
|
|
448
421
|
|
|
449
422
|
/** Semantic tag attributes map. */
|
|
@@ -518,4 +491,4 @@ interface Renderer<Output> {
|
|
|
518
491
|
*/
|
|
519
492
|
declare function renderRichMessage<Output>(message: MessageValue, renderer: Renderer<Output>): Output[];
|
|
520
493
|
|
|
521
|
-
export { type ASTNode, type AtPath, type Attributes, type DefaultDepth, type FallbackLocalesMap, type FormatHandler, type HandlerContext, type LeafKeys, type LeafValue, type LoadingHandler, type Locale, type LocaleMessages, type LocalizedLeafKeys, type LocalizedLeafValue, type LocalizedNodeKeys, type
|
|
494
|
+
export { type ASTNode, type AtPath, type Attributes, type DefaultDepth, type FallbackLocalesMap, type FormatHandler, type HandlerContext, type LeafKeys, type LeafValue, type LoadingHandler, type Locale, type LocaleMessages, type LocalizedLeafKeys, type LocalizedLeafValue, type LocalizedNodeKeys, type MessageObject, type MessageValue, type MissingHandler, type NodeKeys, type Renderer, type Replacement, type ScopedLeafKeys, type ScopedLeafValue, type TranslateConfig, type TranslateContext, type TranslateHandlers, type TranslateHook, ScopeTranslator as Translator, type ScopeTranslatorMethods as TranslatorMethods, type ScopeTranslatorOptions as TranslatorOptions, type TranslatorPlugin, parseRichMessage, renderRichMessage };
|
package/dist/index.d.ts
CHANGED
|
@@ -82,6 +82,24 @@ type Locale<M = unknown> = M extends LocaleMessages ? keyof M & string : string;
|
|
|
82
82
|
*/
|
|
83
83
|
type FallbackLocalesMap<L extends string = string> = Partial<Record<L, L[]>>;
|
|
84
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Represents a replacement map used for interpolating values
|
|
87
|
+
* in message templates.
|
|
88
|
+
*
|
|
89
|
+
* Replacement values are treated as plain data and interpreted
|
|
90
|
+
* by the message formatter at runtime.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* const replacements: Replacement = {
|
|
94
|
+
* name: "Alice",
|
|
95
|
+
* count: 5,
|
|
96
|
+
* nested: {
|
|
97
|
+
* score: 100,
|
|
98
|
+
* },
|
|
99
|
+
* };
|
|
100
|
+
*/
|
|
101
|
+
type Replacement = Record<string, MessageValue>;
|
|
102
|
+
|
|
85
103
|
/**
|
|
86
104
|
* Default maximum recursive depth for nested key type computations,
|
|
87
105
|
* balancing type safety and compiler performance.
|
|
@@ -222,49 +240,6 @@ type ScopedLeafKeys<M, PK extends string, D extends number = DefaultDepth> = M e
|
|
|
222
240
|
*/
|
|
223
241
|
type ScopedLeafValue<M, PK extends string, K extends string> = M extends LocaleMessages ? M[Locale<M>] extends infer Messages ? Messages extends MessageValue ? AtPath<Messages, PK> extends infer Scoped ? Scoped extends MessageObject ? LeafValue<Scoped, K> : never : never : never : never : MessageValue;
|
|
224
242
|
|
|
225
|
-
/**
|
|
226
|
-
* Represents a replacement map used for interpolating values
|
|
227
|
-
* in message templates.
|
|
228
|
-
*
|
|
229
|
-
* Replacement values are treated as plain data and interpreted
|
|
230
|
-
* by the message formatter at runtime.
|
|
231
|
-
*
|
|
232
|
-
* @example
|
|
233
|
-
* const replacements: Replacement = {
|
|
234
|
-
* name: "Alice",
|
|
235
|
-
* count: 5,
|
|
236
|
-
* nested: {
|
|
237
|
-
* score: 100,
|
|
238
|
-
* },
|
|
239
|
-
* };
|
|
240
|
-
*/
|
|
241
|
-
type Replacement = Record<string, unknown>;
|
|
242
|
-
/**
|
|
243
|
-
* Resolves the expected replacement type for a localized message key.
|
|
244
|
-
*
|
|
245
|
-
* Uses the canonical (default-locale) replacement schema when available,
|
|
246
|
-
* otherwise falls back to `Replacement`.
|
|
247
|
-
*
|
|
248
|
-
* @example
|
|
249
|
-
* ```ts
|
|
250
|
-
* type RMap = {
|
|
251
|
-
* "{locale}": {
|
|
252
|
-
* welcome: { name: MessageValue };
|
|
253
|
-
* total: { count: MessageValue };
|
|
254
|
-
* };
|
|
255
|
-
* };
|
|
256
|
-
*
|
|
257
|
-
* type WelcomeReplacement = LocalizedReplacement<RMap, "welcome">;
|
|
258
|
-
* // => { name: MessageValue }
|
|
259
|
-
*
|
|
260
|
-
* type UnknownReplacement = LocalizedReplacement<RMap, "unknown">;
|
|
261
|
-
* // => Replacement
|
|
262
|
-
* ```
|
|
263
|
-
*/
|
|
264
|
-
type LocalizedReplacement<ReplacementSchema, K extends string> = ReplacementSchema extends {
|
|
265
|
-
"{locale}": infer LM;
|
|
266
|
-
} ? AtPath<LM, K> extends infer R ? R extends MessageObject ? R : Replacement : Replacement : Replacement;
|
|
267
|
-
|
|
268
243
|
/**
|
|
269
244
|
* Options for initializing a translator
|
|
270
245
|
*
|
|
@@ -414,7 +389,7 @@ interface TranslatorPlugin {
|
|
|
414
389
|
*
|
|
415
390
|
* @template M - Shape of the messages object.
|
|
416
391
|
*/
|
|
417
|
-
declare class CoreTranslator<M extends LocaleMessages | unknown = unknown
|
|
392
|
+
declare class CoreTranslator<M extends LocaleMessages | unknown = unknown> extends BaseTranslator<M> {
|
|
418
393
|
/** User-provided options including messages, locale, and config. */
|
|
419
394
|
protected translateConfig: TranslateConfig<M>;
|
|
420
395
|
/** Active pipeline hooks applied during translation. */
|
|
@@ -429,21 +404,19 @@ declare class CoreTranslator<M extends LocaleMessages | unknown = unknown, Repla
|
|
|
429
404
|
/** Check if a key exists in the specified locale or current locale. */
|
|
430
405
|
hasKey: <K extends LocalizedLeafKeys<M>>(key: K, targetLocale?: Locale<M>) => boolean;
|
|
431
406
|
/** Get the translated message for a key, with optional replacements. */
|
|
432
|
-
t: <K extends LocalizedLeafKeys<M> = LocalizedLeafKeys<M>>(key: K,
|
|
407
|
+
t: <K extends LocalizedLeafKeys<M> = LocalizedLeafKeys<M>>(key: K, replacements?: Replacement) => LocalizedLeafValue<M, K>;
|
|
433
408
|
}
|
|
434
409
|
|
|
435
410
|
type ScopeTranslatorOptions<M> = CoreTranslatorOptions<M>;
|
|
436
|
-
type ScopeTranslatorMethods<M extends LocaleMessages | unknown = unknown,
|
|
411
|
+
type ScopeTranslatorMethods<M extends LocaleMessages | unknown = unknown, PK extends string | undefined = undefined, K extends string = PK extends string ? ScopedLeafKeys<M, PK> : LocalizedLeafKeys<M>> = {
|
|
437
412
|
hasKey: (key?: K, targetLocale?: Locale<M>) => boolean;
|
|
438
|
-
t:
|
|
439
|
-
LocalizedReplacement<ReplacementSchema, PK extends string ? `${PK}.${Key & string}` : Key>?
|
|
440
|
-
]) => PK extends string ? ScopedLeafValue<M, PK, Key> : LocalizedLeafValue<M, Key>;
|
|
413
|
+
t: (key?: K, replacements?: Replacement) => PK extends string ? ScopedLeafValue<M, PK, K> : LocalizedLeafValue<M, K>;
|
|
441
414
|
};
|
|
442
415
|
|
|
443
|
-
declare class ScopeTranslator<M extends LocaleMessages | unknown = unknown
|
|
416
|
+
declare class ScopeTranslator<M extends LocaleMessages | unknown = unknown> extends CoreTranslator<M> {
|
|
444
417
|
constructor(options: ScopeTranslatorOptions<M>);
|
|
445
418
|
/** Create a scoped translator with a prefix key for resolving nested message paths. */
|
|
446
|
-
scoped<PK extends LocalizedNodeKeys<M> | undefined = undefined>(preKey?: PK): PK extends string ? ScopeTranslatorMethods<M,
|
|
419
|
+
scoped<PK extends LocalizedNodeKeys<M> | undefined = undefined>(preKey?: PK): PK extends string ? ScopeTranslatorMethods<M, PK> : ScopeTranslatorMethods<M>;
|
|
447
420
|
}
|
|
448
421
|
|
|
449
422
|
/** Semantic tag attributes map. */
|
|
@@ -518,4 +491,4 @@ interface Renderer<Output> {
|
|
|
518
491
|
*/
|
|
519
492
|
declare function renderRichMessage<Output>(message: MessageValue, renderer: Renderer<Output>): Output[];
|
|
520
493
|
|
|
521
|
-
export { type ASTNode, type AtPath, type Attributes, type DefaultDepth, type FallbackLocalesMap, type FormatHandler, type HandlerContext, type LeafKeys, type LeafValue, type LoadingHandler, type Locale, type LocaleMessages, type LocalizedLeafKeys, type LocalizedLeafValue, type LocalizedNodeKeys, type
|
|
494
|
+
export { type ASTNode, type AtPath, type Attributes, type DefaultDepth, type FallbackLocalesMap, type FormatHandler, type HandlerContext, type LeafKeys, type LeafValue, type LoadingHandler, type Locale, type LocaleMessages, type LocalizedLeafKeys, type LocalizedLeafValue, type LocalizedNodeKeys, type MessageObject, type MessageValue, type MissingHandler, type NodeKeys, type Renderer, type Replacement, type ScopedLeafKeys, type ScopedLeafValue, type TranslateConfig, type TranslateContext, type TranslateHandlers, type TranslateHook, ScopeTranslator as Translator, type ScopeTranslatorMethods as TranslatorMethods, type ScopeTranslatorOptions as TranslatorOptions, type TranslatorPlugin, parseRichMessage, renderRichMessage };
|
package/dist/index.js
CHANGED
|
@@ -293,7 +293,7 @@ var CoreTranslator = class extends BaseTranslator {
|
|
|
293
293
|
});
|
|
294
294
|
};
|
|
295
295
|
/** Get the translated message for a key, with optional replacements. */
|
|
296
|
-
t = (key,
|
|
296
|
+
t = (key, replacements) => {
|
|
297
297
|
return translate({
|
|
298
298
|
hooks: this.hooks,
|
|
299
299
|
messages: this._messages,
|
|
@@ -301,7 +301,7 @@ var CoreTranslator = class extends BaseTranslator {
|
|
|
301
301
|
isLoading: this._isLoading,
|
|
302
302
|
translateConfig: this.translateConfig,
|
|
303
303
|
key,
|
|
304
|
-
replacements
|
|
304
|
+
replacements
|
|
305
305
|
});
|
|
306
306
|
};
|
|
307
307
|
};
|