intor-translator 1.4.6 → 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 CHANGED
@@ -3,6 +3,15 @@
3
3
  var rura = require('rura');
4
4
 
5
5
  // src/translators/core-translator/core-translator.ts
6
+ function runTranslate(options) {
7
+ const context = {
8
+ ...options,
9
+ config: options.translateConfig,
10
+ candidateLocales: [],
11
+ meta: {}
12
+ };
13
+ return rura.rura.run(context, options.hooks);
14
+ }
6
15
 
7
16
  // src/shared/utils/find-message-in-locales.ts
8
17
  var findMessageInLocales = ({
@@ -231,15 +240,6 @@ var hasKey = ({
231
240
  const message = findMessageInLocales({ messages, candidateLocales, key });
232
241
  return message !== void 0;
233
242
  };
234
- function runTranslate(options) {
235
- const context = {
236
- ...options,
237
- config: options.translateConfig,
238
- candidateLocales: [],
239
- meta: {}
240
- };
241
- return rura.rura.run(context, options.hooks);
242
- }
243
243
 
244
244
  // src/translators/methods/translate.ts
245
245
  function translate(options) {
@@ -332,7 +332,7 @@ var ScopeTranslator = class extends CoreTranslator {
332
332
  targetLocale
333
333
  });
334
334
  },
335
- t: (key, replacements) => {
335
+ t: (key, ...args) => {
336
336
  const fullKey = getFullKey(preKey, key);
337
337
  return translate({
338
338
  hooks: this.hooks,
@@ -341,7 +341,7 @@ var ScopeTranslator = class extends CoreTranslator {
341
341
  isLoading: this._isLoading,
342
342
  translateConfig: this.translateConfig,
343
343
  key: fullKey,
344
- replacements
344
+ replacements: args[0]
345
345
  });
346
346
  }
347
347
  };
package/dist/index.d.cts CHANGED
@@ -42,30 +42,6 @@ type MessageLeaf = MessagePrimitive | MessageArray;
42
42
  * ```
43
43
  */
44
44
  type LocaleMessages = Record<string, MessageObject>;
45
- /**
46
- * Merges messages from all locales into a single unified structure,
47
- * or extracts messages for a specific locale if `L` is provided.
48
- *
49
- * @example
50
- * ```ts
51
- * const messages = {
52
- * en: { greeting: { morning: "morning" } },
53
- * zh: { greeting: { evening: "晚上好" } },
54
- * };
55
- *
56
- * // 1. Union of all locales
57
- * UnionLocaleMessages<typeof messages>;
58
- * // → { greeting: { morning: string; }; } | { greeting: { evening: string; }; }
59
- *
60
- * // 2. Messages for a specified locale
61
- * UnionLocaleMessages<typeof messages, "en">; // → { greeting: { morning: string; }; }
62
- * UnionLocaleMessages<typeof messages, "zh">; // → { greeting: { evening: string; }; }
63
- *
64
- * // 3. Fallback if M is not LocaleMessages
65
- * UnionLocaleMessages // → unknown
66
- * ```
67
- */
68
- type LocalizedMessagesUnion<M = unknown, L extends keyof M | "union" = "union"> = M extends LocaleMessages ? L extends "union" ? M[Locale<M>] : M[L & keyof M] : unknown;
69
45
 
70
46
  /**
71
47
  * Extracts locale keys only when M is a valid messages object.
@@ -107,23 +83,22 @@ type Locale<M = unknown> = M extends LocaleMessages ? keyof M & string : string;
107
83
  type FallbackLocalesMap<L extends string = string> = Partial<Record<L, L[]>>;
108
84
 
109
85
  /**
110
- * Represents a recursive replacement object used for interpolating values in message templates.
86
+ * Represents a replacement map used for interpolating values
87
+ * in message templates.
111
88
  *
112
- * - Each key can be any value (`string`, `number`, `boolean`, `Date`, `function`, nested object, etc.).
113
- * - Supports nested structures for complex interpolation.
114
- * - Can be used directly with `intl-messageformat` or custom format handlers.
89
+ * Replacement values are treated as plain data and interpreted
90
+ * by the message formatter at runtime.
115
91
  *
116
92
  * @example
117
93
  * const replacements: Replacement = {
118
94
  * name: "Alice",
119
95
  * count: 5,
120
96
  * nested: {
121
- * score: 100
97
+ * score: 100,
122
98
  * },
123
- * formatter: (value: unknown) => `<b>${value}</b>`
124
99
  * };
125
100
  */
126
- type Replacement = Record<string, unknown>;
101
+ type Replacement = Record<string, MessageValue>;
127
102
 
128
103
  /**
129
104
  * Default maximum recursive depth for nested key type computations,
@@ -164,6 +139,19 @@ type LeafKeys<M, D extends number = DefaultDepth> = [D] extends [never] ? never
164
139
  * ```
165
140
  */
166
141
  type LeafValue<M, K extends string> = K extends `${infer Seg}.${infer Rest}` ? Seg extends keyof M ? LeafValue<M[Seg], Rest> : never : K extends keyof M ? M[K] : never;
142
+ /**
143
+ * Resolves the type located at a dot-separated path.
144
+ *
145
+ * The resolved type may be a subtree or a leaf value.
146
+ *
147
+ * @example
148
+ * ```ts
149
+ * const messages = { a: { b: { c: "1" }, z: "2" } };
150
+ * AtPath<typeof messages, "a">; // → { b: { c: string }; z: string };
151
+ * AtPath<typeof messages, "a.b">; // → { c: string };
152
+ * ```
153
+ */
154
+ type AtPath<MessageSchema, PK extends string> = PK extends `${infer Head}.${infer Tail}` ? Head extends keyof MessageSchema ? AtPath<MessageSchema[Head], Tail> : never : PK extends keyof MessageSchema ? MessageSchema[PK] : never;
167
155
 
168
156
  /**
169
157
  * Extracts all **node keys** from the messages
@@ -187,7 +175,7 @@ type LeafValue<M, K extends string> = K extends `${infer Seg}.${infer Rest}` ? S
187
175
  * LocalizedNodeKeys // → string
188
176
  * ```
189
177
  */
190
- type LocalizedNodeKeys<M = unknown, L extends keyof M | "union" = "union", D extends number = DefaultDepth> = M extends LocaleMessages ? L extends "union" ? NodeKeys<M[keyof M], D> : NodeKeys<M[Extract<L, keyof M>], D> : string;
178
+ type LocalizedNodeKeys<M = unknown, D extends number = DefaultDepth> = M extends LocaleMessages ? NodeKeys<M[keyof M], D> : string;
191
179
  /**
192
180
  * Extracts all **leaf keys** from the messages
193
181
  * of a specified locale (or union of locales).
@@ -210,32 +198,15 @@ type LocalizedNodeKeys<M = unknown, L extends keyof M | "union" = "union", D ext
210
198
  * LocalizedLeafKeys // → string
211
199
  * ```
212
200
  */
213
- type LocalizedLeafKeys<M = unknown, L extends keyof M | "union" = "union", D extends number = DefaultDepth> = M extends LocaleMessages ? L extends "union" ? LeafKeys<M[keyof M], D> : LeafKeys<M[Extract<L, keyof M>], D> : string;
201
+ type LocalizedLeafKeys<M = unknown, D extends number = DefaultDepth> = M extends LocaleMessages ? LeafKeys<M[keyof M], D> : string;
214
202
  /**
215
203
  * Resolves the value type of a **localized leaf key**
216
204
  * from the messages of a specified locale (or union of locales).
217
205
  *
218
206
  * - Fallback to `MessageValue` if M is not LocaleMessages
219
207
  */
220
- type LocalizedLeafValue<M = unknown, K extends string = string, L extends keyof M | "union" = "union"> = M extends LocaleMessages ? L extends "union" ? LeafValue<M[keyof M], K> : LeafValue<M[Extract<L, keyof M>], K> : MessageValue;
208
+ type LocalizedLeafValue<M = unknown, K extends string = string> = M extends LocaleMessages ? LeafValue<M[keyof M], K> : MessageValue;
221
209
 
222
- /**
223
- * Resolves the type at a dot-separated key in a nested object.
224
- *
225
- * @example
226
- * ```ts
227
- * const structure = {
228
- * a: {
229
- * b: { c: "hello" },
230
- * z: "world",
231
- * },
232
- * };
233
- *
234
- * MessagesAtPreKey<typeof structure, "a"> // → { b: { c: string; }; z: string;}
235
- * MessagesAtPreKey<typeof structure, "a.b"> // → { c: string; };
236
- * ```
237
- */
238
- type MessagesAtPreKey<T, PK extends string> = PK extends `${infer Head}.${infer Tail}` ? Head extends keyof T ? MessagesAtPreKey<T[Head], Tail> : never : PK extends keyof T ? T[PK] : never;
239
210
  /**
240
211
  * Extracts all **leaf keys** under a scoped path (`PK`) from the messages
241
212
  * of a specified locale (`L`) (or union of locales).
@@ -252,7 +223,7 @@ type MessagesAtPreKey<T, PK extends string> = PK extends `${infer Head}.${infer
252
223
  * ScopedLeafKeys<typeof messages, "a", "zh">; // → "b"
253
224
  * ```
254
225
  */
255
- type ScopedLeafKeys<M, PK extends string, L extends keyof M | "union" = "union", D extends number = DefaultDepth> = M extends LocaleMessages ? LocalizedMessagesUnion<M, L> extends infer Messages ? Messages extends MessageValue ? MessagesAtPreKey<Messages, PK> extends infer Scoped ? Scoped extends MessageObject ? LeafKeys<Scoped, D> : never : never : never : never : string;
226
+ type ScopedLeafKeys<M, PK extends string, D extends number = DefaultDepth> = M extends LocaleMessages ? M[Locale<M>] extends infer Messages ? Messages extends MessageValue ? AtPath<Messages, PK> extends infer Scoped ? Scoped extends MessageObject ? LeafKeys<Scoped, D> : never : never : never : never : string;
256
227
  /**
257
228
  * Resolves the value type of a scoped leaf key (`K`)
258
229
  * under a prefix path (`PK`) from localized messages.
@@ -267,7 +238,65 @@ type ScopedLeafKeys<M, PK extends string, L extends keyof M | "union" = "union",
267
238
  * ScopedLeafValue<typeof messages, "a.b", "c">; // string
268
239
  * ```
269
240
  */
270
- type ScopedLeafValue<M, PK extends string, K extends string, L extends keyof M | "union" = "union"> = M extends LocaleMessages ? LocalizedMessagesUnion<M, L> extends infer Messages ? Messages extends MessageValue ? MessagesAtPreKey<Messages, PK> extends infer Scoped ? Scoped extends MessageObject ? LeafValue<Scoped, K> : never : never : never : never : MessageValue;
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;
242
+
243
+ /**
244
+ * Options for initializing a translator
245
+ *
246
+ * @template M - type of messages object
247
+ */
248
+ interface BaseTranslatorOptions<M = unknown> {
249
+ /**
250
+ * Messages object for translations.
251
+ * - Use `LocaleMessages` type to enable key inference for `hasKey` and `t`.
252
+ */
253
+ messages?: Readonly<M>;
254
+ /**
255
+ * Current locale key.
256
+ * - If `messages` is typed as `LocaleMessages`, this can be inferred automatically.
257
+ */
258
+ locale: Locale<M>;
259
+ /**
260
+ * Indicates whether the translator is in a loading state.
261
+ */
262
+ isLoading?: boolean;
263
+ }
264
+
265
+ /**
266
+ * The minimal, shared foundation for all translators.
267
+ *
268
+ * @template M - Shape of the messages object.
269
+ */
270
+ declare class BaseTranslator<M extends LocaleMessages | unknown = unknown> {
271
+ /** Current messages for translation */
272
+ protected _messages: Readonly<M>;
273
+ /** Current active locale */
274
+ protected _locale: Locale<M>;
275
+ /** Current loading state */
276
+ protected _isLoading: boolean;
277
+ constructor(options: BaseTranslatorOptions<M>);
278
+ /** Get messages. */
279
+ get messages(): M;
280
+ /** Get the current active locale. */
281
+ get locale(): Locale<M>;
282
+ /** Get the current loading state. */
283
+ get isLoading(): boolean;
284
+ /**
285
+ * Replace messages with new ones.
286
+ *
287
+ * - Note: This allows runtime setting of messages even if `M` is inferred as `never`.
288
+ * The type cast bypasses TypeScript restrictions on dynamic messages.
289
+ */
290
+ setMessages<N extends LocaleMessages>(messages: N): void;
291
+ /**
292
+ * Set the active locale.
293
+ *
294
+ * - Note: Unlike `setMessages`, the locale structure cannot be changed at runtime.
295
+ */
296
+ setLocale(newLocale: Locale<M>): void;
297
+ /** Set the loading state. */
298
+ setLoading(state: boolean): void;
299
+ }
271
300
 
272
301
  /**
273
302
  * Configuration options for translation behavior.
@@ -343,64 +372,6 @@ interface TranslateContext {
343
372
  */
344
373
  type TranslateHook = RuraHook<TranslateContext, MessageValue>;
345
374
 
346
- /**
347
- * Options for initializing a translator
348
- *
349
- * @template M - type of messages object
350
- */
351
- interface BaseTranslatorOptions<M = unknown> {
352
- /**
353
- * Messages object for translations.
354
- * - Use `LocaleMessages` type to enable key inference for `hasKey` and `t`.
355
- */
356
- messages?: Readonly<M>;
357
- /**
358
- * Current locale key.
359
- * - If `messages` is typed as `LocaleMessages`, this can be inferred automatically.
360
- */
361
- locale: Locale<M>;
362
- /**
363
- * Indicates whether the translator is in a loading state.
364
- */
365
- isLoading?: boolean;
366
- }
367
-
368
- /**
369
- * The minimal, shared foundation for all translators.
370
- *
371
- * @template M - Shape of the messages object.
372
- */
373
- declare class BaseTranslator<M extends LocaleMessages | unknown = unknown> {
374
- /** Current messages for translation */
375
- protected _messages: Readonly<M>;
376
- /** Current active locale */
377
- protected _locale: Locale<M>;
378
- /** Current loading state */
379
- protected _isLoading: boolean;
380
- constructor(options: BaseTranslatorOptions<M>);
381
- /** Get messages. */
382
- get messages(): M;
383
- /** Get the current active locale. */
384
- get locale(): Locale<M>;
385
- /** Get the current loading state. */
386
- get isLoading(): boolean;
387
- /**
388
- * Replace messages with new ones.
389
- *
390
- * - Note: This allows runtime setting of messages even if `M` is inferred as `never`.
391
- * The type cast bypasses TypeScript restrictions on dynamic messages.
392
- */
393
- setMessages<N extends LocaleMessages>(messages: N): void;
394
- /**
395
- * Set the active locale.
396
- *
397
- * - Note: Unlike `setMessages`, the locale structure cannot be changed at runtime.
398
- */
399
- setLocale(newLocale: Locale<M>): void;
400
- /** Set the loading state. */
401
- setLoading(state: boolean): void;
402
- }
403
-
404
375
  interface CoreTranslatorOptions<M> extends BaseTranslatorOptions<M>, TranslateConfig<M> {
405
376
  /** Optional plugins or raw hooks to extend the translation pipeline */
406
377
  plugins?: Array<TranslatorPlugin | TranslateHook>;
@@ -417,9 +388,8 @@ interface TranslatorPlugin {
417
388
  * using the pipeline engine and built-in hooks.
418
389
  *
419
390
  * @template M - Shape of the messages object.
420
- * @template L - Locale selection strategy ("union" or specific locale keys).
421
391
  */
422
- declare class CoreTranslator<M extends LocaleMessages | unknown = unknown, L extends keyof M | "union" = "union"> extends BaseTranslator<M> {
392
+ declare class CoreTranslator<M extends LocaleMessages | unknown = unknown> extends BaseTranslator<M> {
423
393
  /** User-provided options including messages, locale, and config. */
424
394
  protected translateConfig: TranslateConfig<M>;
425
395
  /** Active pipeline hooks applied during translation. */
@@ -432,21 +402,21 @@ declare class CoreTranslator<M extends LocaleMessages | unknown = unknown, L ext
432
402
  /** Outputs a debug overview of the active pipeline. */
433
403
  debugHooks(): void;
434
404
  /** Check if a key exists in the specified locale or current locale. */
435
- hasKey: <K extends LocalizedLeafKeys<M, L>>(key: K, targetLocale?: Locale<M>) => boolean;
405
+ hasKey: <K extends LocalizedLeafKeys<M>>(key: K, targetLocale?: Locale<M>) => boolean;
436
406
  /** Get the translated message for a key, with optional replacements. */
437
- t: <K extends LocalizedLeafKeys<M, L> = LocalizedLeafKeys<M, L>>(key: K, replacements?: Replacement) => LocalizedLeafValue<M, K, L>;
407
+ t: <K extends LocalizedLeafKeys<M> = LocalizedLeafKeys<M>>(key: K, replacements?: Replacement) => LocalizedLeafValue<M, K>;
438
408
  }
439
409
 
440
410
  type ScopeTranslatorOptions<M> = CoreTranslatorOptions<M>;
441
- type ScopeTranslatorMethods<M extends LocaleMessages | unknown = unknown, L extends keyof M | "union" = "union", PK extends string | undefined = undefined, K extends string = PK extends string ? ScopedLeafKeys<M, PK, L> : LocalizedLeafKeys<M, L>> = {
411
+ type ScopeTranslatorMethods<M extends LocaleMessages | unknown = unknown, PK extends string | undefined = undefined, K extends string = PK extends string ? ScopedLeafKeys<M, PK> : LocalizedLeafKeys<M>> = {
442
412
  hasKey: (key?: K, targetLocale?: Locale<M>) => boolean;
443
- t: <Key extends K>(key?: Key, replacements?: Replacement) => PK extends string ? ScopedLeafValue<M, PK, Key, L> : LocalizedLeafValue<M, Key, L>;
413
+ t: (key?: K, replacements?: Replacement) => PK extends string ? ScopedLeafValue<M, PK, K> : LocalizedLeafValue<M, K>;
444
414
  };
445
415
 
446
- declare class ScopeTranslator<M extends LocaleMessages | unknown = unknown, L extends keyof M | "union" = "union"> extends CoreTranslator<M> {
416
+ declare class ScopeTranslator<M extends LocaleMessages | unknown = unknown> extends CoreTranslator<M> {
447
417
  constructor(options: ScopeTranslatorOptions<M>);
448
418
  /** Create a scoped translator with a prefix key for resolving nested message paths. */
449
- scoped<PK extends LocalizedNodeKeys<M, L> | undefined = undefined>(preKey?: PK): PK extends string ? ScopeTranslatorMethods<M, L, PK> : ScopeTranslatorMethods<M, L>;
419
+ scoped<PK extends LocalizedNodeKeys<M> | undefined = undefined>(preKey?: PK): PK extends string ? ScopeTranslatorMethods<M, PK> : ScopeTranslatorMethods<M>;
450
420
  }
451
421
 
452
422
  /** Semantic tag attributes map. */
@@ -521,4 +491,4 @@ interface Renderer<Output> {
521
491
  */
522
492
  declare function renderRichMessage<Output>(message: MessageValue, renderer: Renderer<Output>): Output[];
523
493
 
524
- export { type ASTNode, 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 LocalizedMessagesUnion, 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 };
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
@@ -42,30 +42,6 @@ type MessageLeaf = MessagePrimitive | MessageArray;
42
42
  * ```
43
43
  */
44
44
  type LocaleMessages = Record<string, MessageObject>;
45
- /**
46
- * Merges messages from all locales into a single unified structure,
47
- * or extracts messages for a specific locale if `L` is provided.
48
- *
49
- * @example
50
- * ```ts
51
- * const messages = {
52
- * en: { greeting: { morning: "morning" } },
53
- * zh: { greeting: { evening: "晚上好" } },
54
- * };
55
- *
56
- * // 1. Union of all locales
57
- * UnionLocaleMessages<typeof messages>;
58
- * // → { greeting: { morning: string; }; } | { greeting: { evening: string; }; }
59
- *
60
- * // 2. Messages for a specified locale
61
- * UnionLocaleMessages<typeof messages, "en">; // → { greeting: { morning: string; }; }
62
- * UnionLocaleMessages<typeof messages, "zh">; // → { greeting: { evening: string; }; }
63
- *
64
- * // 3. Fallback if M is not LocaleMessages
65
- * UnionLocaleMessages // → unknown
66
- * ```
67
- */
68
- type LocalizedMessagesUnion<M = unknown, L extends keyof M | "union" = "union"> = M extends LocaleMessages ? L extends "union" ? M[Locale<M>] : M[L & keyof M] : unknown;
69
45
 
70
46
  /**
71
47
  * Extracts locale keys only when M is a valid messages object.
@@ -107,23 +83,22 @@ type Locale<M = unknown> = M extends LocaleMessages ? keyof M & string : string;
107
83
  type FallbackLocalesMap<L extends string = string> = Partial<Record<L, L[]>>;
108
84
 
109
85
  /**
110
- * Represents a recursive replacement object used for interpolating values in message templates.
86
+ * Represents a replacement map used for interpolating values
87
+ * in message templates.
111
88
  *
112
- * - Each key can be any value (`string`, `number`, `boolean`, `Date`, `function`, nested object, etc.).
113
- * - Supports nested structures for complex interpolation.
114
- * - Can be used directly with `intl-messageformat` or custom format handlers.
89
+ * Replacement values are treated as plain data and interpreted
90
+ * by the message formatter at runtime.
115
91
  *
116
92
  * @example
117
93
  * const replacements: Replacement = {
118
94
  * name: "Alice",
119
95
  * count: 5,
120
96
  * nested: {
121
- * score: 100
97
+ * score: 100,
122
98
  * },
123
- * formatter: (value: unknown) => `<b>${value}</b>`
124
99
  * };
125
100
  */
126
- type Replacement = Record<string, unknown>;
101
+ type Replacement = Record<string, MessageValue>;
127
102
 
128
103
  /**
129
104
  * Default maximum recursive depth for nested key type computations,
@@ -164,6 +139,19 @@ type LeafKeys<M, D extends number = DefaultDepth> = [D] extends [never] ? never
164
139
  * ```
165
140
  */
166
141
  type LeafValue<M, K extends string> = K extends `${infer Seg}.${infer Rest}` ? Seg extends keyof M ? LeafValue<M[Seg], Rest> : never : K extends keyof M ? M[K] : never;
142
+ /**
143
+ * Resolves the type located at a dot-separated path.
144
+ *
145
+ * The resolved type may be a subtree or a leaf value.
146
+ *
147
+ * @example
148
+ * ```ts
149
+ * const messages = { a: { b: { c: "1" }, z: "2" } };
150
+ * AtPath<typeof messages, "a">; // → { b: { c: string }; z: string };
151
+ * AtPath<typeof messages, "a.b">; // → { c: string };
152
+ * ```
153
+ */
154
+ type AtPath<MessageSchema, PK extends string> = PK extends `${infer Head}.${infer Tail}` ? Head extends keyof MessageSchema ? AtPath<MessageSchema[Head], Tail> : never : PK extends keyof MessageSchema ? MessageSchema[PK] : never;
167
155
 
168
156
  /**
169
157
  * Extracts all **node keys** from the messages
@@ -187,7 +175,7 @@ type LeafValue<M, K extends string> = K extends `${infer Seg}.${infer Rest}` ? S
187
175
  * LocalizedNodeKeys // → string
188
176
  * ```
189
177
  */
190
- type LocalizedNodeKeys<M = unknown, L extends keyof M | "union" = "union", D extends number = DefaultDepth> = M extends LocaleMessages ? L extends "union" ? NodeKeys<M[keyof M], D> : NodeKeys<M[Extract<L, keyof M>], D> : string;
178
+ type LocalizedNodeKeys<M = unknown, D extends number = DefaultDepth> = M extends LocaleMessages ? NodeKeys<M[keyof M], D> : string;
191
179
  /**
192
180
  * Extracts all **leaf keys** from the messages
193
181
  * of a specified locale (or union of locales).
@@ -210,32 +198,15 @@ type LocalizedNodeKeys<M = unknown, L extends keyof M | "union" = "union", D ext
210
198
  * LocalizedLeafKeys // → string
211
199
  * ```
212
200
  */
213
- type LocalizedLeafKeys<M = unknown, L extends keyof M | "union" = "union", D extends number = DefaultDepth> = M extends LocaleMessages ? L extends "union" ? LeafKeys<M[keyof M], D> : LeafKeys<M[Extract<L, keyof M>], D> : string;
201
+ type LocalizedLeafKeys<M = unknown, D extends number = DefaultDepth> = M extends LocaleMessages ? LeafKeys<M[keyof M], D> : string;
214
202
  /**
215
203
  * Resolves the value type of a **localized leaf key**
216
204
  * from the messages of a specified locale (or union of locales).
217
205
  *
218
206
  * - Fallback to `MessageValue` if M is not LocaleMessages
219
207
  */
220
- type LocalizedLeafValue<M = unknown, K extends string = string, L extends keyof M | "union" = "union"> = M extends LocaleMessages ? L extends "union" ? LeafValue<M[keyof M], K> : LeafValue<M[Extract<L, keyof M>], K> : MessageValue;
208
+ type LocalizedLeafValue<M = unknown, K extends string = string> = M extends LocaleMessages ? LeafValue<M[keyof M], K> : MessageValue;
221
209
 
222
- /**
223
- * Resolves the type at a dot-separated key in a nested object.
224
- *
225
- * @example
226
- * ```ts
227
- * const structure = {
228
- * a: {
229
- * b: { c: "hello" },
230
- * z: "world",
231
- * },
232
- * };
233
- *
234
- * MessagesAtPreKey<typeof structure, "a"> // → { b: { c: string; }; z: string;}
235
- * MessagesAtPreKey<typeof structure, "a.b"> // → { c: string; };
236
- * ```
237
- */
238
- type MessagesAtPreKey<T, PK extends string> = PK extends `${infer Head}.${infer Tail}` ? Head extends keyof T ? MessagesAtPreKey<T[Head], Tail> : never : PK extends keyof T ? T[PK] : never;
239
210
  /**
240
211
  * Extracts all **leaf keys** under a scoped path (`PK`) from the messages
241
212
  * of a specified locale (`L`) (or union of locales).
@@ -252,7 +223,7 @@ type MessagesAtPreKey<T, PK extends string> = PK extends `${infer Head}.${infer
252
223
  * ScopedLeafKeys<typeof messages, "a", "zh">; // → "b"
253
224
  * ```
254
225
  */
255
- type ScopedLeafKeys<M, PK extends string, L extends keyof M | "union" = "union", D extends number = DefaultDepth> = M extends LocaleMessages ? LocalizedMessagesUnion<M, L> extends infer Messages ? Messages extends MessageValue ? MessagesAtPreKey<Messages, PK> extends infer Scoped ? Scoped extends MessageObject ? LeafKeys<Scoped, D> : never : never : never : never : string;
226
+ type ScopedLeafKeys<M, PK extends string, D extends number = DefaultDepth> = M extends LocaleMessages ? M[Locale<M>] extends infer Messages ? Messages extends MessageValue ? AtPath<Messages, PK> extends infer Scoped ? Scoped extends MessageObject ? LeafKeys<Scoped, D> : never : never : never : never : string;
256
227
  /**
257
228
  * Resolves the value type of a scoped leaf key (`K`)
258
229
  * under a prefix path (`PK`) from localized messages.
@@ -267,7 +238,65 @@ type ScopedLeafKeys<M, PK extends string, L extends keyof M | "union" = "union",
267
238
  * ScopedLeafValue<typeof messages, "a.b", "c">; // string
268
239
  * ```
269
240
  */
270
- type ScopedLeafValue<M, PK extends string, K extends string, L extends keyof M | "union" = "union"> = M extends LocaleMessages ? LocalizedMessagesUnion<M, L> extends infer Messages ? Messages extends MessageValue ? MessagesAtPreKey<Messages, PK> extends infer Scoped ? Scoped extends MessageObject ? LeafValue<Scoped, K> : never : never : never : never : MessageValue;
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;
242
+
243
+ /**
244
+ * Options for initializing a translator
245
+ *
246
+ * @template M - type of messages object
247
+ */
248
+ interface BaseTranslatorOptions<M = unknown> {
249
+ /**
250
+ * Messages object for translations.
251
+ * - Use `LocaleMessages` type to enable key inference for `hasKey` and `t`.
252
+ */
253
+ messages?: Readonly<M>;
254
+ /**
255
+ * Current locale key.
256
+ * - If `messages` is typed as `LocaleMessages`, this can be inferred automatically.
257
+ */
258
+ locale: Locale<M>;
259
+ /**
260
+ * Indicates whether the translator is in a loading state.
261
+ */
262
+ isLoading?: boolean;
263
+ }
264
+
265
+ /**
266
+ * The minimal, shared foundation for all translators.
267
+ *
268
+ * @template M - Shape of the messages object.
269
+ */
270
+ declare class BaseTranslator<M extends LocaleMessages | unknown = unknown> {
271
+ /** Current messages for translation */
272
+ protected _messages: Readonly<M>;
273
+ /** Current active locale */
274
+ protected _locale: Locale<M>;
275
+ /** Current loading state */
276
+ protected _isLoading: boolean;
277
+ constructor(options: BaseTranslatorOptions<M>);
278
+ /** Get messages. */
279
+ get messages(): M;
280
+ /** Get the current active locale. */
281
+ get locale(): Locale<M>;
282
+ /** Get the current loading state. */
283
+ get isLoading(): boolean;
284
+ /**
285
+ * Replace messages with new ones.
286
+ *
287
+ * - Note: This allows runtime setting of messages even if `M` is inferred as `never`.
288
+ * The type cast bypasses TypeScript restrictions on dynamic messages.
289
+ */
290
+ setMessages<N extends LocaleMessages>(messages: N): void;
291
+ /**
292
+ * Set the active locale.
293
+ *
294
+ * - Note: Unlike `setMessages`, the locale structure cannot be changed at runtime.
295
+ */
296
+ setLocale(newLocale: Locale<M>): void;
297
+ /** Set the loading state. */
298
+ setLoading(state: boolean): void;
299
+ }
271
300
 
272
301
  /**
273
302
  * Configuration options for translation behavior.
@@ -343,64 +372,6 @@ interface TranslateContext {
343
372
  */
344
373
  type TranslateHook = RuraHook<TranslateContext, MessageValue>;
345
374
 
346
- /**
347
- * Options for initializing a translator
348
- *
349
- * @template M - type of messages object
350
- */
351
- interface BaseTranslatorOptions<M = unknown> {
352
- /**
353
- * Messages object for translations.
354
- * - Use `LocaleMessages` type to enable key inference for `hasKey` and `t`.
355
- */
356
- messages?: Readonly<M>;
357
- /**
358
- * Current locale key.
359
- * - If `messages` is typed as `LocaleMessages`, this can be inferred automatically.
360
- */
361
- locale: Locale<M>;
362
- /**
363
- * Indicates whether the translator is in a loading state.
364
- */
365
- isLoading?: boolean;
366
- }
367
-
368
- /**
369
- * The minimal, shared foundation for all translators.
370
- *
371
- * @template M - Shape of the messages object.
372
- */
373
- declare class BaseTranslator<M extends LocaleMessages | unknown = unknown> {
374
- /** Current messages for translation */
375
- protected _messages: Readonly<M>;
376
- /** Current active locale */
377
- protected _locale: Locale<M>;
378
- /** Current loading state */
379
- protected _isLoading: boolean;
380
- constructor(options: BaseTranslatorOptions<M>);
381
- /** Get messages. */
382
- get messages(): M;
383
- /** Get the current active locale. */
384
- get locale(): Locale<M>;
385
- /** Get the current loading state. */
386
- get isLoading(): boolean;
387
- /**
388
- * Replace messages with new ones.
389
- *
390
- * - Note: This allows runtime setting of messages even if `M` is inferred as `never`.
391
- * The type cast bypasses TypeScript restrictions on dynamic messages.
392
- */
393
- setMessages<N extends LocaleMessages>(messages: N): void;
394
- /**
395
- * Set the active locale.
396
- *
397
- * - Note: Unlike `setMessages`, the locale structure cannot be changed at runtime.
398
- */
399
- setLocale(newLocale: Locale<M>): void;
400
- /** Set the loading state. */
401
- setLoading(state: boolean): void;
402
- }
403
-
404
375
  interface CoreTranslatorOptions<M> extends BaseTranslatorOptions<M>, TranslateConfig<M> {
405
376
  /** Optional plugins or raw hooks to extend the translation pipeline */
406
377
  plugins?: Array<TranslatorPlugin | TranslateHook>;
@@ -417,9 +388,8 @@ interface TranslatorPlugin {
417
388
  * using the pipeline engine and built-in hooks.
418
389
  *
419
390
  * @template M - Shape of the messages object.
420
- * @template L - Locale selection strategy ("union" or specific locale keys).
421
391
  */
422
- declare class CoreTranslator<M extends LocaleMessages | unknown = unknown, L extends keyof M | "union" = "union"> extends BaseTranslator<M> {
392
+ declare class CoreTranslator<M extends LocaleMessages | unknown = unknown> extends BaseTranslator<M> {
423
393
  /** User-provided options including messages, locale, and config. */
424
394
  protected translateConfig: TranslateConfig<M>;
425
395
  /** Active pipeline hooks applied during translation. */
@@ -432,21 +402,21 @@ declare class CoreTranslator<M extends LocaleMessages | unknown = unknown, L ext
432
402
  /** Outputs a debug overview of the active pipeline. */
433
403
  debugHooks(): void;
434
404
  /** Check if a key exists in the specified locale or current locale. */
435
- hasKey: <K extends LocalizedLeafKeys<M, L>>(key: K, targetLocale?: Locale<M>) => boolean;
405
+ hasKey: <K extends LocalizedLeafKeys<M>>(key: K, targetLocale?: Locale<M>) => boolean;
436
406
  /** Get the translated message for a key, with optional replacements. */
437
- t: <K extends LocalizedLeafKeys<M, L> = LocalizedLeafKeys<M, L>>(key: K, replacements?: Replacement) => LocalizedLeafValue<M, K, L>;
407
+ t: <K extends LocalizedLeafKeys<M> = LocalizedLeafKeys<M>>(key: K, replacements?: Replacement) => LocalizedLeafValue<M, K>;
438
408
  }
439
409
 
440
410
  type ScopeTranslatorOptions<M> = CoreTranslatorOptions<M>;
441
- type ScopeTranslatorMethods<M extends LocaleMessages | unknown = unknown, L extends keyof M | "union" = "union", PK extends string | undefined = undefined, K extends string = PK extends string ? ScopedLeafKeys<M, PK, L> : LocalizedLeafKeys<M, L>> = {
411
+ type ScopeTranslatorMethods<M extends LocaleMessages | unknown = unknown, PK extends string | undefined = undefined, K extends string = PK extends string ? ScopedLeafKeys<M, PK> : LocalizedLeafKeys<M>> = {
442
412
  hasKey: (key?: K, targetLocale?: Locale<M>) => boolean;
443
- t: <Key extends K>(key?: Key, replacements?: Replacement) => PK extends string ? ScopedLeafValue<M, PK, Key, L> : LocalizedLeafValue<M, Key, L>;
413
+ t: (key?: K, replacements?: Replacement) => PK extends string ? ScopedLeafValue<M, PK, K> : LocalizedLeafValue<M, K>;
444
414
  };
445
415
 
446
- declare class ScopeTranslator<M extends LocaleMessages | unknown = unknown, L extends keyof M | "union" = "union"> extends CoreTranslator<M> {
416
+ declare class ScopeTranslator<M extends LocaleMessages | unknown = unknown> extends CoreTranslator<M> {
447
417
  constructor(options: ScopeTranslatorOptions<M>);
448
418
  /** Create a scoped translator with a prefix key for resolving nested message paths. */
449
- scoped<PK extends LocalizedNodeKeys<M, L> | undefined = undefined>(preKey?: PK): PK extends string ? ScopeTranslatorMethods<M, L, PK> : ScopeTranslatorMethods<M, L>;
419
+ scoped<PK extends LocalizedNodeKeys<M> | undefined = undefined>(preKey?: PK): PK extends string ? ScopeTranslatorMethods<M, PK> : ScopeTranslatorMethods<M>;
450
420
  }
451
421
 
452
422
  /** Semantic tag attributes map. */
@@ -521,4 +491,4 @@ interface Renderer<Output> {
521
491
  */
522
492
  declare function renderRichMessage<Output>(message: MessageValue, renderer: Renderer<Output>): Output[];
523
493
 
524
- export { type ASTNode, 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 LocalizedMessagesUnion, 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 };
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
@@ -1,6 +1,15 @@
1
1
  import { rura } from 'rura';
2
2
 
3
3
  // src/translators/core-translator/core-translator.ts
4
+ function runTranslate(options) {
5
+ const context = {
6
+ ...options,
7
+ config: options.translateConfig,
8
+ candidateLocales: [],
9
+ meta: {}
10
+ };
11
+ return rura.run(context, options.hooks);
12
+ }
4
13
 
5
14
  // src/shared/utils/find-message-in-locales.ts
6
15
  var findMessageInLocales = ({
@@ -229,15 +238,6 @@ var hasKey = ({
229
238
  const message = findMessageInLocales({ messages, candidateLocales, key });
230
239
  return message !== void 0;
231
240
  };
232
- function runTranslate(options) {
233
- const context = {
234
- ...options,
235
- config: options.translateConfig,
236
- candidateLocales: [],
237
- meta: {}
238
- };
239
- return rura.run(context, options.hooks);
240
- }
241
241
 
242
242
  // src/translators/methods/translate.ts
243
243
  function translate(options) {
@@ -330,7 +330,7 @@ var ScopeTranslator = class extends CoreTranslator {
330
330
  targetLocale
331
331
  });
332
332
  },
333
- t: (key, replacements) => {
333
+ t: (key, ...args) => {
334
334
  const fullKey = getFullKey(preKey, key);
335
335
  return translate({
336
336
  hooks: this.hooks,
@@ -339,7 +339,7 @@ var ScopeTranslator = class extends CoreTranslator {
339
339
  isLoading: this._isLoading,
340
340
  translateConfig: this.translateConfig,
341
341
  key: fullKey,
342
- replacements
342
+ replacements: args[0]
343
343
  });
344
344
  }
345
345
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "intor-translator",
3
- "version": "1.4.6",
3
+ "version": "1.4.8",
4
4
  "description": "🤖 A modern, type-safe i18n engine.",
5
5
  "author": {
6
6
  "name": "Yiming Liao",