intor 2.2.6 → 2.2.7

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.
@@ -690,16 +690,21 @@ var loadMessages = async ({
690
690
  };
691
691
 
692
692
  // src/server/translator/get-translator.ts
693
- async function getTranslator(opts) {
694
- const { config, locale, pathname = "", preKey, handlers } = opts;
695
- const messages = await loadMessages({ config, locale, pathname });
693
+ async function getTranslator(options) {
694
+ const { config, locale, pathname = "", preKey } = options;
695
+ const messages = await loadMessages({
696
+ config,
697
+ locale,
698
+ pathname,
699
+ extraOptions: options.extraOptions
700
+ });
696
701
  const translator = new intorTranslator.Translator({
697
702
  locale,
698
703
  messages,
699
704
  fallbackLocales: config.fallbackLocales,
700
705
  loadingMessage: config.translator?.loadingMessage,
701
706
  placeholder: config.translator?.placeholder,
702
- handlers
707
+ handlers: options.handlers
703
708
  });
704
709
  const props = { messages, locale };
705
710
  const scoped = translator.scoped(preKey);
@@ -711,12 +716,15 @@ async function getTranslator(opts) {
711
716
  }
712
717
 
713
718
  // src/adapters/next/server/get-translator.ts
714
- async function getTranslator2(config, preKey) {
719
+ async function getTranslator2(options) {
720
+ const { config, preKey, handlers, extraOptions } = options;
715
721
  const { locale, pathname } = await getI18nContext(config);
716
722
  const translatorInstance = getTranslator({
717
723
  config,
718
724
  locale,
719
725
  pathname,
726
+ handlers,
727
+ extraOptions,
720
728
  preKey
721
729
  });
722
730
  return translatorInstance;
@@ -1,5 +1,7 @@
1
1
  import { Level, NormalizerConfig, FormatterConfig, LoggerPreset } from 'logry/edge';
2
- import { Locale, LocaleMessages, FallbackLocalesMap, ScopedLeafKeys, LocalizedLeafKeys, Replacement, LocalizedNodeKeys } from 'intor-translator';
2
+ import { Locale, LocaleMessages, FallbackLocalesMap, NestedMessage, ScopedLeafKeys, LocalizedLeafKeys, Replacement, TranslateHandlers, LocalizedNodeKeys } from 'intor-translator';
3
+ import Keyv from 'keyv';
4
+ import { Logger } from 'logry';
3
5
 
4
6
  type CookieRawOptions = {
5
7
  /** Completely disable cookie usage (no read, no write, no lookup by name) - default: false */
@@ -162,6 +164,53 @@ interface I18nContext {
162
164
  */
163
165
  declare const getI18nContext: <CK extends GenConfigKeys = "__default__">(config: IntorResolvedConfig) => Promise<I18nContext>;
164
166
 
167
+ /**
168
+ * Represents a collection of localized messages.
169
+ *
170
+ * - Each key is a namespace or message identifier, and the value is a
171
+ * `NestedMessage` object that can contain nested message structures.
172
+ *
173
+ * @example
174
+ * ```ts
175
+ * const messages: Messages = {
176
+ * ui: {
177
+ * greeting: "Hello",
178
+ * farewell: "Goodbye"
179
+ * },
180
+ * errors: {
181
+ * network: "Network error occurred"
182
+ * }
183
+ * };
184
+ * ```
185
+ */
186
+ type Messages = Record<string, NestedMessage>;
187
+ /**
188
+ * A function that reads messages from a given file path.
189
+ *
190
+ * - This function is expected to return a `Promise` that resolves to a `Messages` object.
191
+ * - It can be implemented to support different file formats such as JSON, YAML, or others.
192
+ *
193
+ * @param filePath - The path to the message file to read.
194
+ * @returns A Promise that resolves to a `Messages` object.
195
+ *
196
+ * @example
197
+ * ```ts
198
+ * const reader: MessagesReader = async (filePath) => {
199
+ * const content = await fs.promises.readFile(filePath, "utf-8");
200
+ * return JSON.parse(content) as Messages;
201
+ * };
202
+ * ```
203
+ */
204
+ type MessagesReader = (filePath: string) => Promise<Messages>;
205
+
206
+ /**
207
+ * Global messages pool (cross-module + hot-reload safe)
208
+ */
209
+ type MessagesPool = Keyv<LocaleMessages>;
210
+ declare global {
211
+ var __INTOR_MESSAGES_POOL__: MessagesPool | undefined;
212
+ }
213
+
165
214
  /** Base properties shared by all translator instances. */
166
215
  interface TranslatorBaseProps<M = unknown> {
167
216
  /** `messages`: The message object containing all translations. */
@@ -193,16 +242,38 @@ type TranslatorInstance<M, PK extends string | undefined = undefined> = {
193
242
  t: <Result = string>(key?: Key<M, PK>, replacements?: Replacement) => Result;
194
243
  } & TranslatorBaseProps<M> & TranslatorClientProps<M>;
195
244
 
245
+ /**
246
+ * Global logger pool (cross-module + hot-reload safe)
247
+ */
248
+ type LoggerPool = Map<string, Logger>;
249
+ declare global {
250
+ var __INTOR_LOGGER_POOL__: LoggerPool | undefined;
251
+ }
252
+
196
253
  /**
197
254
  * Create a translator instance ready for the current Next.js SSR environment.
198
255
  *
199
- * - Automatically resolves the current locale and pathname using the Next.js adapter.
200
- * - Loads all corresponding messages for the resolved locale and pathname.
201
- * - Returns a translator object containing `t`, `hasKey`, `messages`, and other helpers.
202
- * - Supports optional `preKey` to create a scoped translator for nested translation keys.
203
- * - Allows passing additional `TranslateConfig` options to the underlying translator.
256
+ * - **Automatically resolves the current locale and pathname using the Next.js adapter.**
257
+ * - Loads messages using the provided config, locale, and pathname.
258
+ * - Initializes a translator with `t`, `hasKey`, and optional scoped methods.
259
+ * - Supports optional `preKey` to create a scoped translator for nested keys.
204
260
  */
205
- declare function getTranslator<CK extends GenConfigKeys = "__default__">(config: IntorResolvedConfig): Promise<TranslatorInstance<GenMessages<CK>>>;
206
- declare function getTranslator<CK extends GenConfigKeys = "__default__", PK extends string = LocalizedNodeKeys<GenMessages<CK>>>(config: IntorResolvedConfig, preKey: IfGen<PK, string>): Promise<TranslatorInstance<GenMessages<CK>, PK>>;
261
+ declare function getTranslator<CK extends GenConfigKeys = "__default__">(options: {
262
+ config: IntorResolvedConfig;
263
+ handlers?: TranslateHandlers;
264
+ extraOptions?: {
265
+ exts?: string[];
266
+ messagesReader?: MessagesReader;
267
+ };
268
+ }): Promise<TranslatorInstance<GenMessages<CK>>>;
269
+ declare function getTranslator<CK extends GenConfigKeys = "__default__", PK extends string = LocalizedNodeKeys<GenMessages<CK>>>(options: {
270
+ config: IntorResolvedConfig;
271
+ handlers?: TranslateHandlers;
272
+ extraOptions?: {
273
+ exts?: string[];
274
+ messagesReader?: MessagesReader;
275
+ };
276
+ preKey: IfGen<PK, string>;
277
+ }): Promise<TranslatorInstance<GenMessages<CK>, PK>>;
207
278
 
208
279
  export { getI18nContext, getTranslator };
@@ -1,5 +1,7 @@
1
1
  import { Level, NormalizerConfig, FormatterConfig, LoggerPreset } from 'logry/edge';
2
- import { Locale, LocaleMessages, FallbackLocalesMap, ScopedLeafKeys, LocalizedLeafKeys, Replacement, LocalizedNodeKeys } from 'intor-translator';
2
+ import { Locale, LocaleMessages, FallbackLocalesMap, NestedMessage, ScopedLeafKeys, LocalizedLeafKeys, Replacement, TranslateHandlers, LocalizedNodeKeys } from 'intor-translator';
3
+ import Keyv from 'keyv';
4
+ import { Logger } from 'logry';
3
5
 
4
6
  type CookieRawOptions = {
5
7
  /** Completely disable cookie usage (no read, no write, no lookup by name) - default: false */
@@ -162,6 +164,53 @@ interface I18nContext {
162
164
  */
163
165
  declare const getI18nContext: <CK extends GenConfigKeys = "__default__">(config: IntorResolvedConfig) => Promise<I18nContext>;
164
166
 
167
+ /**
168
+ * Represents a collection of localized messages.
169
+ *
170
+ * - Each key is a namespace or message identifier, and the value is a
171
+ * `NestedMessage` object that can contain nested message structures.
172
+ *
173
+ * @example
174
+ * ```ts
175
+ * const messages: Messages = {
176
+ * ui: {
177
+ * greeting: "Hello",
178
+ * farewell: "Goodbye"
179
+ * },
180
+ * errors: {
181
+ * network: "Network error occurred"
182
+ * }
183
+ * };
184
+ * ```
185
+ */
186
+ type Messages = Record<string, NestedMessage>;
187
+ /**
188
+ * A function that reads messages from a given file path.
189
+ *
190
+ * - This function is expected to return a `Promise` that resolves to a `Messages` object.
191
+ * - It can be implemented to support different file formats such as JSON, YAML, or others.
192
+ *
193
+ * @param filePath - The path to the message file to read.
194
+ * @returns A Promise that resolves to a `Messages` object.
195
+ *
196
+ * @example
197
+ * ```ts
198
+ * const reader: MessagesReader = async (filePath) => {
199
+ * const content = await fs.promises.readFile(filePath, "utf-8");
200
+ * return JSON.parse(content) as Messages;
201
+ * };
202
+ * ```
203
+ */
204
+ type MessagesReader = (filePath: string) => Promise<Messages>;
205
+
206
+ /**
207
+ * Global messages pool (cross-module + hot-reload safe)
208
+ */
209
+ type MessagesPool = Keyv<LocaleMessages>;
210
+ declare global {
211
+ var __INTOR_MESSAGES_POOL__: MessagesPool | undefined;
212
+ }
213
+
165
214
  /** Base properties shared by all translator instances. */
166
215
  interface TranslatorBaseProps<M = unknown> {
167
216
  /** `messages`: The message object containing all translations. */
@@ -193,16 +242,38 @@ type TranslatorInstance<M, PK extends string | undefined = undefined> = {
193
242
  t: <Result = string>(key?: Key<M, PK>, replacements?: Replacement) => Result;
194
243
  } & TranslatorBaseProps<M> & TranslatorClientProps<M>;
195
244
 
245
+ /**
246
+ * Global logger pool (cross-module + hot-reload safe)
247
+ */
248
+ type LoggerPool = Map<string, Logger>;
249
+ declare global {
250
+ var __INTOR_LOGGER_POOL__: LoggerPool | undefined;
251
+ }
252
+
196
253
  /**
197
254
  * Create a translator instance ready for the current Next.js SSR environment.
198
255
  *
199
- * - Automatically resolves the current locale and pathname using the Next.js adapter.
200
- * - Loads all corresponding messages for the resolved locale and pathname.
201
- * - Returns a translator object containing `t`, `hasKey`, `messages`, and other helpers.
202
- * - Supports optional `preKey` to create a scoped translator for nested translation keys.
203
- * - Allows passing additional `TranslateConfig` options to the underlying translator.
256
+ * - **Automatically resolves the current locale and pathname using the Next.js adapter.**
257
+ * - Loads messages using the provided config, locale, and pathname.
258
+ * - Initializes a translator with `t`, `hasKey`, and optional scoped methods.
259
+ * - Supports optional `preKey` to create a scoped translator for nested keys.
204
260
  */
205
- declare function getTranslator<CK extends GenConfigKeys = "__default__">(config: IntorResolvedConfig): Promise<TranslatorInstance<GenMessages<CK>>>;
206
- declare function getTranslator<CK extends GenConfigKeys = "__default__", PK extends string = LocalizedNodeKeys<GenMessages<CK>>>(config: IntorResolvedConfig, preKey: IfGen<PK, string>): Promise<TranslatorInstance<GenMessages<CK>, PK>>;
261
+ declare function getTranslator<CK extends GenConfigKeys = "__default__">(options: {
262
+ config: IntorResolvedConfig;
263
+ handlers?: TranslateHandlers;
264
+ extraOptions?: {
265
+ exts?: string[];
266
+ messagesReader?: MessagesReader;
267
+ };
268
+ }): Promise<TranslatorInstance<GenMessages<CK>>>;
269
+ declare function getTranslator<CK extends GenConfigKeys = "__default__", PK extends string = LocalizedNodeKeys<GenMessages<CK>>>(options: {
270
+ config: IntorResolvedConfig;
271
+ handlers?: TranslateHandlers;
272
+ extraOptions?: {
273
+ exts?: string[];
274
+ messagesReader?: MessagesReader;
275
+ };
276
+ preKey: IfGen<PK, string>;
277
+ }): Promise<TranslatorInstance<GenMessages<CK>, PK>>;
207
278
 
208
279
  export { getI18nContext, getTranslator };
@@ -680,16 +680,21 @@ var loadMessages = async ({
680
680
  };
681
681
 
682
682
  // src/server/translator/get-translator.ts
683
- async function getTranslator(opts) {
684
- const { config, locale, pathname = "", preKey, handlers } = opts;
685
- const messages = await loadMessages({ config, locale, pathname });
683
+ async function getTranslator(options) {
684
+ const { config, locale, pathname = "", preKey } = options;
685
+ const messages = await loadMessages({
686
+ config,
687
+ locale,
688
+ pathname,
689
+ extraOptions: options.extraOptions
690
+ });
686
691
  const translator = new Translator({
687
692
  locale,
688
693
  messages,
689
694
  fallbackLocales: config.fallbackLocales,
690
695
  loadingMessage: config.translator?.loadingMessage,
691
696
  placeholder: config.translator?.placeholder,
692
- handlers
697
+ handlers: options.handlers
693
698
  });
694
699
  const props = { messages, locale };
695
700
  const scoped = translator.scoped(preKey);
@@ -701,12 +706,15 @@ async function getTranslator(opts) {
701
706
  }
702
707
 
703
708
  // src/adapters/next/server/get-translator.ts
704
- async function getTranslator2(config, preKey) {
709
+ async function getTranslator2(options) {
710
+ const { config, preKey, handlers, extraOptions } = options;
705
711
  const { locale, pathname } = await getI18nContext(config);
706
712
  const translatorInstance = getTranslator({
707
713
  config,
708
714
  locale,
709
715
  pathname,
716
+ handlers,
717
+ extraOptions,
710
718
  preKey
711
719
  });
712
720
  return translatorInstance;
@@ -669,16 +669,21 @@ var intor = async (config, i18nContext, loadMessagesOptions = {}) => {
669
669
  messages: mergedMessages
670
670
  };
671
671
  };
672
- async function getTranslator(opts) {
673
- const { config, locale, pathname = "", preKey, handlers } = opts;
674
- const messages = await loadMessages({ config, locale, pathname });
672
+ async function getTranslator(options) {
673
+ const { config, locale, pathname = "", preKey } = options;
674
+ const messages = await loadMessages({
675
+ config,
676
+ locale,
677
+ pathname,
678
+ extraOptions: options.extraOptions
679
+ });
675
680
  const translator = new intorTranslator.Translator({
676
681
  locale,
677
682
  messages,
678
683
  fallbackLocales: config.fallbackLocales,
679
684
  loadingMessage: config.translator?.loadingMessage,
680
685
  placeholder: config.translator?.placeholder,
681
- handlers
686
+ handlers: options.handlers
682
687
  });
683
688
  const props = { messages, locale };
684
689
  const scoped = translator.scoped(preKey);
@@ -340,19 +340,26 @@ type TranslatorInstance<M, PK extends string | undefined = undefined> = {
340
340
  * - Loads messages using the provided config, locale, and pathname.
341
341
  * - Initializes a translator with `t`, `hasKey`, and optional scoped methods.
342
342
  * - Supports optional `preKey` to create a scoped translator for nested keys.
343
- * - Passes additional options to the underlying `Translator`.
344
343
  */
345
- declare function getTranslator<CK extends GenConfigKeys = "__default__">(opts: {
344
+ declare function getTranslator<CK extends GenConfigKeys = "__default__">(options: {
346
345
  config: IntorResolvedConfig;
347
346
  locale: GenLocale;
348
347
  pathname?: string;
349
348
  handlers?: TranslateHandlers;
349
+ extraOptions?: {
350
+ exts?: string[];
351
+ messagesReader?: MessagesReader;
352
+ };
350
353
  }): Promise<TranslatorInstance<GenMessages<CK>>>;
351
- declare function getTranslator<CK extends GenConfigKeys = "__default__", PK extends string = LocalizedNodeKeys<GenMessages<CK>>>(opts: {
354
+ declare function getTranslator<CK extends GenConfigKeys = "__default__", PK extends string = LocalizedNodeKeys<GenMessages<CK>>>(options: {
352
355
  config: IntorResolvedConfig;
353
356
  locale: GenLocale;
354
357
  pathname?: string;
355
358
  handlers?: TranslateHandlers;
359
+ extraOptions?: {
360
+ exts?: string[];
361
+ messagesReader?: MessagesReader;
362
+ };
356
363
  preKey?: PK;
357
364
  }): Promise<TranslatorInstance<GenMessages<CK>, PK>>;
358
365
 
@@ -340,19 +340,26 @@ type TranslatorInstance<M, PK extends string | undefined = undefined> = {
340
340
  * - Loads messages using the provided config, locale, and pathname.
341
341
  * - Initializes a translator with `t`, `hasKey`, and optional scoped methods.
342
342
  * - Supports optional `preKey` to create a scoped translator for nested keys.
343
- * - Passes additional options to the underlying `Translator`.
344
343
  */
345
- declare function getTranslator<CK extends GenConfigKeys = "__default__">(opts: {
344
+ declare function getTranslator<CK extends GenConfigKeys = "__default__">(options: {
346
345
  config: IntorResolvedConfig;
347
346
  locale: GenLocale;
348
347
  pathname?: string;
349
348
  handlers?: TranslateHandlers;
349
+ extraOptions?: {
350
+ exts?: string[];
351
+ messagesReader?: MessagesReader;
352
+ };
350
353
  }): Promise<TranslatorInstance<GenMessages<CK>>>;
351
- declare function getTranslator<CK extends GenConfigKeys = "__default__", PK extends string = LocalizedNodeKeys<GenMessages<CK>>>(opts: {
354
+ declare function getTranslator<CK extends GenConfigKeys = "__default__", PK extends string = LocalizedNodeKeys<GenMessages<CK>>>(options: {
352
355
  config: IntorResolvedConfig;
353
356
  locale: GenLocale;
354
357
  pathname?: string;
355
358
  handlers?: TranslateHandlers;
359
+ extraOptions?: {
360
+ exts?: string[];
361
+ messagesReader?: MessagesReader;
362
+ };
356
363
  preKey?: PK;
357
364
  }): Promise<TranslatorInstance<GenMessages<CK>, PK>>;
358
365
 
@@ -659,16 +659,21 @@ var intor = async (config, i18nContext, loadMessagesOptions = {}) => {
659
659
  messages: mergedMessages
660
660
  };
661
661
  };
662
- async function getTranslator(opts) {
663
- const { config, locale, pathname = "", preKey, handlers } = opts;
664
- const messages = await loadMessages({ config, locale, pathname });
662
+ async function getTranslator(options) {
663
+ const { config, locale, pathname = "", preKey } = options;
664
+ const messages = await loadMessages({
665
+ config,
666
+ locale,
667
+ pathname,
668
+ extraOptions: options.extraOptions
669
+ });
665
670
  const translator = new Translator({
666
671
  locale,
667
672
  messages,
668
673
  fallbackLocales: config.fallbackLocales,
669
674
  loadingMessage: config.translator?.loadingMessage,
670
675
  placeholder: config.translator?.placeholder,
671
- handlers
676
+ handlers: options.handlers
672
677
  });
673
678
  const props = { messages, locale };
674
679
  const scoped = translator.scoped(preKey);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "intor",
3
- "version": "2.2.6",
3
+ "version": "2.2.7",
4
4
  "description": "A modular and extensible i18n core designed for TypeScript and JavaScript projects. Intor enables custom translation logic with support for both frontend and backend environments, featuring runtime configuration, caching, adapters, and message loaders.",
5
5
  "author": "Yiming Liao",
6
6
  "license": "MIT",