intor 2.4.7 → 2.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.
@@ -1,10 +1,9 @@
1
1
  export { intor } from '../../src/server/intor/intor.js';
2
- export { loadMessages } from '../../src/server/messages/load-messages.js';
3
- import 'node:path';
4
- import 'p-limit';
5
2
  import '../../src/core/error/intor-error.js';
6
3
  import 'logry';
4
+ import 'p-limit';
7
5
  import 'intor-translator';
6
+ import 'node:path';
8
7
  export { clearMessagesPool } from '../../src/server/messages/load-local-messages/cache/messages-pool.js';
9
8
  import 'node:fs/promises';
10
9
  export { getTranslator } from '../../src/server/helpers/get-translator.js';
@@ -9,14 +9,15 @@ import { initTranslator } from '../translator/init-translator.js';
9
9
  * Get a server-side translator for the current execution context.
10
10
  */
11
11
  async function getTranslator(config, params) {
12
- const { locale, readers, allowCacheWrite, fetch, preKey, handlers, plugins } = params;
12
+ const { locale, loader, readers, allowCacheWrite = false, fetch, handlers, plugins, preKey, } = params;
13
13
  // Initialize a locale-bound translator snapshot with messages loaded
14
14
  const translator = await initTranslator(config, locale, {
15
+ loader,
15
16
  readers,
16
17
  allowCacheWrite,
17
18
  fetch: fetch || globalThis.fetch,
18
- plugins,
19
19
  handlers,
20
+ plugins,
20
21
  });
21
22
  const scoped = translator.scoped(preKey);
22
23
  return {
@@ -15,11 +15,11 @@ import 'node:fs/promises';
15
15
  * - Creates an immutable Translator instance for server usage
16
16
  */
17
17
  async function initTranslator(config, locale, options) {
18
- const { readers, allowCacheWrite = false, fetch, handlers, plugins, } = options;
19
- const loader = resolveLoaderOptions(config, "server");
18
+ const { loader, readers, allowCacheWrite = false, fetch, handlers, plugins, } = options;
19
+ const loaderOptions = resolveLoaderOptions(config, "server");
20
20
  // Load messages
21
21
  let messages = {};
22
- if (loader) {
22
+ if (loaderOptions && !loader) {
23
23
  const loaded = await loadMessages({
24
24
  config,
25
25
  locale,
@@ -29,6 +29,9 @@ async function initTranslator(config, locale, options) {
29
29
  });
30
30
  messages = loaded || {};
31
31
  }
32
+ if (loader) {
33
+ messages = await loader(config, locale);
34
+ }
32
35
  // Create immutable translator snapshot
33
36
  return createTranslator({ config, locale, messages, handlers, plugins });
34
37
  }
@@ -14,14 +14,15 @@ import { getTranslator as getTranslator$1 } from '../../server/helpers/get-trans
14
14
  * @platform Express
15
15
  */
16
16
  async function getTranslator(config, req, params) {
17
- const { preKey, handlers, plugins, readers, allowCacheWrite } = params || {};
17
+ const { loader, readers, allowCacheWrite, handlers, plugins, preKey } = params || {};
18
18
  return getTranslator$1(config, {
19
19
  locale: req.intor?.locale || config.defaultLocale,
20
- preKey,
21
- handlers,
22
- plugins,
20
+ loader,
23
21
  readers,
24
22
  allowCacheWrite,
23
+ handlers,
24
+ plugins,
25
+ preKey,
25
26
  });
26
27
  }
27
28
 
@@ -9,14 +9,15 @@ import { initTranslator } from '../translator/init-translator.js';
9
9
  * Get a server-side translator for the current execution context.
10
10
  */
11
11
  async function getTranslator(config, params) {
12
- const { locale, readers, allowCacheWrite, fetch, preKey, handlers, plugins } = params;
12
+ const { locale, loader, readers, allowCacheWrite = false, fetch, handlers, plugins, preKey, } = params;
13
13
  // Initialize a locale-bound translator snapshot with messages loaded
14
14
  const translator = await initTranslator(config, locale, {
15
+ loader,
15
16
  readers,
16
17
  allowCacheWrite,
17
18
  fetch: fetch || globalThis.fetch,
18
- plugins,
19
19
  handlers,
20
+ plugins,
20
21
  });
21
22
  const scoped = translator.scoped(preKey);
22
23
  return {
@@ -15,11 +15,11 @@ import 'node:fs/promises';
15
15
  * - Creates an immutable Translator instance for server usage
16
16
  */
17
17
  async function initTranslator(config, locale, options) {
18
- const { readers, allowCacheWrite = false, fetch, handlers, plugins, } = options;
19
- const loader = resolveLoaderOptions(config);
18
+ const { loader, readers, allowCacheWrite = false, fetch, handlers, plugins, } = options;
19
+ const loaderOptions = resolveLoaderOptions(config);
20
20
  // Load messages
21
21
  let messages = {};
22
- if (loader) {
22
+ if (loaderOptions && !loader) {
23
23
  const loaded = await loadMessages({
24
24
  config,
25
25
  locale,
@@ -29,6 +29,9 @@ async function initTranslator(config, locale, options) {
29
29
  });
30
30
  messages = loaded || {};
31
31
  }
32
+ if (loader) {
33
+ messages = await loader(config, locale);
34
+ }
32
35
  // Create immutable translator snapshot
33
36
  return createTranslator({ config, locale, messages, handlers, plugins });
34
37
  }
@@ -14,14 +14,15 @@ import { getTranslator as getTranslator$1 } from '../../server/helpers/get-trans
14
14
  * @platform Fastify
15
15
  */
16
16
  async function getTranslator(config, request, params) {
17
- const { preKey, handlers, plugins, readers, allowCacheWrite } = params || {};
17
+ const { loader, readers, allowCacheWrite, handlers, plugins, preKey } = params || {};
18
18
  return getTranslator$1(config, {
19
19
  locale: request.intor?.locale || config.defaultLocale,
20
- preKey,
21
- handlers,
22
- plugins,
20
+ loader,
23
21
  readers,
24
22
  allowCacheWrite,
23
+ handlers,
24
+ plugins,
25
+ preKey,
25
26
  });
26
27
  }
27
28
 
@@ -9,14 +9,15 @@ import { initTranslator } from '../translator/init-translator.js';
9
9
  * Get a server-side translator for the current execution context.
10
10
  */
11
11
  async function getTranslator(config, params) {
12
- const { locale, readers, allowCacheWrite, fetch, preKey, handlers, plugins } = params;
12
+ const { locale, loader, readers, allowCacheWrite = false, fetch, handlers, plugins, preKey, } = params;
13
13
  // Initialize a locale-bound translator snapshot with messages loaded
14
14
  const translator = await initTranslator(config, locale, {
15
+ loader,
15
16
  readers,
16
17
  allowCacheWrite,
17
18
  fetch: fetch || globalThis.fetch,
18
- plugins,
19
19
  handlers,
20
+ plugins,
20
21
  });
21
22
  const scoped = translator.scoped(preKey);
22
23
  return {
@@ -15,11 +15,11 @@ import 'node:fs/promises';
15
15
  * - Creates an immutable Translator instance for server usage
16
16
  */
17
17
  async function initTranslator(config, locale, options) {
18
- const { readers, allowCacheWrite = false, fetch, handlers, plugins, } = options;
19
- const loader = resolveLoaderOptions(config);
18
+ const { loader, readers, allowCacheWrite = false, fetch, handlers, plugins, } = options;
19
+ const loaderOptions = resolveLoaderOptions(config);
20
20
  // Load messages
21
21
  let messages = {};
22
- if (loader) {
22
+ if (loaderOptions && !loader) {
23
23
  const loaded = await loadMessages({
24
24
  config,
25
25
  locale,
@@ -29,6 +29,9 @@ async function initTranslator(config, locale, options) {
29
29
  });
30
30
  messages = loaded || {};
31
31
  }
32
+ if (loader) {
33
+ messages = await loader(config, locale);
34
+ }
32
35
  // Create immutable translator snapshot
33
36
  return createTranslator({ config, locale, messages, handlers, plugins });
34
37
  }
@@ -15,14 +15,15 @@ import { getLocale } from './get-locale.js';
15
15
  * @platform Next.js
16
16
  */
17
17
  async function getTranslator(config, params) {
18
- const { preKey, handlers, plugins, readers, allowCacheWrite } = params || {};
18
+ const { loader, readers, allowCacheWrite, handlers, plugins, preKey } = params || {};
19
19
  return getTranslator$1(config, {
20
20
  locale: await getLocale(config),
21
- preKey,
22
- handlers,
23
- plugins,
21
+ loader,
24
22
  readers,
25
23
  allowCacheWrite,
24
+ handlers,
25
+ plugins,
26
+ preKey,
26
27
  });
27
28
  }
28
29
 
@@ -9,14 +9,15 @@ import { initTranslator } from '../translator/init-translator.js';
9
9
  * Get a server-side translator for the current execution context.
10
10
  */
11
11
  async function getTranslator(config, params) {
12
- const { locale, readers, allowCacheWrite, fetch, preKey, handlers, plugins } = params;
12
+ const { locale, loader, readers, allowCacheWrite = false, fetch, handlers, plugins, preKey, } = params;
13
13
  // Initialize a locale-bound translator snapshot with messages loaded
14
14
  const translator = await initTranslator(config, locale, {
15
+ loader,
15
16
  readers,
16
17
  allowCacheWrite,
17
18
  fetch: fetch || globalThis.fetch,
18
- plugins,
19
19
  handlers,
20
+ plugins,
20
21
  });
21
22
  const scoped = translator.scoped(preKey);
22
23
  return {
@@ -15,11 +15,11 @@ import 'node:fs/promises';
15
15
  * - Creates an immutable Translator instance for server usage
16
16
  */
17
17
  async function initTranslator(config, locale, options) {
18
- const { readers, allowCacheWrite = false, fetch, handlers, plugins, } = options;
19
- const loader = resolveLoaderOptions(config, "server");
18
+ const { loader, readers, allowCacheWrite = false, fetch, handlers, plugins, } = options;
19
+ const loaderOptions = resolveLoaderOptions(config, "server");
20
20
  // Load messages
21
21
  let messages = {};
22
- if (loader) {
22
+ if (loaderOptions && !loader) {
23
23
  const loaded = await loadMessages({
24
24
  config,
25
25
  locale,
@@ -29,6 +29,9 @@ async function initTranslator(config, locale, options) {
29
29
  });
30
30
  messages = loaded || {};
31
31
  }
32
+ if (loader) {
33
+ messages = await loader(config, locale);
34
+ }
32
35
  // Create immutable translator snapshot
33
36
  return createTranslator({ config, locale, messages, handlers, plugins });
34
37
  }
@@ -1 +1 @@
1
- export { intor, type IntorValue, loadMessages, clearMessagesPool, getTranslator, } from "../../src/server";
1
+ export { intor, type IntorValue, clearMessagesPool, getTranslator, type MessagesLoader, } from "../../src/server";
@@ -1,5 +1,4 @@
1
1
  import type { IntorValue } from "../provider";
2
2
  import type { IntorResolvedConfig } from "../../../config";
3
- import type { GenConfigKeys } from "../../../core";
4
3
  import type { Locale, LocaleMessages } from "intor-translator";
5
- export declare function useIntor<CK extends GenConfigKeys = "__default__">(config: IntorResolvedConfig, loader: (config: IntorResolvedConfig, locale: Locale) => Promise<LocaleMessages>): Omit<IntorValue<CK>, "handlers" | "plugins">;
4
+ export declare function useIntor(config: IntorResolvedConfig, loader: (config: IntorResolvedConfig, locale: Locale) => Promise<LocaleMessages>): Omit<IntorValue, "handlers" | "plugins">;
@@ -1,5 +1,4 @@
1
1
  import type { IntorContextValue, IntorProviderProps } from "./types";
2
- import type { GenConfigKeys } from "../../../core";
3
2
  import * as React from "react";
4
3
  export declare const IntorContext: React.Context<IntorContextValue | undefined>;
5
- export declare function IntorProvider<CK extends GenConfigKeys = "__default__">({ value: { config, locale: initialLocale, messages, handlers, plugins, onLocaleChange, isLoading: externalIsLoading, }, children, }: IntorProviderProps<CK>): import("react/jsx-runtime").JSX.Element;
4
+ export declare function IntorProvider({ value: { config, locale: initialLocale, messages, handlers, plugins, onLocaleChange, isLoading: externalIsLoading, }, children, }: IntorProviderProps): import("react/jsx-runtime").JSX.Element;
@@ -1,24 +1,23 @@
1
1
  import type { IntorResolvedConfig } from "../../../config";
2
- import type { GenConfigKeys, GenLocale, GenMessages } from "../../../core";
3
- import type { TranslateHandlers, TranslateHook, TranslatorPlugin } from "intor-translator";
2
+ import type { Locale, LocaleMessages, TranslateHandlers, TranslateHook, TranslatorPlugin } from "intor-translator";
4
3
  import type { Translator } from "intor-translator";
5
4
  import type * as React from "react";
6
- export interface IntorValue<CK extends GenConfigKeys = "__default__"> {
5
+ export interface IntorValue {
7
6
  config: IntorResolvedConfig;
8
- locale: GenLocale<CK>;
9
- messages?: Readonly<GenMessages<CK>>;
7
+ locale: Locale;
8
+ messages?: Readonly<LocaleMessages>;
10
9
  isLoading?: boolean;
11
- onLocaleChange?: (newLocale: GenLocale<CK>) => Promise<void> | void;
10
+ onLocaleChange?: (newLocale: Locale) => Promise<void> | void;
12
11
  handlers?: TranslateHandlers;
13
12
  plugins?: (TranslatorPlugin | TranslateHook)[];
14
13
  }
15
- export interface IntorProviderProps<CK extends GenConfigKeys = "__default__"> {
16
- value: IntorValue<CK>;
14
+ export interface IntorProviderProps {
15
+ value: IntorValue;
17
16
  children: React.ReactNode;
18
17
  }
19
- export type IntorContextValue<CK extends GenConfigKeys = "__default__"> = {
18
+ export type IntorContextValue = {
20
19
  config: IntorResolvedConfig;
21
- locale: GenLocale<CK>;
22
- setLocale: (locale: GenLocale<CK>) => void;
20
+ locale: Locale;
21
+ setLocale: (locale: Locale) => void;
23
22
  translator: Translator<unknown>;
24
23
  };
@@ -1,3 +1,2 @@
1
1
  import type { IntorContextValue } from "./types";
2
- import type { GenConfigKeys } from "../../../core";
3
- export declare function useIntorContext<CK extends GenConfigKeys = "__default__">(): IntorContextValue<CK>;
2
+ export declare function useIntorContext(): IntorContextValue;
@@ -1,8 +1,10 @@
1
1
  import type { IntorResolvedConfig } from "../../config";
2
2
  import type { LocalizedPreKey, TranslateHandlers, TranslateHook, TranslatorPlugin } from "intor-translator";
3
3
  import { type GenConfigKeys, type GenLocale, type GenMessages, type GenReplacements, type GenRich, type MessagesReaders, type RuntimeFetch, type TranslatorInstance } from "../../core";
4
+ import { type MessagesLoader } from "../translator";
4
5
  export interface GetTranslatorParams<CK extends GenConfigKeys = "__default__"> {
5
6
  locale: GenLocale<CK> | (string & {});
7
+ loader?: MessagesLoader;
6
8
  readers?: MessagesReaders;
7
9
  allowCacheWrite?: boolean;
8
10
  fetch?: RuntimeFetch;
@@ -1,3 +1,4 @@
1
1
  export { intor, type IntorValue } from "./intor";
2
- export { loadMessages, clearMessagesPool } from "./messages";
2
+ export { clearMessagesPool } from "./messages";
3
3
  export { getTranslator, type GetTranslatorParams } from "./helpers";
4
+ export type { MessagesLoader } from "./translator";
@@ -1 +1,2 @@
1
1
  export { initTranslator } from "../translator/init-translator";
2
+ export type { MessagesLoader } from "./types";
@@ -1,8 +1,10 @@
1
+ import type { MessagesLoader } from "./types";
1
2
  import type { IntorResolvedConfig } from "../../config";
2
3
  import type { Locale, Translator } from "intor-translator";
3
4
  import { type CreateTranslatorParams } from "../../core";
4
5
  import { type LoadMessagesParams } from "../messages";
5
6
  interface InitTranslatorOptions extends Pick<LoadMessagesParams, "readers" | "allowCacheWrite" | "fetch">, Pick<CreateTranslatorParams, "handlers" | "plugins"> {
7
+ loader?: MessagesLoader;
6
8
  }
7
9
  /**
8
10
  * Initialize a locale-bound Translator snapshot.
@@ -0,0 +1,9 @@
1
+ import type { IntorResolvedConfig } from "../../config";
2
+ import type { LocaleMessages } from "intor-translator";
3
+ /**
4
+ * Runtime message loader override.
5
+ *
6
+ * Loads locale messages imperatively at runtime, bypassing
7
+ * message loading defined in the Intor config.
8
+ */
9
+ export type MessagesLoader = (config: IntorResolvedConfig, locale: string) => Promise<LocaleMessages>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "intor",
3
- "version": "2.4.7",
3
+ "version": "2.4.8",
4
4
  "description": "The i18n library for modern JavaScript",
5
5
  "author": "Yiming Liao",
6
6
  "homepage": "https://github.com/yiming-liao/intor#readme",