intor-translator 1.0.13 → 1.0.15

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
@@ -194,11 +194,11 @@ var translate = ({
194
194
  return loadingMessage;
195
195
  }
196
196
  }
197
- if (!message) {
197
+ if (message === void 0 || message === null) {
198
198
  if (onMissing) {
199
199
  return onMissing({ key, locale, replacements });
200
200
  }
201
- if (placeholder) {
201
+ if (placeholder !== void 0 && placeholder !== null) {
202
202
  return placeholder;
203
203
  }
204
204
  return key;
@@ -212,9 +212,8 @@ var translate = ({
212
212
 
213
213
  // src/translators/base-translator/base-translator.ts
214
214
  var BaseTranslator = class {
215
- constructor(options = {}) {
215
+ constructor(options) {
216
216
  this.messagesRef = { current: void 0 };
217
- this.localeRef = { current: void 0 };
218
217
  /** Check if a key exists in the specified locale or current locale. */
219
218
  this.hasKey = (key, targetLocale) => {
220
219
  return hasKey({
@@ -224,12 +223,8 @@ var BaseTranslator = class {
224
223
  targetLocale
225
224
  });
226
225
  };
227
- if (options.messages) {
228
- this.messagesRef.current = options.messages;
229
- }
230
- if (options.locale) {
231
- this.localeRef.current = options.locale;
232
- }
226
+ this.messagesRef = { current: options.messages };
227
+ this.localeRef = { current: options.locale };
233
228
  }
234
229
  /** Get all message data. */
235
230
  get messages() {
@@ -270,7 +265,7 @@ var CoreTranslator = class extends BaseTranslator {
270
265
  replacements
271
266
  });
272
267
  };
273
- this.options = options || {};
268
+ this.options = options;
274
269
  }
275
270
  /** Get the current loading state. */
276
271
  get isLoading() {
package/dist/index.d.cts CHANGED
@@ -313,7 +313,7 @@ type MessagesRef<M> = {
313
313
  * };
314
314
  */
315
315
  type LocaleRef<M> = {
316
- current?: LocaleKey<M>;
316
+ current: LocaleKey<M>;
317
317
  };
318
318
  /**
319
319
  * A ref object indicating whether translation is loading.
@@ -374,13 +374,13 @@ type TranslateContext = {
374
374
 
375
375
  interface BaseTranslatorOptions<M = unknown> {
376
376
  messages?: Readonly<M>;
377
- locale?: LocaleKey<M>;
377
+ locale: LocaleKey<M>;
378
378
  }
379
379
 
380
380
  declare class BaseTranslator<M = unknown> {
381
381
  protected messagesRef: MessagesRef<M>;
382
382
  protected localeRef: LocaleRef<M>;
383
- constructor(options?: BaseTranslatorOptions<M>);
383
+ constructor(options: BaseTranslatorOptions<M>);
384
384
  /** Get all message data. */
385
385
  get messages(): M | undefined;
386
386
  /**
@@ -391,7 +391,7 @@ declare class BaseTranslator<M = unknown> {
391
391
  */
392
392
  setMessages<N extends LocaleNamespaceMessages>(messages: N): void;
393
393
  /** Get the current active locale. */
394
- get locale(): LocaleKey<M> | undefined;
394
+ get locale(): LocaleKey<M>;
395
395
  /** Change the active locale. */
396
396
  setLocale(newLocale: LocaleKey<M>): void;
397
397
  /** Check if a key exists in the specified locale or current locale. */
@@ -404,7 +404,7 @@ interface CoreTranslatorOptions<M> extends BaseTranslatorOptions<M>, TranslateCo
404
404
  declare class CoreTranslator<M = unknown> extends BaseTranslator<M> {
405
405
  protected options: CoreTranslatorOptions<M>;
406
406
  protected isLoadingRef: IsLoadingRef;
407
- constructor(options?: CoreTranslatorOptions<M>);
407
+ constructor(options: CoreTranslatorOptions<M>);
408
408
  /** Get the current loading state. */
409
409
  get isLoading(): boolean;
410
410
  /** Set the loading state. */
@@ -413,29 +413,9 @@ declare class CoreTranslator<M = unknown> extends BaseTranslator<M> {
413
413
  }
414
414
 
415
415
  declare class ScopeTranslator<M = unknown> extends CoreTranslator<M> {
416
- constructor(options?: CoreTranslatorOptions<M>);
416
+ constructor(options: CoreTranslatorOptions<M>);
417
417
  scoped<K extends NodeKeys<UnionLocaleMessages<M>> & string>(preKey: K): ScopedTranslatorMethods<M, K>;
418
418
  scoped(): TranslatorMethods<M>;
419
419
  }
420
420
 
421
- /**
422
- * Generic translate function type.
423
- *
424
- * Represents the shape of a `t()` function that supports typed keys and optional replacements.
425
- *
426
- * @template M - The shape of the messages object (e.g., typeof messages)
427
- * @template Result - The expected return type (usually string, but can be JSX or others)
428
- */
429
- type TranslateFunction = <M, Result = string>(key: InferTranslatorKey<M>, replacements?: Replacement | RichReplacement) => Result;
430
- /**
431
- * Generic hasKey function type.
432
- *
433
- * Represents a function that checks if a given key exists in a message set,
434
- * optionally for a specific locale.
435
- *
436
- * @template M - The shape of the messages object
437
- * @template Result - The return value (default is boolean, override if needed)
438
- */
439
- type HasKeyFunction = <M, Result = boolean>(key: InferTranslatorKey<M>, targetLocale?: LocaleKey<M>) => Result;
440
-
441
- export { type FallbackLocalesMap, type FormatMessage, type HasKeyFunction, type InferTranslatorKey, type IsLoadingRef, type LeafKeys, type Locale, type LocaleKey, type LocaleNamespaceMessages, type LocaleRef, type Message, type MessageRecord, type MessagesRef, type Namespace, type NamespaceMessages, type NestedMessage, type NodeKeys, type OnLoading, type OnMissing, type Replacement, type RichReplacement, type ScopedLeafKeys, type StrictLocaleKey, type TranslateConfig, type TranslateContext, type TranslateFunction, type TranslateHandlers, ScopeTranslator as Translator, type UnionLocaleLeafKeys, type UnionLocaleMessages };
421
+ export { type FallbackLocalesMap, type FormatMessage, type InferTranslatorKey, type IsLoadingRef, type LeafKeys, type Locale, type LocaleKey, type LocaleNamespaceMessages, type LocaleRef, type Message, type MessageRecord, type MessagesRef, type Namespace, type NamespaceMessages, type NestedMessage, type NodeKeys, type OnLoading, type OnMissing, type Replacement, type RichReplacement, type ScopedLeafKeys, type StrictLocaleKey, type TranslateConfig, type TranslateContext, type TranslateHandlers, ScopeTranslator as Translator, type UnionLocaleLeafKeys, type UnionLocaleMessages };
package/dist/index.d.ts CHANGED
@@ -313,7 +313,7 @@ type MessagesRef<M> = {
313
313
  * };
314
314
  */
315
315
  type LocaleRef<M> = {
316
- current?: LocaleKey<M>;
316
+ current: LocaleKey<M>;
317
317
  };
318
318
  /**
319
319
  * A ref object indicating whether translation is loading.
@@ -374,13 +374,13 @@ type TranslateContext = {
374
374
 
375
375
  interface BaseTranslatorOptions<M = unknown> {
376
376
  messages?: Readonly<M>;
377
- locale?: LocaleKey<M>;
377
+ locale: LocaleKey<M>;
378
378
  }
379
379
 
380
380
  declare class BaseTranslator<M = unknown> {
381
381
  protected messagesRef: MessagesRef<M>;
382
382
  protected localeRef: LocaleRef<M>;
383
- constructor(options?: BaseTranslatorOptions<M>);
383
+ constructor(options: BaseTranslatorOptions<M>);
384
384
  /** Get all message data. */
385
385
  get messages(): M | undefined;
386
386
  /**
@@ -391,7 +391,7 @@ declare class BaseTranslator<M = unknown> {
391
391
  */
392
392
  setMessages<N extends LocaleNamespaceMessages>(messages: N): void;
393
393
  /** Get the current active locale. */
394
- get locale(): LocaleKey<M> | undefined;
394
+ get locale(): LocaleKey<M>;
395
395
  /** Change the active locale. */
396
396
  setLocale(newLocale: LocaleKey<M>): void;
397
397
  /** Check if a key exists in the specified locale or current locale. */
@@ -404,7 +404,7 @@ interface CoreTranslatorOptions<M> extends BaseTranslatorOptions<M>, TranslateCo
404
404
  declare class CoreTranslator<M = unknown> extends BaseTranslator<M> {
405
405
  protected options: CoreTranslatorOptions<M>;
406
406
  protected isLoadingRef: IsLoadingRef;
407
- constructor(options?: CoreTranslatorOptions<M>);
407
+ constructor(options: CoreTranslatorOptions<M>);
408
408
  /** Get the current loading state. */
409
409
  get isLoading(): boolean;
410
410
  /** Set the loading state. */
@@ -413,29 +413,9 @@ declare class CoreTranslator<M = unknown> extends BaseTranslator<M> {
413
413
  }
414
414
 
415
415
  declare class ScopeTranslator<M = unknown> extends CoreTranslator<M> {
416
- constructor(options?: CoreTranslatorOptions<M>);
416
+ constructor(options: CoreTranslatorOptions<M>);
417
417
  scoped<K extends NodeKeys<UnionLocaleMessages<M>> & string>(preKey: K): ScopedTranslatorMethods<M, K>;
418
418
  scoped(): TranslatorMethods<M>;
419
419
  }
420
420
 
421
- /**
422
- * Generic translate function type.
423
- *
424
- * Represents the shape of a `t()` function that supports typed keys and optional replacements.
425
- *
426
- * @template M - The shape of the messages object (e.g., typeof messages)
427
- * @template Result - The expected return type (usually string, but can be JSX or others)
428
- */
429
- type TranslateFunction = <M, Result = string>(key: InferTranslatorKey<M>, replacements?: Replacement | RichReplacement) => Result;
430
- /**
431
- * Generic hasKey function type.
432
- *
433
- * Represents a function that checks if a given key exists in a message set,
434
- * optionally for a specific locale.
435
- *
436
- * @template M - The shape of the messages object
437
- * @template Result - The return value (default is boolean, override if needed)
438
- */
439
- type HasKeyFunction = <M, Result = boolean>(key: InferTranslatorKey<M>, targetLocale?: LocaleKey<M>) => Result;
440
-
441
- export { type FallbackLocalesMap, type FormatMessage, type HasKeyFunction, type InferTranslatorKey, type IsLoadingRef, type LeafKeys, type Locale, type LocaleKey, type LocaleNamespaceMessages, type LocaleRef, type Message, type MessageRecord, type MessagesRef, type Namespace, type NamespaceMessages, type NestedMessage, type NodeKeys, type OnLoading, type OnMissing, type Replacement, type RichReplacement, type ScopedLeafKeys, type StrictLocaleKey, type TranslateConfig, type TranslateContext, type TranslateFunction, type TranslateHandlers, ScopeTranslator as Translator, type UnionLocaleLeafKeys, type UnionLocaleMessages };
421
+ export { type FallbackLocalesMap, type FormatMessage, type InferTranslatorKey, type IsLoadingRef, type LeafKeys, type Locale, type LocaleKey, type LocaleNamespaceMessages, type LocaleRef, type Message, type MessageRecord, type MessagesRef, type Namespace, type NamespaceMessages, type NestedMessage, type NodeKeys, type OnLoading, type OnMissing, type Replacement, type RichReplacement, type ScopedLeafKeys, type StrictLocaleKey, type TranslateConfig, type TranslateContext, type TranslateHandlers, ScopeTranslator as Translator, type UnionLocaleLeafKeys, type UnionLocaleMessages };
package/dist/index.js CHANGED
@@ -192,11 +192,11 @@ var translate = ({
192
192
  return loadingMessage;
193
193
  }
194
194
  }
195
- if (!message) {
195
+ if (message === void 0 || message === null) {
196
196
  if (onMissing) {
197
197
  return onMissing({ key, locale, replacements });
198
198
  }
199
- if (placeholder) {
199
+ if (placeholder !== void 0 && placeholder !== null) {
200
200
  return placeholder;
201
201
  }
202
202
  return key;
@@ -210,9 +210,8 @@ var translate = ({
210
210
 
211
211
  // src/translators/base-translator/base-translator.ts
212
212
  var BaseTranslator = class {
213
- constructor(options = {}) {
213
+ constructor(options) {
214
214
  this.messagesRef = { current: void 0 };
215
- this.localeRef = { current: void 0 };
216
215
  /** Check if a key exists in the specified locale or current locale. */
217
216
  this.hasKey = (key, targetLocale) => {
218
217
  return hasKey({
@@ -222,12 +221,8 @@ var BaseTranslator = class {
222
221
  targetLocale
223
222
  });
224
223
  };
225
- if (options.messages) {
226
- this.messagesRef.current = options.messages;
227
- }
228
- if (options.locale) {
229
- this.localeRef.current = options.locale;
230
- }
224
+ this.messagesRef = { current: options.messages };
225
+ this.localeRef = { current: options.locale };
231
226
  }
232
227
  /** Get all message data. */
233
228
  get messages() {
@@ -268,7 +263,7 @@ var CoreTranslator = class extends BaseTranslator {
268
263
  replacements
269
264
  });
270
265
  };
271
- this.options = options || {};
266
+ this.options = options;
272
267
  }
273
268
  /** Get the current loading state. */
274
269
  get isLoading() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "intor-translator",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "A type safe translator that knows what to say and how to handle the rest. Supports custom messages, rich replacements, and async loading.",
5
5
  "author": "Yiming Liao",
6
6
  "license": "MIT",