@intlayer/use-intl 9.0.0-canary.2

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.
Files changed (106) hide show
  1. package/README.md +330 -0
  2. package/dist/cjs/_virtual/_rolldown/runtime.cjs +29 -0
  3. package/dist/cjs/core/createFormatter.cjs +23 -0
  4. package/dist/cjs/core/createFormatter.cjs.map +1 -0
  5. package/dist/cjs/core/createTranslator.cjs +30 -0
  6. package/dist/cjs/core/createTranslator.cjs.map +1 -0
  7. package/dist/cjs/core/errors.cjs +42 -0
  8. package/dist/cjs/core/errors.cjs.map +1 -0
  9. package/dist/cjs/core/hasLocale.cjs +23 -0
  10. package/dist/cjs/core/hasLocale.cjs.map +1 -0
  11. package/dist/cjs/core/index.cjs +13 -0
  12. package/dist/cjs/core/initializeConfig.cjs +25 -0
  13. package/dist/cjs/core/initializeConfig.cjs.map +1 -0
  14. package/dist/cjs/core/types.cjs +0 -0
  15. package/dist/cjs/index.cjs +30 -0
  16. package/dist/cjs/plugin/index.cjs +80 -0
  17. package/dist/cjs/plugin/index.cjs.map +1 -0
  18. package/dist/cjs/react/IntlProvider.cjs +30 -0
  19. package/dist/cjs/react/IntlProvider.cjs.map +1 -0
  20. package/dist/cjs/react/helpers.cjs +49 -0
  21. package/dist/cjs/react/helpers.cjs.map +1 -0
  22. package/dist/cjs/react/index.cjs +27 -0
  23. package/dist/cjs/react/index.cjs.map +1 -0
  24. package/dist/cjs/react/useLocale.cjs +20 -0
  25. package/dist/cjs/react/useLocale.cjs.map +1 -0
  26. package/dist/cjs/react/useTranslations.cjs +39 -0
  27. package/dist/cjs/react/useTranslations.cjs.map +1 -0
  28. package/dist/cjs/shared/intlFormatter.cjs +63 -0
  29. package/dist/cjs/shared/intlFormatter.cjs.map +1 -0
  30. package/dist/cjs/shared/namespaceTranslator.cjs +122 -0
  31. package/dist/cjs/shared/namespaceTranslator.cjs.map +1 -0
  32. package/dist/cjs/shared/translateFunctionTypes.cjs +0 -0
  33. package/dist/cjs/useDictionary.cjs +37 -0
  34. package/dist/cjs/useDictionary.cjs.map +1 -0
  35. package/dist/cjs/useDictionaryDynamic.cjs +31 -0
  36. package/dist/cjs/useDictionaryDynamic.cjs.map +1 -0
  37. package/dist/esm/core/createFormatter.mjs +22 -0
  38. package/dist/esm/core/createFormatter.mjs.map +1 -0
  39. package/dist/esm/core/createTranslator.mjs +28 -0
  40. package/dist/esm/core/createTranslator.mjs.map +1 -0
  41. package/dist/esm/core/errors.mjs +39 -0
  42. package/dist/esm/core/errors.mjs.map +1 -0
  43. package/dist/esm/core/hasLocale.mjs +21 -0
  44. package/dist/esm/core/hasLocale.mjs.map +1 -0
  45. package/dist/esm/core/index.mjs +7 -0
  46. package/dist/esm/core/initializeConfig.mjs +23 -0
  47. package/dist/esm/core/initializeConfig.mjs.map +1 -0
  48. package/dist/esm/core/types.mjs +0 -0
  49. package/dist/esm/index.mjs +14 -0
  50. package/dist/esm/plugin/index.mjs +76 -0
  51. package/dist/esm/plugin/index.mjs.map +1 -0
  52. package/dist/esm/react/IntlProvider.mjs +28 -0
  53. package/dist/esm/react/IntlProvider.mjs.map +1 -0
  54. package/dist/esm/react/helpers.mjs +44 -0
  55. package/dist/esm/react/helpers.mjs.map +1 -0
  56. package/dist/esm/react/index.mjs +17 -0
  57. package/dist/esm/react/index.mjs.map +1 -0
  58. package/dist/esm/react/useLocale.mjs +18 -0
  59. package/dist/esm/react/useLocale.mjs.map +1 -0
  60. package/dist/esm/react/useTranslations.mjs +37 -0
  61. package/dist/esm/react/useTranslations.mjs.map +1 -0
  62. package/dist/esm/shared/intlFormatter.mjs +61 -0
  63. package/dist/esm/shared/intlFormatter.mjs.map +1 -0
  64. package/dist/esm/shared/namespaceTranslator.mjs +119 -0
  65. package/dist/esm/shared/namespaceTranslator.mjs.map +1 -0
  66. package/dist/esm/shared/translateFunctionTypes.mjs +0 -0
  67. package/dist/esm/useDictionary.mjs +35 -0
  68. package/dist/esm/useDictionary.mjs.map +1 -0
  69. package/dist/esm/useDictionaryDynamic.mjs +29 -0
  70. package/dist/esm/useDictionaryDynamic.mjs.map +1 -0
  71. package/dist/types/core/createFormatter.d.ts +21 -0
  72. package/dist/types/core/createFormatter.d.ts.map +1 -0
  73. package/dist/types/core/createTranslator.d.ts +56 -0
  74. package/dist/types/core/createTranslator.d.ts.map +1 -0
  75. package/dist/types/core/errors.d.ts +31 -0
  76. package/dist/types/core/errors.d.ts.map +1 -0
  77. package/dist/types/core/hasLocale.d.ts +18 -0
  78. package/dist/types/core/hasLocale.d.ts.map +1 -0
  79. package/dist/types/core/index.d.ts +7 -0
  80. package/dist/types/core/initializeConfig.d.ts +14 -0
  81. package/dist/types/core/initializeConfig.d.ts.map +1 -0
  82. package/dist/types/core/types.d.ts +2 -0
  83. package/dist/types/index.d.ts +14 -0
  84. package/dist/types/plugin/index.d.ts +22 -0
  85. package/dist/types/plugin/index.d.ts.map +1 -0
  86. package/dist/types/react/IntlProvider.d.ts +29 -0
  87. package/dist/types/react/IntlProvider.d.ts.map +1 -0
  88. package/dist/types/react/helpers.d.ts +31 -0
  89. package/dist/types/react/helpers.d.ts.map +1 -0
  90. package/dist/types/react/index.d.ts +16 -0
  91. package/dist/types/react/index.d.ts.map +1 -0
  92. package/dist/types/react/useLocale.d.ts +12 -0
  93. package/dist/types/react/useLocale.d.ts.map +1 -0
  94. package/dist/types/react/useTranslations.d.ts +44 -0
  95. package/dist/types/react/useTranslations.d.ts.map +1 -0
  96. package/dist/types/shared/intlFormatter.d.ts +19 -0
  97. package/dist/types/shared/intlFormatter.d.ts.map +1 -0
  98. package/dist/types/shared/namespaceTranslator.d.ts +51 -0
  99. package/dist/types/shared/namespaceTranslator.d.ts.map +1 -0
  100. package/dist/types/shared/translateFunctionTypes.d.ts +34 -0
  101. package/dist/types/shared/translateFunctionTypes.d.ts.map +1 -0
  102. package/dist/types/useDictionary.d.ts +23 -0
  103. package/dist/types/useDictionary.d.ts.map +1 -0
  104. package/dist/types/useDictionaryDynamic.d.ts +18 -0
  105. package/dist/types/useDictionaryDynamic.d.ts.map +1 -0
  106. package/package.json +131 -0
@@ -0,0 +1,28 @@
1
+ import { createNamespaceTranslator } from "../shared/namespaceTranslator.mjs";
2
+ import { log } from "@intlayer/config/built";
3
+ import { CYAN } from "@intlayer/config/colors";
4
+ import { colorize, getAppLogger } from "@intlayer/config/logger";
5
+
6
+ //#region src/core/createTranslator.ts
7
+ /**
8
+ * Drop-in for use-intl's `createTranslator`.
9
+ *
10
+ * Returns a translate function (with `rich`, `markup`, `raw`, `has`) that
11
+ * resolves messages from Intlayer's compiled dictionaries for the given
12
+ * `locale`. Messages support ICU MessageFormat syntax.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const t = createTranslator({ locale: 'en', namespace: 'about' });
17
+ * t('counter.label'); // ✓ typed
18
+ * t('items', { count: 3 }); // ICU plural
19
+ * ```
20
+ */
21
+ const createTranslator = (({ locale, namespace, messages: _messages }) => {
22
+ if (_messages !== void 0) getAppLogger({ log })(`${colorize("createTranslator", CYAN)} do not pass the messages option with intlayer. Messages are loaded automatically under the hood for bundle optimization reason`);
23
+ return createNamespaceTranslator(locale, namespace);
24
+ });
25
+
26
+ //#endregion
27
+ export { createTranslator };
28
+ //# sourceMappingURL=createTranslator.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createTranslator.mjs","names":[],"sources":["../../../src/core/createTranslator.ts"],"sourcesContent":["import { log } from '@intlayer/config/built';\nimport { CYAN } from '@intlayer/config/colors';\nimport { colorize, getAppLogger } from '@intlayer/config/logger';\nimport type {\n DictionaryKeys,\n LocalesValues,\n} from '@intlayer/types/module_augmentation';\nimport { createNamespaceTranslator } from '../shared/namespaceTranslator';\nimport type {\n LooseTranslateFunction,\n TranslateFunction,\n} from '../shared/translateFunctionTypes';\n\n/**\n * Configuration accepted by {@link createTranslator}.\n *\n * Mirrors use-intl's `IntlConfig`, but `messages`, `formats`, `now`,\n * `timeZone`, `onError`, and `getMessageFallback` have no effect — Intlayer\n * resolves content from its own compiled dictionaries.\n */\nexport type CreateTranslatorConfig<N extends DictionaryKeys> = {\n /** The locale dictionaries are resolved for. */\n locale: LocalesValues;\n /** A bare dictionary key, or a nested `'dictionary.scope'` namespace. */\n namespace?: N | (string & {});\n /**\n * @deprecated has no use case with intlayer. Messages are loaded\n * automatically under the hood for bundle optimization reason.\n */\n messages?: never;\n} & Record<string, unknown>;\n\n/**\n * Overload set for {@link createTranslator}:\n *\n * 1. A bare dictionary key → fully-typed `t()` (autocompleted dot-paths).\n * 2. A nested namespace `'dictionary.sub.scope'` → `t()` accepts relative\n * `string` paths, matching use-intl's scoped-namespace behaviour.\n * 3. No namespace → root scope; the first segment of each key designates\n * the dictionary (`t('about.title')`).\n */\ntype CreateTranslator = {\n <N extends DictionaryKeys>(\n config: CreateTranslatorConfig<N> & { namespace: N }\n ): TranslateFunction<N>;\n (\n config: CreateTranslatorConfig<DictionaryKeys> & {\n namespace: `${string}.${string}`;\n }\n ): LooseTranslateFunction;\n (config: CreateTranslatorConfig<DictionaryKeys>): LooseTranslateFunction;\n};\n\n/**\n * Drop-in for use-intl's `createTranslator`.\n *\n * Returns a translate function (with `rich`, `markup`, `raw`, `has`) that\n * resolves messages from Intlayer's compiled dictionaries for the given\n * `locale`. Messages support ICU MessageFormat syntax.\n *\n * @example\n * ```ts\n * const t = createTranslator({ locale: 'en', namespace: 'about' });\n * t('counter.label'); // ✓ typed\n * t('items', { count: 3 }); // ICU plural\n * ```\n */\nexport const createTranslator = (({\n locale,\n namespace,\n messages: _messages,\n}: CreateTranslatorConfig<DictionaryKeys>) => {\n if (process.env.NODE_ENV === 'development' && _messages !== undefined) {\n const appLogger = getAppLogger({ log });\n appLogger(\n `${colorize('createTranslator', CYAN)} do not pass the messages option with intlayer. Messages are loaded automatically under the hood for bundle optimization reason`\n );\n }\n\n return createNamespaceTranslator(locale, namespace);\n}) as CreateTranslator;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAmEA,MAAa,qBAAqB,EAChC,QACA,WACA,UAAU,gBACkC;AAC5C,KAA8C,cAAc,OAE1D,CADkB,aAAa,EAAE,KAAK,CAC7B,CACP,GAAG,SAAS,oBAAoB,KAAK,CAAC,iIACvC;AAGH,QAAO,0BAA0B,QAAQ,UAAU"}
@@ -0,0 +1,39 @@
1
+ //#region src/core/errors.ts
2
+ /**
3
+ * Drop-in for use-intl's `IntlErrorCode` enum.
4
+ *
5
+ * Re-implemented locally (rather than re-exported) because `use-intl` is
6
+ * aliased to this package at build time, so importing its runtime value would
7
+ * be circular.
8
+ */
9
+ let IntlErrorCode = /* @__PURE__ */ function(IntlErrorCode) {
10
+ IntlErrorCode["MISSING_MESSAGE"] = "MISSING_MESSAGE";
11
+ IntlErrorCode["MISSING_FORMAT"] = "MISSING_FORMAT";
12
+ IntlErrorCode["ENVIRONMENT_FALLBACK"] = "ENVIRONMENT_FALLBACK";
13
+ IntlErrorCode["INSUFFICIENT_PATH"] = "INSUFFICIENT_PATH";
14
+ IntlErrorCode["INVALID_MESSAGE"] = "INVALID_MESSAGE";
15
+ IntlErrorCode["INVALID_KEY"] = "INVALID_KEY";
16
+ IntlErrorCode["FORMATTING_ERROR"] = "FORMATTING_ERROR";
17
+ return IntlErrorCode;
18
+ }({});
19
+ /**
20
+ * Drop-in for use-intl's `IntlError`.
21
+ *
22
+ * Carries the {@link IntlErrorCode} and the original message, matching
23
+ * use-intl's error shape so consumer `onError` handlers keep working.
24
+ */
25
+ var IntlError = class extends Error {
26
+ code;
27
+ originalMessage;
28
+ constructor(code, originalMessage) {
29
+ let message = code;
30
+ if (originalMessage) message += `: ${originalMessage}`;
31
+ super(message);
32
+ this.code = code;
33
+ if (originalMessage) this.originalMessage = originalMessage;
34
+ }
35
+ };
36
+
37
+ //#endregion
38
+ export { IntlError, IntlErrorCode };
39
+ //# sourceMappingURL=errors.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.mjs","names":[],"sources":["../../../src/core/errors.ts"],"sourcesContent":["/**\n * Drop-in for use-intl's `IntlErrorCode` enum.\n *\n * Re-implemented locally (rather than re-exported) because `use-intl` is\n * aliased to this package at build time, so importing its runtime value would\n * be circular.\n */\nexport enum IntlErrorCode {\n MISSING_MESSAGE = 'MISSING_MESSAGE',\n MISSING_FORMAT = 'MISSING_FORMAT',\n ENVIRONMENT_FALLBACK = 'ENVIRONMENT_FALLBACK',\n INSUFFICIENT_PATH = 'INSUFFICIENT_PATH',\n INVALID_MESSAGE = 'INVALID_MESSAGE',\n INVALID_KEY = 'INVALID_KEY',\n FORMATTING_ERROR = 'FORMATTING_ERROR',\n}\n\n/**\n * Drop-in for use-intl's `IntlError`.\n *\n * Carries the {@link IntlErrorCode} and the original message, matching\n * use-intl's error shape so consumer `onError` handlers keep working.\n */\nexport class IntlError extends Error {\n public readonly code: IntlErrorCode;\n public readonly originalMessage: string | undefined;\n\n constructor(code: IntlErrorCode, originalMessage?: string) {\n let message: string = code;\n if (originalMessage) {\n message += `: ${originalMessage}`;\n }\n super(message);\n\n this.code = code;\n if (originalMessage) {\n this.originalMessage = originalMessage;\n }\n }\n}\n"],"mappings":";;;;;;;;AAOA,IAAY,gBAAL;AACL;AACA;AACA;AACA;AACA;AACA;AACA;;KACD;;;;;;;AAQD,IAAa,YAAb,cAA+B,MAAM;CACnC,AAAgB;CAChB,AAAgB;CAEhB,YAAY,MAAqB,iBAA0B;EACzD,IAAI,UAAkB;AACtB,MAAI,gBACF,YAAW,KAAK;AAElB,QAAM,QAAQ;AAEd,OAAK,OAAO;AACZ,MAAI,gBACF,MAAK,kBAAkB"}
@@ -0,0 +1,21 @@
1
+ import { internationalization } from "@intlayer/config/built";
2
+
3
+ //#region src/core/hasLocale.ts
4
+ /**
5
+ * Drop-in for use-intl's `hasLocale`.
6
+ *
7
+ * Narrows a locale candidate to the given locales list. When the list is
8
+ * omitted, the locales configured in intlayer are used.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * if (hasLocale(['en', 'fr'], requested)) { … }
13
+ * ```
14
+ */
15
+ const hasLocale = ((locales, candidate) => {
16
+ return (locales ?? internationalization?.locales?.map(String) ?? []).includes(candidate);
17
+ });
18
+
19
+ //#endregion
20
+ export { hasLocale };
21
+ //# sourceMappingURL=hasLocale.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hasLocale.mjs","names":[],"sources":["../../../src/core/hasLocale.ts"],"sourcesContent":["import { internationalization } from '@intlayer/config/built';\nimport type { hasLocale as _hasLocale } from 'use-intl';\n\n/**\n * Drop-in for use-intl's `hasLocale`.\n *\n * Narrows a locale candidate to the given locales list. When the list is\n * omitted, the locales configured in intlayer are used.\n *\n * @example\n * ```ts\n * if (hasLocale(['en', 'fr'], requested)) { … }\n * ```\n */\nexport const hasLocale: typeof _hasLocale = ((\n locales: readonly string[],\n candidate: unknown\n): boolean => {\n const availableLocales =\n locales ?? internationalization?.locales?.map(String) ?? [];\n return availableLocales.includes(candidate as string);\n}) as typeof _hasLocale;\n"],"mappings":";;;;;;;;;;;;;;AAcA,MAAa,cACX,SACA,cACY;AAGZ,SADE,WAAW,sBAAsB,SAAS,IAAI,OAAO,IAAI,EAAE,EACrC,SAAS,UAAoB"}
@@ -0,0 +1,7 @@
1
+ import { createFormatter } from "./createFormatter.mjs";
2
+ import { createTranslator } from "./createTranslator.mjs";
3
+ import { IntlError, IntlErrorCode } from "./errors.mjs";
4
+ import { hasLocale } from "./hasLocale.mjs";
5
+ import { initializeConfig } from "./initializeConfig.mjs";
6
+
7
+ export { IntlError, IntlErrorCode, createFormatter, createTranslator, hasLocale, initializeConfig };
@@ -0,0 +1,23 @@
1
+ //#region src/core/initializeConfig.ts
2
+ /**
3
+ * Drop-in for use-intl's `initializeConfig`.
4
+ *
5
+ * Enhances an incoming config with defaults. With intlayer, `messages` and
6
+ * `formats` are not consumed at runtime (content comes from compiled
7
+ * dictionaries) but the shape is preserved so callers keep type-checking.
8
+ */
9
+ const initializeConfig = ({ formats, getMessageFallback, messages, onError, ...rest }) => {
10
+ const finalOnError = onError ?? ((error) => error);
11
+ const finalGetMessageFallback = getMessageFallback ?? (({ key, namespace }) => [namespace, key].filter(Boolean).join("."));
12
+ return {
13
+ ...rest,
14
+ formats: formats || void 0,
15
+ messages: messages || void 0,
16
+ onError: finalOnError,
17
+ getMessageFallback: finalGetMessageFallback
18
+ };
19
+ };
20
+
21
+ //#endregion
22
+ export { initializeConfig };
23
+ //# sourceMappingURL=initializeConfig.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"initializeConfig.mjs","names":[],"sources":["../../../src/core/initializeConfig.ts"],"sourcesContent":["import type { initializeConfig as _initializeConfig } from 'use-intl';\n\n/**\n * Drop-in for use-intl's `initializeConfig`.\n *\n * Enhances an incoming config with defaults. With intlayer, `messages` and\n * `formats` are not consumed at runtime (content comes from compiled\n * dictionaries) but the shape is preserved so callers keep type-checking.\n */\nexport const initializeConfig: typeof _initializeConfig = ({\n formats,\n getMessageFallback,\n messages,\n onError,\n ...rest\n}) => {\n const finalOnError = onError ?? ((error) => error);\n const finalGetMessageFallback =\n getMessageFallback ?? (({ key, namespace }) => [namespace, key]\n .filter(Boolean)\n .join('.'));\n\n return {\n ...rest,\n formats: formats || undefined,\n messages: messages || undefined,\n onError: finalOnError,\n getMessageFallback: finalGetMessageFallback,\n };\n};\n"],"mappings":";;;;;;;;AASA,MAAa,oBAA8C,EACzD,SACA,oBACA,UACA,SACA,GAAG,WACC;CACJ,MAAM,eAAe,aAAa,UAAU;CAC5C,MAAM,0BACJ,wBAAwB,EAAE,KAAK,gBAAgB,CAAC,WAAW,IAAI,CAC5D,OAAO,QAAQ,CACf,KAAK,IAAI;AAEd,QAAO;EACL,GAAG;EACH,SAAS,WAAW;EACpB,UAAU,YAAY;EACtB,SAAS;EACT,oBAAoB;EACrB"}
File without changes
@@ -0,0 +1,14 @@
1
+ import { useDictionaryDynamic } from "./useDictionaryDynamic.mjs";
2
+ import { createFormatter } from "./core/createFormatter.mjs";
3
+ import { createTranslator } from "./core/createTranslator.mjs";
4
+ import { IntlError, IntlErrorCode } from "./core/errors.mjs";
5
+ import { hasLocale } from "./core/hasLocale.mjs";
6
+ import { initializeConfig } from "./core/initializeConfig.mjs";
7
+ import { IntlProvider } from "./react/IntlProvider.mjs";
8
+ import { useFormatter, useMessages, useNow, useTimeZone } from "./react/helpers.mjs";
9
+ import { useLocale } from "./react/useLocale.mjs";
10
+ import { useTranslations } from "./react/useTranslations.mjs";
11
+ import { useDictionary } from "./useDictionary.mjs";
12
+ import { _useExtracted } from "./react/index.mjs";
13
+
14
+ export { IntlError, IntlErrorCode, IntlProvider, _useExtracted, createFormatter, createTranslator, hasLocale, initializeConfig, useDictionary, useDictionaryDynamic, useFormatter, useLocale, useMessages, useNow, useTimeZone, useTranslations };
@@ -0,0 +1,76 @@
1
+ import * as ANSIColors from "@intlayer/config/colors";
2
+ import { colorize, getAppLogger } from "@intlayer/config/logger";
3
+ import { join } from "node:path";
4
+ import { runOnce } from "@intlayer/chokidar/utils";
5
+ import { getConfiguration } from "@intlayer/config/node";
6
+ import { intlayer } from "vite-intlayer";
7
+
8
+ //#region src/plugin/index.ts
9
+ /**
10
+ * Caller configurations for use-intl's translation entry points.
11
+ *
12
+ * Tells the intlayer field-usage analyser how to extract the dictionary key
13
+ * (namespace) and consumed fields from `useTranslations` / `createTranslator`
14
+ * call sites, enabling accurate dictionary pruning for projects using
15
+ * `@intlayer/use-intl`.
16
+ */
17
+ const USE_INTL_COMPAT_CALLERS = [{
18
+ callerName: "useTranslations",
19
+ importSources: [
20
+ "use-intl",
21
+ "use-intl/react",
22
+ "@intlayer/use-intl"
23
+ ],
24
+ namespace: {
25
+ from: "argument",
26
+ index: 0
27
+ },
28
+ translationFunction: "return-value"
29
+ }, {
30
+ callerName: "createTranslator",
31
+ importSources: [
32
+ "use-intl",
33
+ "use-intl/core",
34
+ "@intlayer/use-intl"
35
+ ],
36
+ namespace: {
37
+ from: "option",
38
+ argumentIndex: 0,
39
+ property: "namespace"
40
+ },
41
+ translationFunction: "return-value"
42
+ }];
43
+ /**
44
+ * A Vite plugin for use-intl compat that wraps vite-intlayer and configures
45
+ * resolve aliases so `use-intl` imports are served by `@intlayer/use-intl`.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * // vite.config.ts
50
+ * import useIntlVitePlugin from '@intlayer/use-intl/plugin';
51
+ *
52
+ * export default defineConfig({
53
+ * plugins: [useIntlVitePlugin()],
54
+ * });
55
+ * ```
56
+ */
57
+ const useIntlVitePlugin = (options) => {
58
+ const intlayerConfig = getConfiguration();
59
+ const appLogger = getAppLogger(intlayerConfig);
60
+ runOnce(join(intlayerConfig.system.baseDir, ".intlayer", "cache", "intlayer-issues-invitation.lock"), () => {
61
+ appLogger([colorize("Please report any issues you met on GitHub:", ANSIColors.GREY), colorize("https://github.com/aymericzip/intlayer/issues", ANSIColors.GREY_LIGHT)]);
62
+ }, { cacheTimeoutMs: 1e3 * 60 * 60 });
63
+ const basePlugins = intlayer({
64
+ ...options,
65
+ compatCallers: [...options?.compatCallers ?? [], ...USE_INTL_COMPAT_CALLERS]
66
+ });
67
+ const compatPlugin = {
68
+ name: "vite-use-intl-compat-plugin",
69
+ config: () => ({ resolve: { alias: { "use-intl": "@intlayer/use-intl" } } })
70
+ };
71
+ return [...Array.isArray(basePlugins) ? basePlugins : [basePlugins], compatPlugin];
72
+ };
73
+
74
+ //#endregion
75
+ export { useIntlVitePlugin as default, useIntlVitePlugin };
76
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../../src/plugin/index.ts"],"sourcesContent":["import { join } from 'node:path';\nimport { runOnce } from '@intlayer/chokidar/utils';\nimport * as ANSIColors from '@intlayer/config/colors';\nimport { colorize, getAppLogger } from '@intlayer/config/logger';\nimport { getConfiguration } from '@intlayer/config/node';\nimport type { PluginOption } from 'vite';\nimport { type CompatCallerConfig, intlayer } from 'vite-intlayer';\n\n/**\n * Caller configurations for use-intl's translation entry points.\n *\n * Tells the intlayer field-usage analyser how to extract the dictionary key\n * (namespace) and consumed fields from `useTranslations` / `createTranslator`\n * call sites, enabling accurate dictionary pruning for projects using\n * `@intlayer/use-intl`.\n */\nconst USE_INTL_COMPAT_CALLERS: CompatCallerConfig[] = [\n {\n callerName: 'useTranslations',\n importSources: ['use-intl', 'use-intl/react', '@intlayer/use-intl'],\n namespace: { from: 'argument', index: 0 },\n translationFunction: 'return-value',\n },\n {\n callerName: 'createTranslator',\n importSources: ['use-intl', 'use-intl/core', '@intlayer/use-intl'],\n namespace: { from: 'option', argumentIndex: 0, property: 'namespace' },\n translationFunction: 'return-value',\n },\n];\n\n/**\n * A Vite plugin for use-intl compat that wraps vite-intlayer and configures\n * resolve aliases so `use-intl` imports are served by `@intlayer/use-intl`.\n *\n * @example\n * ```ts\n * // vite.config.ts\n * import useIntlVitePlugin from '@intlayer/use-intl/plugin';\n *\n * export default defineConfig({\n * plugins: [useIntlVitePlugin()],\n * });\n * ```\n */\nexport const useIntlVitePlugin = (\n options?: Parameters<typeof intlayer>[0]\n): PluginOption[] => {\n const intlayerConfig = getConfiguration();\n const appLogger = getAppLogger(intlayerConfig);\n\n runOnce(\n join(\n intlayerConfig.system.baseDir,\n '.intlayer',\n 'cache',\n 'intlayer-issues-invitation.lock'\n ),\n () => {\n appLogger([\n colorize(\n 'Please report any issues you met on GitHub:',\n ANSIColors.GREY\n ),\n colorize(\n 'https://github.com/aymericzip/intlayer/issues',\n ANSIColors.GREY_LIGHT\n ),\n ]);\n },\n {\n cacheTimeoutMs: 1000 * 60 * 60, // 1 hour\n }\n );\n\n const basePlugins = intlayer({\n ...options,\n compatCallers: [\n ...(options?.compatCallers ?? []),\n ...USE_INTL_COMPAT_CALLERS,\n ],\n });\n\n const compatPlugin: PluginOption = {\n name: 'vite-use-intl-compat-plugin',\n config: () => ({\n resolve: {\n alias: {\n 'use-intl': '@intlayer/use-intl',\n },\n },\n }),\n };\n\n return [\n ...(Array.isArray(basePlugins) ? basePlugins : [basePlugins]),\n compatPlugin,\n ];\n};\n\nexport default useIntlVitePlugin;\n"],"mappings":";;;;;;;;;;;;;;;;AAgBA,MAAM,0BAAgD,CACpD;CACE,YAAY;CACZ,eAAe;EAAC;EAAY;EAAkB;EAAqB;CACnE,WAAW;EAAE,MAAM;EAAY,OAAO;EAAG;CACzC,qBAAqB;CACtB,EACD;CACE,YAAY;CACZ,eAAe;EAAC;EAAY;EAAiB;EAAqB;CAClE,WAAW;EAAE,MAAM;EAAU,eAAe;EAAG,UAAU;EAAa;CACtE,qBAAqB;CACtB,CACF;;;;;;;;;;;;;;;AAgBD,MAAa,qBACX,YACmB;CACnB,MAAM,iBAAiB,kBAAkB;CACzC,MAAM,YAAY,aAAa,eAAe;AAE9C,SACE,KACE,eAAe,OAAO,SACtB,aACA,SACA,kCACD,QACK;AACJ,YAAU,CACR,SACE,+CACA,WAAW,KACZ,EACD,SACE,iDACA,WAAW,WACZ,CACF,CAAC;IAEJ,EACE,gBAAgB,MAAO,KAAK,IAC7B,CACF;CAED,MAAM,cAAc,SAAS;EAC3B,GAAG;EACH,eAAe,CACb,GAAI,SAAS,iBAAiB,EAAE,EAChC,GAAG,wBACJ;EACF,CAAC;CAEF,MAAM,eAA6B;EACjC,MAAM;EACN,eAAe,EACb,SAAS,EACP,OAAO,EACL,YAAY,sBACb,EACF,EACF;EACF;AAED,QAAO,CACL,GAAI,MAAM,QAAQ,YAAY,GAAG,cAAc,CAAC,YAAY,EAC5D,aACD"}
@@ -0,0 +1,28 @@
1
+ 'use client';
2
+
3
+ import { IntlayerProvider } from "react-intlayer";
4
+ import { jsx } from "react/jsx-runtime";
5
+ import { log } from "@intlayer/config/built";
6
+ import { CYAN } from "@intlayer/config/colors";
7
+ import { colorize, getAppLogger } from "@intlayer/config/logger";
8
+
9
+ //#region src/react/IntlProvider.tsx
10
+ /**
11
+ * Drop-in for use-intl's `IntlProvider`.
12
+ *
13
+ * Wraps Intlayer's `IntlayerProvider`, seeding the client locale from the
14
+ * `locale` prop. `messages`, `formats`, `now`, `timeZone`, `onError`, and
15
+ * `getMessageFallback` are accepted for API compatibility but have no effect —
16
+ * Intlayer uses its own compiled dictionaries.
17
+ */
18
+ const IntlProvider = ({ locale, children, messages: _messages, formats: _formats, now: _now, timeZone: _timeZone, onError: _onError, getMessageFallback: _getMessageFallback }) => {
19
+ if (typeof _messages !== "undefined") getAppLogger({ log })(`${colorize("IntlProvider", CYAN)} do not pass the messages prop with intlayer. Messages are loaded automatically under the hood for bundle optimization reason`);
20
+ return /* @__PURE__ */ jsx(IntlayerProvider, {
21
+ locale,
22
+ children
23
+ }, String(locale));
24
+ };
25
+
26
+ //#endregion
27
+ export { IntlProvider };
28
+ //# sourceMappingURL=IntlProvider.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IntlProvider.mjs","names":[],"sources":["../../../src/react/IntlProvider.tsx"],"sourcesContent":["'use client';\n\nimport { log } from '@intlayer/config/built';\nimport { CYAN } from '@intlayer/config/colors';\nimport { colorize, getAppLogger } from '@intlayer/config/logger';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\nimport { IntlayerProvider } from 'react-intlayer';\nimport type { ComponentProps } from 'react';\nimport type { IntlProvider as _IntlProvider } from 'use-intl';\n\n/**\n * Drop-in for use-intl's `IntlProvider`.\n *\n * Wraps Intlayer's `IntlayerProvider`, seeding the client locale from the\n * `locale` prop. `messages`, `formats`, `now`, `timeZone`, `onError`, and\n * `getMessageFallback` are accepted for API compatibility but have no effect —\n * Intlayer uses its own compiled dictionaries.\n */\nexport const IntlProvider: typeof _IntlProvider = ({\n locale,\n children,\n messages: _messages,\n formats: _formats,\n now: _now,\n timeZone: _timeZone,\n onError: _onError,\n getMessageFallback: _getMessageFallback,\n}) => {\n if (\n process.env.NODE_ENV === 'development' &&\n typeof _messages !== 'undefined'\n ) {\n const appLogger = getAppLogger({ log });\n appLogger(\n `${colorize('IntlProvider', CYAN)} do not pass the messages prop with intlayer. Messages are loaded automatically under the hood for bundle optimization reason`\n );\n }\n\n // `key={locale}` re-seeds the client locale context when the locale changes,\n // so consumers re-render in the new language.\n return (\n <IntlayerProvider\n key={String(locale)}\n locale={locale as LocalesValues}\n >\n {children}\n </IntlayerProvider>\n );\n};\n\n/**\n * Props accepted by {@link IntlProvider}. `messages` is deprecated — Intlayer\n * loads content from compiled dictionaries automatically.\n */\nexport type IntlProviderProps = Omit<\n ComponentProps<typeof _IntlProvider>,\n 'messages' | 'locale'\n> & {\n /**\n * @deprecated has no use case with intlayer. Messages are loaded\n * automatically under the hood for bundle optimization reason.\n */\n messages?: never;\n /** The active locale. */\n locale: LocalesValues;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;AAkBA,MAAa,gBAAsC,EACjD,QACA,UACA,UAAU,WACV,SAAS,UACT,KAAK,MACL,UAAU,WACV,SAAS,UACT,oBAAoB,0BAChB;AACJ,KAEE,OAAO,cAAc,YAGrB,CADkB,aAAa,EAAE,KAAK,CAC7B,CACP,GAAG,SAAS,gBAAgB,KAAK,CAAC,+HACnC;AAKH,QACE,oBAAC,kBAAD;EAEU;EAEP;EACgB,EAJZ,OAAO,OAAO,CAIF"}
@@ -0,0 +1,44 @@
1
+ 'use client';
2
+
3
+ import { buildIntlFormatter } from "../shared/intlFormatter.mjs";
4
+ import { useLocale } from "react-intlayer";
5
+ import { log } from "@intlayer/config/built";
6
+ import { CYAN } from "@intlayer/config/colors";
7
+ import { colorize, getAppLogger } from "@intlayer/config/logger";
8
+
9
+ //#region src/react/helpers.ts
10
+ /**
11
+ * Drop-in for use-intl's `useNow`.
12
+ * Returns the current `Date`. The `updateInterval` option is ignored.
13
+ */
14
+ const useNow = () => /* @__PURE__ */ new Date();
15
+ /**
16
+ * Drop-in for use-intl's `useTimeZone`.
17
+ * Returns the system time zone resolved from `Intl.DateTimeFormat`.
18
+ */
19
+ const useTimeZone = () => Intl.DateTimeFormat().resolvedOptions().timeZone;
20
+ /**
21
+ * Drop-in for use-intl's `useMessages`.
22
+ *
23
+ * @deprecated useMessages has no use case with intlayer.
24
+ * Messages are loaded automatically under the hood.
25
+ * @returns An empty object.
26
+ */
27
+ const useMessages = () => {
28
+ getAppLogger({ log })(`${colorize("useMessages", CYAN)} has no use case with intlayer. Messages are loaded automatically under the hood for bundle optimization reason`);
29
+ return {};
30
+ };
31
+ /**
32
+ * Drop-in for use-intl's `useFormatter`.
33
+ * Returns locale-aware formatters backed by the native `Intl.*` APIs:
34
+ * `dateTime`, `number`, `dateTimeRange`, `relativeTime`, `list`, and
35
+ * `displayName`.
36
+ */
37
+ const useFormatter = () => {
38
+ const { locale } = useLocale();
39
+ return buildIntlFormatter(locale ?? "en");
40
+ };
41
+
42
+ //#endregion
43
+ export { useFormatter, useMessages, useNow, useTimeZone };
44
+ //# sourceMappingURL=helpers.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.mjs","names":["useLocaleIntlayer"],"sources":["../../../src/react/helpers.ts"],"sourcesContent":["'use client';\n\nimport { log } from '@intlayer/config/built';\nimport { CYAN } from '@intlayer/config/colors';\nimport { colorize, getAppLogger } from '@intlayer/config/logger';\nimport { useLocale as useLocaleIntlayer } from 'react-intlayer';\nimport type {\n useFormatter as _useFormatter,\n useMessages as _useMessages,\n useNow as _useNow,\n useTimeZone as _useTimeZone,\n} from 'use-intl';\nimport { buildIntlFormatter } from '../shared/intlFormatter';\n\n/**\n * Drop-in for use-intl's `useNow`.\n * Returns the current `Date`. The `updateInterval` option is ignored.\n */\nexport const useNow: typeof _useNow = () => new Date();\n\n/**\n * Drop-in for use-intl's `useTimeZone`.\n * Returns the system time zone resolved from `Intl.DateTimeFormat`.\n */\nexport const useTimeZone: typeof _useTimeZone = () =>\n Intl.DateTimeFormat().resolvedOptions().timeZone;\n\n/**\n * Drop-in for use-intl's `useMessages`.\n *\n * @deprecated useMessages has no use case with intlayer.\n * Messages are loaded automatically under the hood.\n * @returns An empty object.\n */\nexport const useMessages: typeof _useMessages = () => {\n if (process.env.NODE_ENV === 'development') {\n const appLogger = getAppLogger({ log });\n appLogger(\n `${colorize('useMessages', CYAN)} has no use case with intlayer. Messages are loaded automatically under the hood for bundle optimization reason`\n );\n }\n\n return {} as ReturnType<typeof _useMessages>;\n};\n\n/**\n * Drop-in for use-intl's `useFormatter`.\n * Returns locale-aware formatters backed by the native `Intl.*` APIs:\n * `dateTime`, `number`, `dateTimeRange`, `relativeTime`, `list`, and\n * `displayName`.\n */\nexport const useFormatter: typeof _useFormatter = () => {\n const { locale } = useLocaleIntlayer();\n return buildIntlFormatter((locale as string) ?? 'en');\n};\n"],"mappings":";;;;;;;;;;;;;AAkBA,MAAa,+BAA+B,IAAI,MAAM;;;;;AAMtD,MAAa,oBACX,KAAK,gBAAgB,CAAC,iBAAiB,CAAC;;;;;;;;AAS1C,MAAa,oBAAyC;AAGlD,CADkB,aAAa,EAAE,KAAK,CAC7B,CACP,GAAG,SAAS,eAAe,KAAK,CAAC,iHAClC;AAGH,QAAO,EAAE;;;;;;;;AASX,MAAa,qBAA2C;CACtD,MAAM,EAAE,WAAWA,WAAmB;AACtC,QAAO,mBAAoB,UAAqB,KAAK"}
@@ -0,0 +1,17 @@
1
+ import { useDictionaryDynamic } from "../useDictionaryDynamic.mjs";
2
+ import { IntlProvider } from "./IntlProvider.mjs";
3
+ import { useFormatter, useMessages, useNow, useTimeZone } from "./helpers.mjs";
4
+ import { useLocale } from "./useLocale.mjs";
5
+ import { useTranslations } from "./useTranslations.mjs";
6
+ import { useDictionary } from "../useDictionary.mjs";
7
+
8
+ //#region src/react/index.ts
9
+ /**
10
+ * @internal Private use-intl export, kept so aliased `_useExtracted` imports
11
+ * resolve. Not part of the supported public surface.
12
+ */
13
+ const _useExtracted = () => ({});
14
+
15
+ //#endregion
16
+ export { IntlProvider, _useExtracted, useDictionary, useDictionaryDynamic, useFormatter, useLocale, useMessages, useNow, useTimeZone, useTranslations };
17
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../../src/react/index.ts"],"sourcesContent":["export { IntlProvider, type IntlProviderProps } from './IntlProvider';\nexport { useFormatter, useMessages, useNow, useTimeZone } from './helpers';\nexport { useLocale } from './useLocale';\nexport { useTranslations } from './useTranslations';\nexport { useDictionary } from '../useDictionary';\nexport { useDictionaryDynamic } from '../useDictionaryDynamic';\n\n/**\n * @internal Private use-intl export, kept so aliased `_useExtracted` imports\n * resolve. Not part of the supported public surface.\n */\nexport const _useExtracted = (): Record<string, never> => ({});\n"],"mappings":";;;;;;;;;;;;AAWA,MAAa,uBAA8C,EAAE"}
@@ -0,0 +1,18 @@
1
+ 'use client';
2
+
3
+ import { useLocale as useLocale$1 } from "react-intlayer";
4
+
5
+ //#region src/react/useLocale.ts
6
+ /**
7
+ * Drop-in for use-intl's `useLocale`.
8
+ *
9
+ * Returns the active locale string from Intlayer's client context.
10
+ */
11
+ const useLocale = () => {
12
+ const { locale } = useLocale$1();
13
+ return locale;
14
+ };
15
+
16
+ //#endregion
17
+ export { useLocale };
18
+ //# sourceMappingURL=useLocale.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useLocale.mjs","names":["useLocaleIntlayer"],"sources":["../../../src/react/useLocale.ts"],"sourcesContent":["'use client';\n\nimport { useLocale as useLocaleIntlayer } from 'react-intlayer';\nimport type { useLocale as _useLocale } from 'use-intl';\n\n/**\n * Drop-in for use-intl's `useLocale`.\n *\n * Returns the active locale string from Intlayer's client context.\n */\nexport const useLocale: typeof _useLocale = () => {\n const { locale } = useLocaleIntlayer();\n return locale as ReturnType<typeof _useLocale>;\n};\n"],"mappings":";;;;;;;;;;AAUA,MAAa,kBAAqC;CAChD,MAAM,EAAE,WAAWA,aAAmB;AACtC,QAAO"}
@@ -0,0 +1,37 @@
1
+ 'use client';
2
+
3
+ import { createNamespaceTranslator } from "../shared/namespaceTranslator.mjs";
4
+ import { IntlayerClientContext } from "react-intlayer";
5
+ import { useContext, useMemo } from "react";
6
+
7
+ //#region src/react/useTranslations.ts
8
+ /**
9
+ * Drop-in for use-intl's `useTranslations`.
10
+ *
11
+ * Messages support ICU MessageFormat syntax: simple arguments (`{name}`),
12
+ * plural (`{count, plural, one {…} other {…}}`, `#`), select, and formatted
13
+ * arguments (`{value, number}`). Rich text is available through `t.rich()`
14
+ * and `t.markup()`.
15
+ *
16
+ * @example
17
+ * ```tsx
18
+ * const t = useTranslations('about');
19
+ * t('counter.label'); // ✓ typed
20
+ * t('items', { count: 3 }); // ICU plural
21
+ *
22
+ * // Scoped to a nested object (use-intl idiom)
23
+ * const t = useTranslations('about.counter');
24
+ * t('label'); // resolves about → counter.label
25
+ *
26
+ * // Rich text
27
+ * t.rich('terms', { link: (chunks) => <a href="/terms">{chunks}</a> });
28
+ * ```
29
+ */
30
+ const useTranslations = ((namespace) => {
31
+ const { locale: currentLocale } = useContext(IntlayerClientContext) ?? {};
32
+ return useMemo(() => createNamespaceTranslator(currentLocale, namespace), [currentLocale, namespace]);
33
+ });
34
+
35
+ //#endregion
36
+ export { useTranslations };
37
+ //# sourceMappingURL=useTranslations.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTranslations.mjs","names":[],"sources":["../../../src/react/useTranslations.ts"],"sourcesContent":["'use client';\n\nimport type {\n DictionaryKeys,\n LocalesValues,\n} from '@intlayer/types/module_augmentation';\nimport { IntlayerClientContext } from 'react-intlayer';\nimport { useContext, useMemo } from 'react';\nimport { createNamespaceTranslator } from '../shared/namespaceTranslator';\nimport type {\n LooseTranslateFunction,\n TranslateFunction,\n} from '../shared/translateFunctionTypes';\n\n/**\n * Overload set for {@link useTranslations}:\n *\n * 1. A bare dictionary key → fully-typed `t()` (autocompleted dot-paths).\n * 2. A nested namespace `'dictionary.sub.scope'` → `t()` accepts relative\n * `string` paths, matching use-intl's scoped-namespace behaviour.\n * 3. No namespace → root scope; the first segment of each key designates\n * the dictionary (`t('about.title')`).\n */\ntype UseTranslations = {\n <N extends DictionaryKeys>(namespace: N): TranslateFunction<N>;\n (namespace: `${string}.${string}`): LooseTranslateFunction;\n (): LooseTranslateFunction;\n};\n\n/**\n * Drop-in for use-intl's `useTranslations`.\n *\n * Messages support ICU MessageFormat syntax: simple arguments (`{name}`),\n * plural (`{count, plural, one {…} other {…}}`, `#`), select, and formatted\n * arguments (`{value, number}`). Rich text is available through `t.rich()`\n * and `t.markup()`.\n *\n * @example\n * ```tsx\n * const t = useTranslations('about');\n * t('counter.label'); // ✓ typed\n * t('items', { count: 3 }); // ICU plural\n *\n * // Scoped to a nested object (use-intl idiom)\n * const t = useTranslations('about.counter');\n * t('label'); // resolves about → counter.label\n *\n * // Rich text\n * t.rich('terms', { link: (chunks) => <a href=\"/terms\">{chunks}</a> });\n * ```\n */\nexport const useTranslations = ((namespace?: string) => {\n const { locale: currentLocale } = useContext(IntlayerClientContext) ?? {};\n\n return useMemo(\n () => createNamespaceTranslator(currentLocale as LocalesValues, namespace),\n [currentLocale, namespace]\n );\n}) as UseTranslations;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmDA,MAAa,oBAAoB,cAAuB;CACtD,MAAM,EAAE,QAAQ,kBAAkB,WAAW,sBAAsB,IAAI,EAAE;AAEzE,QAAO,cACC,0BAA0B,eAAgC,UAAU,EAC1E,CAAC,eAAe,UAAU,CAC3B"}
@@ -0,0 +1,61 @@
1
+ //#region src/shared/intlFormatter.ts
2
+ /** Time units orderable by their duration in seconds. */
3
+ const RELATIVE_TIME_UNITS = [
4
+ ["year", 31536e3],
5
+ ["month", 2628e3],
6
+ ["week", 604800],
7
+ ["day", 86400],
8
+ ["hour", 3600],
9
+ ["minute", 60],
10
+ ["second", 1]
11
+ ];
12
+ const toDate = (value) => value instanceof Date ? value : new Date(value);
13
+ /**
14
+ * Builds a use-intl compatible formatter backed by the native `Intl.*` APIs
15
+ * for the given locale.
16
+ *
17
+ * Shared by `useFormatter` (React hook) and `createFormatter` (non-React core).
18
+ *
19
+ * @param locale - The locale used by every `Intl.*` formatter instance.
20
+ * @returns A formatter exposing `dateTime`, `number`, `dateTimeRange`,
21
+ * `relativeTime`, `list`, and `displayName`.
22
+ */
23
+ const buildIntlFormatter = (locale) => ({
24
+ dateTime: (value, options) => new Intl.DateTimeFormat(locale, options).format(value),
25
+ number: (value, options) => new Intl.NumberFormat(locale, options).format(value),
26
+ /** Formats a date range with `Intl.DateTimeFormat#formatRange`. */
27
+ dateTimeRange: (start, end, options) => new Intl.DateTimeFormat(locale, options).formatRange(toDate(start), toDate(end)),
28
+ /**
29
+ * Formats a date relative to now (or a reference date), automatically
30
+ * selecting the largest fitting unit.
31
+ */
32
+ relativeTime: (date, nowOrOptions) => {
33
+ const options = nowOrOptions instanceof Date || typeof nowOrOptions === "number" ? { now: nowOrOptions } : nowOrOptions ?? {};
34
+ const nowDate = options.now !== void 0 ? toDate(options.now) : /* @__PURE__ */ new Date();
35
+ const diffInSeconds = (toDate(date).getTime() - nowDate.getTime()) / 1e3;
36
+ let unit = options.unit;
37
+ if (!unit) {
38
+ const absoluteDiff = Math.abs(diffInSeconds);
39
+ unit = RELATIVE_TIME_UNITS.find(([, unitSeconds]) => absoluteDiff >= unitSeconds)?.[0] ?? "second";
40
+ }
41
+ const unitSeconds = RELATIVE_TIME_UNITS.find(([unitName]) => unitName === unit)?.[1] ?? 1;
42
+ return new Intl.RelativeTimeFormat(locale, {
43
+ numeric: "auto",
44
+ style: options.style
45
+ }).format(Math.round(diffInSeconds / unitSeconds), unit);
46
+ },
47
+ /** Formats a list with `Intl.ListFormat`. */
48
+ list: (value, options) => new Intl.ListFormat(locale, options).format(value),
49
+ displayName: (value, optionsOrFormat, extraOptions) => {
50
+ const resolvedOptions = typeof optionsOrFormat === "string" ? extraOptions : optionsOrFormat;
51
+ try {
52
+ return new Intl.DisplayNames([locale], resolvedOptions ?? { type: "language" }).of(value) ?? value;
53
+ } catch {
54
+ return value;
55
+ }
56
+ }
57
+ });
58
+
59
+ //#endregion
60
+ export { buildIntlFormatter };
61
+ //# sourceMappingURL=intlFormatter.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"intlFormatter.mjs","names":[],"sources":["../../../src/shared/intlFormatter.ts"],"sourcesContent":["import type { createFormatter as _createFormatter } from 'use-intl';\n\n/** The formatter object shape exposed by use-intl. */\nexport type Formatter = ReturnType<typeof _createFormatter>;\n\n/** Time units orderable by their duration in seconds. */\nconst RELATIVE_TIME_UNITS: [Intl.RelativeTimeFormatUnit, number][] = [\n ['year', 31536000],\n ['month', 2628000],\n ['week', 604800],\n ['day', 86400],\n ['hour', 3600],\n ['minute', 60],\n ['second', 1],\n];\n\nconst toDate = (value: Date | number | string): Date =>\n value instanceof Date ? value : new Date(value);\n\n/**\n * Builds a use-intl compatible formatter backed by the native `Intl.*` APIs\n * for the given locale.\n *\n * Shared by `useFormatter` (React hook) and `createFormatter` (non-React core).\n *\n * @param locale - The locale used by every `Intl.*` formatter instance.\n * @returns A formatter exposing `dateTime`, `number`, `dateTimeRange`,\n * `relativeTime`, `list`, and `displayName`.\n */\nexport const buildIntlFormatter = (locale: string): Formatter =>\n ({\n dateTime: (value: Date | number, options?: Intl.DateTimeFormatOptions) =>\n new Intl.DateTimeFormat(locale, options).format(value),\n\n number: (value: number | bigint, options?: Intl.NumberFormatOptions) =>\n new Intl.NumberFormat(locale, options).format(value),\n\n /** Formats a date range with `Intl.DateTimeFormat#formatRange`. */\n dateTimeRange: (\n start: Date | number,\n end: Date | number,\n options?: Intl.DateTimeFormatOptions\n ) =>\n new Intl.DateTimeFormat(locale, options).formatRange(\n toDate(start),\n toDate(end)\n ),\n\n /**\n * Formats a date relative to now (or a reference date), automatically\n * selecting the largest fitting unit.\n */\n relativeTime: (\n date: Date | number | string,\n nowOrOptions?:\n | Date\n | number\n | {\n now?: Date | number;\n unit?: Intl.RelativeTimeFormatUnit;\n style?: Intl.RelativeTimeFormatStyle;\n numberingSystem?: string;\n }\n ) => {\n const options =\n nowOrOptions instanceof Date || typeof nowOrOptions === 'number'\n ? { now: nowOrOptions }\n : (nowOrOptions ?? {});\n\n const nowDate =\n options.now !== undefined ? toDate(options.now) : new Date();\n const diffInSeconds = (toDate(date).getTime() - nowDate.getTime()) / 1000;\n\n let unit = options.unit;\n if (!unit) {\n const absoluteDiff = Math.abs(diffInSeconds);\n unit =\n RELATIVE_TIME_UNITS.find(\n ([, unitSeconds]) => absoluteDiff >= unitSeconds\n )?.[0] ?? 'second';\n }\n\n const unitSeconds =\n RELATIVE_TIME_UNITS.find(([unitName]) => unitName === unit)?.[1] ?? 1;\n\n return new Intl.RelativeTimeFormat(locale, {\n numeric: 'auto',\n style: options.style,\n }).format(Math.round(diffInSeconds / unitSeconds), unit);\n },\n\n /** Formats a list with `Intl.ListFormat`. */\n list: (value: Iterable<string>, options?: Intl.ListFormatOptions): string =>\n new Intl.ListFormat(locale, options).format(value),\n\n displayName: (\n value: string,\n optionsOrFormat?: Intl.DisplayNamesOptions | string,\n extraOptions?: Intl.DisplayNamesOptions\n ): string => {\n const resolvedOptions =\n typeof optionsOrFormat === 'string' ? extraOptions : optionsOrFormat;\n try {\n return (\n new Intl.DisplayNames(\n [locale],\n resolvedOptions ?? { type: 'language' }\n ).of(value) ?? value\n );\n } catch {\n return value;\n }\n },\n }) as unknown as Formatter;\n"],"mappings":";;AAMA,MAAM,sBAA+D;CACnE,CAAC,QAAQ,QAAS;CAClB,CAAC,SAAS,OAAQ;CAClB,CAAC,QAAQ,OAAO;CAChB,CAAC,OAAO,MAAM;CACd,CAAC,QAAQ,KAAK;CACd,CAAC,UAAU,GAAG;CACd,CAAC,UAAU,EAAE;CACd;AAED,MAAM,UAAU,UACd,iBAAiB,OAAO,QAAQ,IAAI,KAAK,MAAM;;;;;;;;;;;AAYjD,MAAa,sBAAsB,YAChC;CACC,WAAW,OAAsB,YAC/B,IAAI,KAAK,eAAe,QAAQ,QAAQ,CAAC,OAAO,MAAM;CAExD,SAAS,OAAwB,YAC/B,IAAI,KAAK,aAAa,QAAQ,QAAQ,CAAC,OAAO,MAAM;;CAGtD,gBACE,OACA,KACA,YAEA,IAAI,KAAK,eAAe,QAAQ,QAAQ,CAAC,YACvC,OAAO,MAAM,EACb,OAAO,IAAI,CACZ;;;;;CAMH,eACE,MACA,iBASG;EACH,MAAM,UACJ,wBAAwB,QAAQ,OAAO,iBAAiB,WACpD,EAAE,KAAK,cAAc,GACpB,gBAAgB,EAAE;EAEzB,MAAM,UACJ,QAAQ,QAAQ,SAAY,OAAO,QAAQ,IAAI,mBAAG,IAAI,MAAM;EAC9D,MAAM,iBAAiB,OAAO,KAAK,CAAC,SAAS,GAAG,QAAQ,SAAS,IAAI;EAErE,IAAI,OAAO,QAAQ;AACnB,MAAI,CAAC,MAAM;GACT,MAAM,eAAe,KAAK,IAAI,cAAc;AAC5C,UACE,oBAAoB,MACjB,GAAG,iBAAiB,gBAAgB,YACtC,GAAG,MAAM;;EAGd,MAAM,cACJ,oBAAoB,MAAM,CAAC,cAAc,aAAa,KAAK,GAAG,MAAM;AAEtE,SAAO,IAAI,KAAK,mBAAmB,QAAQ;GACzC,SAAS;GACT,OAAO,QAAQ;GAChB,CAAC,CAAC,OAAO,KAAK,MAAM,gBAAgB,YAAY,EAAE,KAAK;;;CAI1D,OAAO,OAAyB,YAC9B,IAAI,KAAK,WAAW,QAAQ,QAAQ,CAAC,OAAO,MAAM;CAEpD,cACE,OACA,iBACA,iBACW;EACX,MAAM,kBACJ,OAAO,oBAAoB,WAAW,eAAe;AACvD,MAAI;AACF,UACE,IAAI,KAAK,aACP,CAAC,OAAO,EACR,mBAAmB,EAAE,MAAM,YAAY,CACxC,CAAC,GAAG,MAAM,IAAI;UAEX;AACN,UAAO;;;CAGZ"}
@@ -0,0 +1,119 @@
1
+ import { getIntlayer } from "@intlayer/core/interpreter";
2
+ import { parseTaggedMessage, resolveMessage } from "@intlayer/core/messageFormat";
3
+ import { Fragment } from "react";
4
+ import { Fragment as Fragment$1, jsx } from "react/jsx-runtime";
5
+
6
+ //#region src/shared/namespaceTranslator.tsx
7
+ /**
8
+ * Reads a dotted `path` (e.g. `counter.label`) out of a nested object value.
9
+ *
10
+ * @param objectValue - The object to read from.
11
+ * @param path - Dot-separated path. An empty path returns `objectValue`.
12
+ * @returns The value found at `path`, or `undefined` when any segment is absent.
13
+ */
14
+ const navigatePath = (objectValue, path) => {
15
+ if (!path) return objectValue;
16
+ const parts = path.split(".");
17
+ let current = objectValue;
18
+ for (const part of parts) {
19
+ if (current === null || current === void 0 || typeof current !== "object") return;
20
+ current = current[part];
21
+ }
22
+ return current;
23
+ };
24
+ /** Splits rich values into scalar interpolation values and tag renderers. */
25
+ const splitRichValues = (values = {}) => {
26
+ const scalarValues = {};
27
+ const renderers = {};
28
+ for (const [valueKey, value] of Object.entries(values)) if (typeof value === "function") renderers[valueKey] = value;
29
+ else scalarValues[valueKey] = value;
30
+ return {
31
+ scalarValues,
32
+ renderers
33
+ };
34
+ };
35
+ const renderRichTokens = (tokens, renderers) => tokens.map((token, tokenIndex) => {
36
+ if (typeof token === "string") return token;
37
+ const children = renderRichTokens(token.children, renderers);
38
+ const renderer = renderers[token.tag];
39
+ if (typeof renderer === "function") return /* @__PURE__ */ jsx(Fragment, { children: renderer(children) }, tokenIndex);
40
+ return /* @__PURE__ */ jsx(Fragment, { children }, tokenIndex);
41
+ });
42
+ const renderMarkupTokens = (tokens, renderers) => tokens.map((token) => {
43
+ if (typeof token === "string") return token;
44
+ const children = renderMarkupTokens(token.children, renderers);
45
+ const renderer = renderers[token.tag];
46
+ if (typeof renderer === "function") return renderer(children);
47
+ return children;
48
+ }).join("");
49
+ /**
50
+ * The untyped runtime translator shared by `useTranslations` (React hook) and
51
+ * `createTranslator` (non-React core).
52
+ *
53
+ * Behaviour matches use-intl / next-intl:
54
+ * - namespace `'about'` → keys resolved inside the `about` dictionary
55
+ * - namespace `'about.counter'` → dictionary `about`, key prefix `counter`
56
+ * - no namespace → the first segment of each key is the dictionary key
57
+ * - messages support ICU syntax (plural, select, `#`, formatted arguments)
58
+ * - `t.rich` / `t.markup` map `<tag>chunks</tag>` through the provided
59
+ * renderers; `t.raw` returns the raw value; `t.has` checks existence
60
+ *
61
+ * @param locale - The locale dictionaries are resolved for.
62
+ * @param namespace - Optional dictionary key, optionally with a nested scope.
63
+ * @returns A translate function augmented with `has`, `raw`, `rich`, `markup`.
64
+ */
65
+ const createNamespaceTranslator = (locale, namespace) => {
66
+ const [dictionaryKey, ...prefixSegments] = (namespace ?? "").split(".");
67
+ const keyPrefix = prefixSegments.join(".");
68
+ const lookup = (key) => {
69
+ let targetDictionaryKey = dictionaryKey;
70
+ let path = keyPrefix ? `${keyPrefix}.${key}` : key;
71
+ if (!targetDictionaryKey) {
72
+ const [firstSegment, ...restSegments] = key.split(".");
73
+ targetDictionaryKey = firstSegment;
74
+ path = restSegments.join(".");
75
+ }
76
+ try {
77
+ return navigatePath(getIntlayer(targetDictionaryKey, locale), path);
78
+ } catch {
79
+ return;
80
+ }
81
+ };
82
+ const resolveToString = (key, values = {}) => {
83
+ const rawValue = lookup(key);
84
+ if (rawValue === null || rawValue === void 0) return void 0;
85
+ return resolveMessage(rawValue, values, locale, "icu");
86
+ };
87
+ const missingKeyFallback = (key) => namespace ? `${namespace}.${key}` : key;
88
+ const translate = (key, values) => resolveToString(key, values) ?? missingKeyFallback(key);
89
+ return Object.assign(translate, {
90
+ /** Returns `true` if the given key exists in the namespace. */
91
+ has: (key) => lookup(key) !== void 0,
92
+ /** Returns the raw unprocessed content for the given key. */
93
+ raw: (key) => lookup(key),
94
+ /**
95
+ * Resolves a message containing `<tag>chunks</tag>` markup, mapping each
96
+ * tag through the matching renderer in `values`.
97
+ */
98
+ rich: (key, values) => {
99
+ const { scalarValues, renderers } = splitRichValues(values);
100
+ const message = resolveToString(key, scalarValues);
101
+ if (message === void 0) return missingKeyFallback(key);
102
+ return /* @__PURE__ */ jsx(Fragment$1, { children: renderRichTokens(parseTaggedMessage(message), renderers) });
103
+ },
104
+ /**
105
+ * Resolves a message containing `<tag>chunks</tag>` markup into a string,
106
+ * mapping each tag through the matching string renderer in `values`.
107
+ */
108
+ markup: (key, values) => {
109
+ const { scalarValues, renderers } = splitRichValues(values);
110
+ const message = resolveToString(key, scalarValues);
111
+ if (message === void 0) return missingKeyFallback(key);
112
+ return renderMarkupTokens(parseTaggedMessage(message), renderers);
113
+ }
114
+ });
115
+ };
116
+
117
+ //#endregion
118
+ export { createNamespaceTranslator, navigatePath };
119
+ //# sourceMappingURL=namespaceTranslator.mjs.map