intor-translator 1.1.5 → 1.2.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.
- package/README.md +34 -142
- package/dist/index.cjs +206 -85
- package/dist/index.d.cts +92 -81
- package/dist/index.d.ts +92 -81
- package/dist/index.js +207 -85
- package/package.json +15 -23
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { RuraHook } from 'rura';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* A nested message structure or a simple string message.
|
|
3
5
|
*
|
|
@@ -186,7 +188,7 @@ type LeafKeys<M, D extends number = DefaultDepth> = [D] extends [never] ? never
|
|
|
186
188
|
* LocalizedNodeKeys // → string
|
|
187
189
|
* ```
|
|
188
190
|
*/
|
|
189
|
-
type LocalizedNodeKeys<M =
|
|
191
|
+
type LocalizedNodeKeys<M extends LocaleMessages | undefined = undefined, L extends keyof M | "union" = "union", D extends number = DefaultDepth> = [M] extends [undefined] ? string : L extends "union" ? NodeKeys<M[keyof M], D> : NodeKeys<M[Extract<L, keyof M>], D>;
|
|
190
192
|
/**
|
|
191
193
|
* Extracts all **leaf keys** from the messages
|
|
192
194
|
* of a specified locale (or union of locales).
|
|
@@ -209,7 +211,7 @@ type LocalizedNodeKeys<M = unknown, L extends keyof M | "union" = "union", D ext
|
|
|
209
211
|
* LocalizedLeafKeys // → string
|
|
210
212
|
* ```
|
|
211
213
|
*/
|
|
212
|
-
type LocalizedLeafKeys<M =
|
|
214
|
+
type LocalizedLeafKeys<M extends LocaleMessages | undefined = undefined, L extends keyof M | "union" = "union", D extends number = DefaultDepth> = [M] extends [undefined] ? string : L extends "union" ? LeafKeys<M[keyof M], D> : LeafKeys<M[Extract<L, keyof M>], D>;
|
|
213
215
|
|
|
214
216
|
/**
|
|
215
217
|
* Resolves the type at a dot-separated key in a nested object.
|
|
@@ -246,51 +248,10 @@ type MessagesAtPreKey<T, PK extends string> = PK extends `${infer Head}.${infer
|
|
|
246
248
|
*/
|
|
247
249
|
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 NestedMessage ? LeafKeys<MessagesAtPreKey<messages, PK>, D> : never : never : string;
|
|
248
250
|
|
|
249
|
-
/**
|
|
250
|
-
* A ref object holding all localized messages by locale.
|
|
251
|
-
*
|
|
252
|
-
* @example
|
|
253
|
-
* ```ts
|
|
254
|
-
* const messagesRef: MessagesRef<Messages> = {
|
|
255
|
-
* current: {
|
|
256
|
-
* en: { home: { title: "Welcome" } },
|
|
257
|
-
* zh: { home: { title: "歡迎" } }
|
|
258
|
-
* }
|
|
259
|
-
* };
|
|
260
|
-
* ```
|
|
261
|
-
*/
|
|
262
|
-
type MessagesRef<M = unknown> = {
|
|
263
|
-
current: Readonly<M>;
|
|
264
|
-
};
|
|
265
|
-
/**
|
|
266
|
-
* A ref object holding the currently selected locale.
|
|
267
|
-
*
|
|
268
|
-
* @example
|
|
269
|
-
* ```ts
|
|
270
|
-
* const localeRef: LocaleRef<Messages> = {
|
|
271
|
-
* current: "en"
|
|
272
|
-
* };
|
|
273
|
-
* ```
|
|
274
|
-
*/
|
|
275
|
-
type LocaleRef<M = unknown> = {
|
|
276
|
-
current: Locale<M>;
|
|
277
|
-
};
|
|
278
|
-
/**
|
|
279
|
-
* A ref object indicating whether translation is loading.
|
|
280
|
-
*
|
|
281
|
-
* @example
|
|
282
|
-
* ```ts
|
|
283
|
-
* const isLoadingRef: IsLoadingRef = {
|
|
284
|
-
* current: true
|
|
285
|
-
* };
|
|
286
|
-
* ```
|
|
287
|
-
*/
|
|
288
|
-
type IsLoadingRef = {
|
|
289
|
-
current: boolean;
|
|
290
|
-
};
|
|
291
|
-
|
|
292
251
|
/**
|
|
293
252
|
* Configuration options for translation behavior.
|
|
253
|
+
*
|
|
254
|
+
* @template M - Messages map type.
|
|
294
255
|
*/
|
|
295
256
|
type TranslateConfig<M = unknown> = {
|
|
296
257
|
/** Optional mapping of fallback locales to use when a message is missing in the current locale. */
|
|
@@ -302,43 +263,66 @@ type TranslateConfig<M = unknown> = {
|
|
|
302
263
|
/** Optional set of handler functions for customizing translation behavior. */
|
|
303
264
|
handlers?: TranslateHandlers;
|
|
304
265
|
};
|
|
305
|
-
/**
|
|
306
|
-
* Optional handler functions for customizing translation behavior.
|
|
307
|
-
*/
|
|
266
|
+
/** Optional handler functions for customizing translation behavior. */
|
|
308
267
|
type TranslateHandlers = {
|
|
309
|
-
/** Function to format a resolved message before returning it. */
|
|
310
|
-
formatHandler?: FormatHandler;
|
|
311
268
|
/** Function called when a translation is still loading. */
|
|
312
269
|
loadingHandler?: LoadingHandler;
|
|
313
270
|
/** Function called when no message is found for a given key. */
|
|
314
271
|
missingHandler?: MissingHandler;
|
|
272
|
+
/** Function to format a resolved message before returning it. */
|
|
273
|
+
formatHandler?: FormatHandler;
|
|
315
274
|
};
|
|
316
|
-
/** Function to format a resolved message. */
|
|
317
|
-
type FormatHandler<Result = unknown> = (ctx: TranslateHandlerContext & {
|
|
318
|
-
message: string;
|
|
319
|
-
}) => Result;
|
|
320
275
|
/** Function called when translation is still loading. */
|
|
321
|
-
type LoadingHandler<Result = unknown> = (ctx:
|
|
276
|
+
type LoadingHandler<Result = unknown> = (ctx: HandlerContext) => Result;
|
|
322
277
|
/** Function called when no message is found for the given key. */
|
|
323
|
-
type MissingHandler<Result = unknown> = (ctx:
|
|
278
|
+
type MissingHandler<Result = unknown> = (ctx: HandlerContext) => Result;
|
|
279
|
+
/** Function to format a resolved message. */
|
|
280
|
+
type FormatHandler<Result = unknown> = (ctx: HandlerContext & {
|
|
281
|
+
rawMessage: string;
|
|
282
|
+
}) => Result;
|
|
324
283
|
/**
|
|
325
|
-
*
|
|
284
|
+
* Snapshot of the translate pipeline context exposed to handlers.
|
|
326
285
|
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
*
|
|
331
|
-
* replacements: { name: "John" }
|
|
332
|
-
* }
|
|
286
|
+
* This is a readonly view of TranslateContext:
|
|
287
|
+
* - Handlers can inspect the translation state
|
|
288
|
+
* - Handlers must NOT modify it
|
|
289
|
+
* - Pipeline will not be affected by handler changes
|
|
333
290
|
*/
|
|
334
|
-
type
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
291
|
+
type HandlerContext = Readonly<Omit<TranslateContext, "finalMessage">>;
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Context object shared across the translate pipeline.
|
|
295
|
+
*
|
|
296
|
+
* @template Result - Final translated value type.
|
|
297
|
+
*/
|
|
298
|
+
interface TranslateContext<Result = unknown> {
|
|
299
|
+
/** Configuration for the translate pipeline. */
|
|
300
|
+
config: TranslateConfig;
|
|
301
|
+
/** Current messages for translation */
|
|
302
|
+
messages: LocaleMessages;
|
|
303
|
+
/** Current active locale */
|
|
304
|
+
locale: string;
|
|
305
|
+
/** Current loading state */
|
|
306
|
+
isLoading?: boolean;
|
|
307
|
+
/** Message key to look up in the messages map */
|
|
338
308
|
key: string;
|
|
339
|
-
/** Optional replacements
|
|
309
|
+
/** Optional value replacements */
|
|
340
310
|
replacements?: Replacement;
|
|
341
|
-
|
|
311
|
+
/** Ordered list of locales to try, including fallbacks */
|
|
312
|
+
candidateLocales: string[];
|
|
313
|
+
/** Raw message string found in the messages map, before formatting */
|
|
314
|
+
rawMessage?: string;
|
|
315
|
+
/** Message after formatting (e.g. ICU, custom formatters) */
|
|
316
|
+
formattedMessage?: unknown;
|
|
317
|
+
/** Final value produced by the pipeline */
|
|
318
|
+
finalMessage?: Result;
|
|
319
|
+
/** Free-form metadata shared between hooks. */
|
|
320
|
+
meta: Record<string, unknown>;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* A single step in the translate pipeline.
|
|
324
|
+
*/
|
|
325
|
+
type TranslateHook = RuraHook<TranslateContext>;
|
|
342
326
|
|
|
343
327
|
/**
|
|
344
328
|
* Options for initializing a translator
|
|
@@ -362,16 +346,21 @@ interface BaseTranslatorOptions<M = unknown> {
|
|
|
362
346
|
isLoading?: boolean;
|
|
363
347
|
}
|
|
364
348
|
|
|
365
|
-
|
|
349
|
+
/**
|
|
350
|
+
* The minimal, shared foundation for all translators.
|
|
351
|
+
*
|
|
352
|
+
* @template M - Shape of the messages object.
|
|
353
|
+
*/
|
|
354
|
+
declare class BaseTranslator<M extends LocaleMessages> {
|
|
366
355
|
/** Current messages for translation */
|
|
367
|
-
protected
|
|
356
|
+
protected _messages: Readonly<M>;
|
|
368
357
|
/** Current active locale */
|
|
369
|
-
protected
|
|
358
|
+
protected _locale: Locale<M>;
|
|
370
359
|
/** Current loading state */
|
|
371
|
-
protected
|
|
360
|
+
protected _isLoading: boolean;
|
|
372
361
|
constructor(options: BaseTranslatorOptions<M>);
|
|
373
362
|
/** Get messages. */
|
|
374
|
-
get messages(): M
|
|
363
|
+
get messages(): M;
|
|
375
364
|
/** Get the current active locale. */
|
|
376
365
|
get locale(): Locale<M>;
|
|
377
366
|
/** Get the current loading state. */
|
|
@@ -394,27 +383,49 @@ declare class BaseTranslator<M = unknown> {
|
|
|
394
383
|
}
|
|
395
384
|
|
|
396
385
|
interface CoreTranslatorOptions<M> extends BaseTranslatorOptions<M>, TranslateConfig<M> {
|
|
386
|
+
/** Optional plugins or raw hooks to extend the translation pipeline */
|
|
387
|
+
plugins?: Array<TranslatorPlugin | TranslateHook>;
|
|
388
|
+
}
|
|
389
|
+
interface TranslatorPlugin {
|
|
390
|
+
/** Optional name for debugging or ordering hints */
|
|
391
|
+
name?: string;
|
|
392
|
+
/** One or multiple hooks contributed by this plugin */
|
|
393
|
+
hook?: TranslateHook | TranslateHook[];
|
|
397
394
|
}
|
|
398
395
|
|
|
399
|
-
|
|
400
|
-
|
|
396
|
+
/**
|
|
397
|
+
* CoreTranslator provides the default translation behavior
|
|
398
|
+
* using the pipeline engine and built-in hooks.
|
|
399
|
+
*
|
|
400
|
+
* @template M - Shape of the messages object.
|
|
401
|
+
* @template L - Locale selection strategy ("union" or specific locale keys).
|
|
402
|
+
*/
|
|
403
|
+
declare class CoreTranslator<M extends LocaleMessages, L extends keyof M | "union" = "union"> extends BaseTranslator<M> {
|
|
404
|
+
/** User-provided options including messages, locale, and config. */
|
|
405
|
+
protected translateConfig: TranslateConfig<M>;
|
|
406
|
+
/** Active pipeline hooks applied during translation. */
|
|
407
|
+
protected hooks: TranslateHook[];
|
|
401
408
|
constructor(options: CoreTranslatorOptions<M>);
|
|
409
|
+
/** Sort hooks by order value (lower runs earlier). */
|
|
410
|
+
private sortHooks;
|
|
411
|
+
/** Register a plugin or a raw pipeline hook. */
|
|
412
|
+
use(plugin: TranslatorPlugin | TranslateHook): void;
|
|
402
413
|
/** Check if a key exists in the specified locale or current locale. */
|
|
403
|
-
hasKey: <K
|
|
414
|
+
hasKey: <K extends LocalizedLeafKeys<M, L>>(key: K, targetLocale?: Locale<M>) => boolean;
|
|
404
415
|
/** Get the translated message for a key, with optional replacements. */
|
|
405
|
-
t: <Result = string, K = LocalizedLeafKeys<M, L>>(key: K, replacements?: Replacement) => Result;
|
|
416
|
+
t: <Result = string, K extends LocalizedLeafKeys<M, L> = LocalizedLeafKeys<M, L>>(key: K, replacements?: Replacement) => Result;
|
|
406
417
|
}
|
|
407
418
|
|
|
408
419
|
type ScopeTranslatorOptions<M> = CoreTranslatorOptions<M>;
|
|
409
|
-
type ScopeTranslatorMethods<M, L extends keyof M | "union" = "union", K = LocalizedLeafKeys<M, L>> = {
|
|
420
|
+
type ScopeTranslatorMethods<M extends LocaleMessages | undefined = undefined, L extends keyof M | "union" = "union", K = LocalizedLeafKeys<M, L>> = {
|
|
410
421
|
hasKey: (key?: K, targetLocale?: Locale<M>) => boolean;
|
|
411
422
|
t: <Result = string>(key?: K, replacements?: Replacement) => Result;
|
|
412
423
|
};
|
|
413
424
|
|
|
414
|
-
declare class ScopeTranslator<M
|
|
425
|
+
declare class ScopeTranslator<M extends LocaleMessages, L extends keyof M | "union" = "union"> extends CoreTranslator<M> {
|
|
415
426
|
constructor(options: ScopeTranslatorOptions<M>);
|
|
416
427
|
/** Create a scoped translator with a prefix key, providing `t` and `hasKey` for nested keys. */
|
|
417
428
|
scoped<PK extends LocalizedNodeKeys<M, L> | undefined = undefined>(preKey?: PK): PK extends string ? ScopeTranslatorMethods<M, L, ScopedLeafKeys<M, PK, L>> : ScopeTranslatorMethods<M, L>;
|
|
418
429
|
}
|
|
419
430
|
|
|
420
|
-
export { type DefaultDepth, type FallbackLocalesMap, type FormatHandler, type
|
|
431
|
+
export { type DefaultDepth, type FallbackLocalesMap, type FormatHandler, type HandlerContext, type LeafKeys, type LoadingHandler, type Locale, type LocaleMessages, type LocalizedLeafKeys, type LocalizedMessagesUnion, type LocalizedNodeKeys, type MissingHandler, type NestedMessage, type NodeKeys, type Replacement, type ScopedLeafKeys, type TranslateConfig, type TranslateContext, type TranslateHandlers, type TranslateHook, ScopeTranslator as Translator, type ScopeTranslatorMethods as TranslatorMethods, type ScopeTranslatorOptions as TranslatorOptions, type TranslatorPlugin };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { RuraHook } from 'rura';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* A nested message structure or a simple string message.
|
|
3
5
|
*
|
|
@@ -186,7 +188,7 @@ type LeafKeys<M, D extends number = DefaultDepth> = [D] extends [never] ? never
|
|
|
186
188
|
* LocalizedNodeKeys // → string
|
|
187
189
|
* ```
|
|
188
190
|
*/
|
|
189
|
-
type LocalizedNodeKeys<M =
|
|
191
|
+
type LocalizedNodeKeys<M extends LocaleMessages | undefined = undefined, L extends keyof M | "union" = "union", D extends number = DefaultDepth> = [M] extends [undefined] ? string : L extends "union" ? NodeKeys<M[keyof M], D> : NodeKeys<M[Extract<L, keyof M>], D>;
|
|
190
192
|
/**
|
|
191
193
|
* Extracts all **leaf keys** from the messages
|
|
192
194
|
* of a specified locale (or union of locales).
|
|
@@ -209,7 +211,7 @@ type LocalizedNodeKeys<M = unknown, L extends keyof M | "union" = "union", D ext
|
|
|
209
211
|
* LocalizedLeafKeys // → string
|
|
210
212
|
* ```
|
|
211
213
|
*/
|
|
212
|
-
type LocalizedLeafKeys<M =
|
|
214
|
+
type LocalizedLeafKeys<M extends LocaleMessages | undefined = undefined, L extends keyof M | "union" = "union", D extends number = DefaultDepth> = [M] extends [undefined] ? string : L extends "union" ? LeafKeys<M[keyof M], D> : LeafKeys<M[Extract<L, keyof M>], D>;
|
|
213
215
|
|
|
214
216
|
/**
|
|
215
217
|
* Resolves the type at a dot-separated key in a nested object.
|
|
@@ -246,51 +248,10 @@ type MessagesAtPreKey<T, PK extends string> = PK extends `${infer Head}.${infer
|
|
|
246
248
|
*/
|
|
247
249
|
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 NestedMessage ? LeafKeys<MessagesAtPreKey<messages, PK>, D> : never : never : string;
|
|
248
250
|
|
|
249
|
-
/**
|
|
250
|
-
* A ref object holding all localized messages by locale.
|
|
251
|
-
*
|
|
252
|
-
* @example
|
|
253
|
-
* ```ts
|
|
254
|
-
* const messagesRef: MessagesRef<Messages> = {
|
|
255
|
-
* current: {
|
|
256
|
-
* en: { home: { title: "Welcome" } },
|
|
257
|
-
* zh: { home: { title: "歡迎" } }
|
|
258
|
-
* }
|
|
259
|
-
* };
|
|
260
|
-
* ```
|
|
261
|
-
*/
|
|
262
|
-
type MessagesRef<M = unknown> = {
|
|
263
|
-
current: Readonly<M>;
|
|
264
|
-
};
|
|
265
|
-
/**
|
|
266
|
-
* A ref object holding the currently selected locale.
|
|
267
|
-
*
|
|
268
|
-
* @example
|
|
269
|
-
* ```ts
|
|
270
|
-
* const localeRef: LocaleRef<Messages> = {
|
|
271
|
-
* current: "en"
|
|
272
|
-
* };
|
|
273
|
-
* ```
|
|
274
|
-
*/
|
|
275
|
-
type LocaleRef<M = unknown> = {
|
|
276
|
-
current: Locale<M>;
|
|
277
|
-
};
|
|
278
|
-
/**
|
|
279
|
-
* A ref object indicating whether translation is loading.
|
|
280
|
-
*
|
|
281
|
-
* @example
|
|
282
|
-
* ```ts
|
|
283
|
-
* const isLoadingRef: IsLoadingRef = {
|
|
284
|
-
* current: true
|
|
285
|
-
* };
|
|
286
|
-
* ```
|
|
287
|
-
*/
|
|
288
|
-
type IsLoadingRef = {
|
|
289
|
-
current: boolean;
|
|
290
|
-
};
|
|
291
|
-
|
|
292
251
|
/**
|
|
293
252
|
* Configuration options for translation behavior.
|
|
253
|
+
*
|
|
254
|
+
* @template M - Messages map type.
|
|
294
255
|
*/
|
|
295
256
|
type TranslateConfig<M = unknown> = {
|
|
296
257
|
/** Optional mapping of fallback locales to use when a message is missing in the current locale. */
|
|
@@ -302,43 +263,66 @@ type TranslateConfig<M = unknown> = {
|
|
|
302
263
|
/** Optional set of handler functions for customizing translation behavior. */
|
|
303
264
|
handlers?: TranslateHandlers;
|
|
304
265
|
};
|
|
305
|
-
/**
|
|
306
|
-
* Optional handler functions for customizing translation behavior.
|
|
307
|
-
*/
|
|
266
|
+
/** Optional handler functions for customizing translation behavior. */
|
|
308
267
|
type TranslateHandlers = {
|
|
309
|
-
/** Function to format a resolved message before returning it. */
|
|
310
|
-
formatHandler?: FormatHandler;
|
|
311
268
|
/** Function called when a translation is still loading. */
|
|
312
269
|
loadingHandler?: LoadingHandler;
|
|
313
270
|
/** Function called when no message is found for a given key. */
|
|
314
271
|
missingHandler?: MissingHandler;
|
|
272
|
+
/** Function to format a resolved message before returning it. */
|
|
273
|
+
formatHandler?: FormatHandler;
|
|
315
274
|
};
|
|
316
|
-
/** Function to format a resolved message. */
|
|
317
|
-
type FormatHandler<Result = unknown> = (ctx: TranslateHandlerContext & {
|
|
318
|
-
message: string;
|
|
319
|
-
}) => Result;
|
|
320
275
|
/** Function called when translation is still loading. */
|
|
321
|
-
type LoadingHandler<Result = unknown> = (ctx:
|
|
276
|
+
type LoadingHandler<Result = unknown> = (ctx: HandlerContext) => Result;
|
|
322
277
|
/** Function called when no message is found for the given key. */
|
|
323
|
-
type MissingHandler<Result = unknown> = (ctx:
|
|
278
|
+
type MissingHandler<Result = unknown> = (ctx: HandlerContext) => Result;
|
|
279
|
+
/** Function to format a resolved message. */
|
|
280
|
+
type FormatHandler<Result = unknown> = (ctx: HandlerContext & {
|
|
281
|
+
rawMessage: string;
|
|
282
|
+
}) => Result;
|
|
324
283
|
/**
|
|
325
|
-
*
|
|
284
|
+
* Snapshot of the translate pipeline context exposed to handlers.
|
|
326
285
|
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
*
|
|
331
|
-
* replacements: { name: "John" }
|
|
332
|
-
* }
|
|
286
|
+
* This is a readonly view of TranslateContext:
|
|
287
|
+
* - Handlers can inspect the translation state
|
|
288
|
+
* - Handlers must NOT modify it
|
|
289
|
+
* - Pipeline will not be affected by handler changes
|
|
333
290
|
*/
|
|
334
|
-
type
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
291
|
+
type HandlerContext = Readonly<Omit<TranslateContext, "finalMessage">>;
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Context object shared across the translate pipeline.
|
|
295
|
+
*
|
|
296
|
+
* @template Result - Final translated value type.
|
|
297
|
+
*/
|
|
298
|
+
interface TranslateContext<Result = unknown> {
|
|
299
|
+
/** Configuration for the translate pipeline. */
|
|
300
|
+
config: TranslateConfig;
|
|
301
|
+
/** Current messages for translation */
|
|
302
|
+
messages: LocaleMessages;
|
|
303
|
+
/** Current active locale */
|
|
304
|
+
locale: string;
|
|
305
|
+
/** Current loading state */
|
|
306
|
+
isLoading?: boolean;
|
|
307
|
+
/** Message key to look up in the messages map */
|
|
338
308
|
key: string;
|
|
339
|
-
/** Optional replacements
|
|
309
|
+
/** Optional value replacements */
|
|
340
310
|
replacements?: Replacement;
|
|
341
|
-
|
|
311
|
+
/** Ordered list of locales to try, including fallbacks */
|
|
312
|
+
candidateLocales: string[];
|
|
313
|
+
/** Raw message string found in the messages map, before formatting */
|
|
314
|
+
rawMessage?: string;
|
|
315
|
+
/** Message after formatting (e.g. ICU, custom formatters) */
|
|
316
|
+
formattedMessage?: unknown;
|
|
317
|
+
/** Final value produced by the pipeline */
|
|
318
|
+
finalMessage?: Result;
|
|
319
|
+
/** Free-form metadata shared between hooks. */
|
|
320
|
+
meta: Record<string, unknown>;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* A single step in the translate pipeline.
|
|
324
|
+
*/
|
|
325
|
+
type TranslateHook = RuraHook<TranslateContext>;
|
|
342
326
|
|
|
343
327
|
/**
|
|
344
328
|
* Options for initializing a translator
|
|
@@ -362,16 +346,21 @@ interface BaseTranslatorOptions<M = unknown> {
|
|
|
362
346
|
isLoading?: boolean;
|
|
363
347
|
}
|
|
364
348
|
|
|
365
|
-
|
|
349
|
+
/**
|
|
350
|
+
* The minimal, shared foundation for all translators.
|
|
351
|
+
*
|
|
352
|
+
* @template M - Shape of the messages object.
|
|
353
|
+
*/
|
|
354
|
+
declare class BaseTranslator<M extends LocaleMessages> {
|
|
366
355
|
/** Current messages for translation */
|
|
367
|
-
protected
|
|
356
|
+
protected _messages: Readonly<M>;
|
|
368
357
|
/** Current active locale */
|
|
369
|
-
protected
|
|
358
|
+
protected _locale: Locale<M>;
|
|
370
359
|
/** Current loading state */
|
|
371
|
-
protected
|
|
360
|
+
protected _isLoading: boolean;
|
|
372
361
|
constructor(options: BaseTranslatorOptions<M>);
|
|
373
362
|
/** Get messages. */
|
|
374
|
-
get messages(): M
|
|
363
|
+
get messages(): M;
|
|
375
364
|
/** Get the current active locale. */
|
|
376
365
|
get locale(): Locale<M>;
|
|
377
366
|
/** Get the current loading state. */
|
|
@@ -394,27 +383,49 @@ declare class BaseTranslator<M = unknown> {
|
|
|
394
383
|
}
|
|
395
384
|
|
|
396
385
|
interface CoreTranslatorOptions<M> extends BaseTranslatorOptions<M>, TranslateConfig<M> {
|
|
386
|
+
/** Optional plugins or raw hooks to extend the translation pipeline */
|
|
387
|
+
plugins?: Array<TranslatorPlugin | TranslateHook>;
|
|
388
|
+
}
|
|
389
|
+
interface TranslatorPlugin {
|
|
390
|
+
/** Optional name for debugging or ordering hints */
|
|
391
|
+
name?: string;
|
|
392
|
+
/** One or multiple hooks contributed by this plugin */
|
|
393
|
+
hook?: TranslateHook | TranslateHook[];
|
|
397
394
|
}
|
|
398
395
|
|
|
399
|
-
|
|
400
|
-
|
|
396
|
+
/**
|
|
397
|
+
* CoreTranslator provides the default translation behavior
|
|
398
|
+
* using the pipeline engine and built-in hooks.
|
|
399
|
+
*
|
|
400
|
+
* @template M - Shape of the messages object.
|
|
401
|
+
* @template L - Locale selection strategy ("union" or specific locale keys).
|
|
402
|
+
*/
|
|
403
|
+
declare class CoreTranslator<M extends LocaleMessages, L extends keyof M | "union" = "union"> extends BaseTranslator<M> {
|
|
404
|
+
/** User-provided options including messages, locale, and config. */
|
|
405
|
+
protected translateConfig: TranslateConfig<M>;
|
|
406
|
+
/** Active pipeline hooks applied during translation. */
|
|
407
|
+
protected hooks: TranslateHook[];
|
|
401
408
|
constructor(options: CoreTranslatorOptions<M>);
|
|
409
|
+
/** Sort hooks by order value (lower runs earlier). */
|
|
410
|
+
private sortHooks;
|
|
411
|
+
/** Register a plugin or a raw pipeline hook. */
|
|
412
|
+
use(plugin: TranslatorPlugin | TranslateHook): void;
|
|
402
413
|
/** Check if a key exists in the specified locale or current locale. */
|
|
403
|
-
hasKey: <K
|
|
414
|
+
hasKey: <K extends LocalizedLeafKeys<M, L>>(key: K, targetLocale?: Locale<M>) => boolean;
|
|
404
415
|
/** Get the translated message for a key, with optional replacements. */
|
|
405
|
-
t: <Result = string, K = LocalizedLeafKeys<M, L>>(key: K, replacements?: Replacement) => Result;
|
|
416
|
+
t: <Result = string, K extends LocalizedLeafKeys<M, L> = LocalizedLeafKeys<M, L>>(key: K, replacements?: Replacement) => Result;
|
|
406
417
|
}
|
|
407
418
|
|
|
408
419
|
type ScopeTranslatorOptions<M> = CoreTranslatorOptions<M>;
|
|
409
|
-
type ScopeTranslatorMethods<M, L extends keyof M | "union" = "union", K = LocalizedLeafKeys<M, L>> = {
|
|
420
|
+
type ScopeTranslatorMethods<M extends LocaleMessages | undefined = undefined, L extends keyof M | "union" = "union", K = LocalizedLeafKeys<M, L>> = {
|
|
410
421
|
hasKey: (key?: K, targetLocale?: Locale<M>) => boolean;
|
|
411
422
|
t: <Result = string>(key?: K, replacements?: Replacement) => Result;
|
|
412
423
|
};
|
|
413
424
|
|
|
414
|
-
declare class ScopeTranslator<M
|
|
425
|
+
declare class ScopeTranslator<M extends LocaleMessages, L extends keyof M | "union" = "union"> extends CoreTranslator<M> {
|
|
415
426
|
constructor(options: ScopeTranslatorOptions<M>);
|
|
416
427
|
/** Create a scoped translator with a prefix key, providing `t` and `hasKey` for nested keys. */
|
|
417
428
|
scoped<PK extends LocalizedNodeKeys<M, L> | undefined = undefined>(preKey?: PK): PK extends string ? ScopeTranslatorMethods<M, L, ScopedLeafKeys<M, PK, L>> : ScopeTranslatorMethods<M, L>;
|
|
418
429
|
}
|
|
419
430
|
|
|
420
|
-
export { type DefaultDepth, type FallbackLocalesMap, type FormatHandler, type
|
|
431
|
+
export { type DefaultDepth, type FallbackLocalesMap, type FormatHandler, type HandlerContext, type LeafKeys, type LoadingHandler, type Locale, type LocaleMessages, type LocalizedLeafKeys, type LocalizedMessagesUnion, type LocalizedNodeKeys, type MissingHandler, type NestedMessage, type NodeKeys, type Replacement, type ScopedLeafKeys, type TranslateConfig, type TranslateContext, type TranslateHandlers, type TranslateHook, ScopeTranslator as Translator, type ScopeTranslatorMethods as TranslatorMethods, type ScopeTranslatorOptions as TranslatorOptions, type TranslatorPlugin };
|