react-intlayer 1.2.1 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. package/dist/cjs/{ContentEditor/contentRender.cjs → client/ContentEditionLayout.cjs} +13 -14
  2. package/dist/cjs/client/ContentEditionLayout.cjs.map +1 -0
  3. package/dist/cjs/client/ContentEditionLayout.d.ts +6 -0
  4. package/dist/cjs/client/{IntlayerClientProvider.cjs → IntlayerProvider.cjs} +21 -10
  5. package/dist/cjs/client/IntlayerProvider.cjs.map +1 -0
  6. package/dist/{esm/client/IntlayerClientProvider.d.mts → cjs/client/IntlayerProvider.d.ts} +4 -3
  7. package/dist/cjs/client/index.cjs +6 -6
  8. package/dist/cjs/client/index.cjs.map +1 -1
  9. package/dist/cjs/client/index.d.ts +2 -1
  10. package/dist/cjs/client/useContent.d.ts +1 -2
  11. package/dist/cjs/client/useIntlayer.cjs +2 -2
  12. package/dist/cjs/client/useIntlayer.cjs.map +1 -1
  13. package/dist/cjs/client/useIntlayer.d.ts +2 -1
  14. package/dist/cjs/client/useLocale.cjs +2 -2
  15. package/dist/cjs/client/useLocale.cjs.map +1 -1
  16. package/dist/cjs/client/useLocaleBase.cjs +2 -2
  17. package/dist/cjs/client/useLocaleBase.cjs.map +1 -1
  18. package/dist/cjs/client/useTraduction.cjs +2 -2
  19. package/dist/cjs/client/useTraduction.cjs.map +1 -1
  20. package/dist/cjs/getEnumeration.cjs +0 -4
  21. package/dist/cjs/getEnumeration.cjs.map +1 -1
  22. package/dist/cjs/getTranslation.cjs +0 -4
  23. package/dist/cjs/getTranslation.cjs.map +1 -1
  24. package/dist/cjs/index.cjs +7 -4
  25. package/dist/cjs/index.cjs.map +1 -1
  26. package/dist/cjs/index.d.ts +3 -2
  27. package/dist/cjs/processDictionary/{contentDictionary.d.cjs → contentDictionary.cjs} +3 -3
  28. package/dist/cjs/processDictionary/contentDictionary.cjs.map +1 -0
  29. package/dist/cjs/processDictionary/contentDictionary.d.ts +16 -0
  30. package/dist/cjs/processDictionary/index.cjs +114 -33
  31. package/dist/cjs/processDictionary/index.cjs.map +1 -1
  32. package/dist/cjs/processDictionary/index.d.ts +5 -4
  33. package/dist/cjs/server/IntlayerServerProvider.cjs +3 -4
  34. package/dist/cjs/server/IntlayerServerProvider.cjs.map +1 -1
  35. package/dist/cjs/server/IntlayerServerProvider.d.ts +1 -1
  36. package/dist/cjs/server/index.d.ts +1 -0
  37. package/dist/cjs/server/useIntlayer.d.ts +2 -1
  38. package/dist/cjs/useIntlayerBase.cjs +48 -1
  39. package/dist/cjs/useIntlayerBase.cjs.map +1 -1
  40. package/dist/cjs/useIntlayerBase.d.ts +51 -4
  41. package/dist/cjs/vite/intlayerPlugin.cjs +1 -1
  42. package/dist/cjs/vite/intlayerPlugin.cjs.map +1 -1
  43. package/dist/esm/client/ContentEditionLayout.d.mts +6 -0
  44. package/dist/esm/client/ContentEditionLayout.mjs +15 -0
  45. package/dist/esm/client/ContentEditionLayout.mjs.map +1 -0
  46. package/dist/{cjs/client/IntlayerClientProvider.d.ts → esm/client/IntlayerProvider.d.mts} +4 -3
  47. package/dist/esm/client/{IntlayerClientProvider.mjs → IntlayerProvider.mjs} +17 -6
  48. package/dist/esm/client/IntlayerProvider.mjs.map +1 -0
  49. package/dist/esm/client/index.d.mts +2 -1
  50. package/dist/esm/client/index.mjs +4 -4
  51. package/dist/esm/client/index.mjs.map +1 -1
  52. package/dist/esm/client/useContent.d.mts +1 -2
  53. package/dist/esm/client/useIntlayer.d.mts +2 -1
  54. package/dist/esm/client/useIntlayer.mjs +1 -1
  55. package/dist/esm/client/useIntlayer.mjs.map +1 -1
  56. package/dist/esm/client/useLocale.mjs +1 -1
  57. package/dist/esm/client/useLocale.mjs.map +1 -1
  58. package/dist/esm/client/useLocaleBase.mjs +1 -1
  59. package/dist/esm/client/useLocaleBase.mjs.map +1 -1
  60. package/dist/esm/client/useTraduction.mjs +1 -1
  61. package/dist/esm/client/useTraduction.mjs.map +1 -1
  62. package/dist/esm/getEnumeration.mjs +0 -4
  63. package/dist/esm/getEnumeration.mjs.map +1 -1
  64. package/dist/esm/getTranslation.mjs +0 -4
  65. package/dist/esm/getTranslation.mjs.map +1 -1
  66. package/dist/esm/index.d.mts +3 -2
  67. package/dist/esm/index.mjs +6 -4
  68. package/dist/esm/index.mjs.map +1 -1
  69. package/dist/esm/processDictionary/contentDictionary.d.mts +16 -0
  70. package/dist/esm/processDictionary/contentDictionary.mjs +1 -0
  71. package/dist/esm/processDictionary/index.d.mts +5 -4
  72. package/dist/esm/processDictionary/index.mjs +116 -34
  73. package/dist/esm/processDictionary/index.mjs.map +1 -1
  74. package/dist/esm/server/IntlayerServerProvider.d.mts +1 -1
  75. package/dist/esm/server/IntlayerServerProvider.mjs +3 -4
  76. package/dist/esm/server/IntlayerServerProvider.mjs.map +1 -1
  77. package/dist/esm/server/index.d.mts +1 -0
  78. package/dist/esm/server/useIntlayer.d.mts +2 -1
  79. package/dist/esm/useIntlayerBase.d.mts +51 -4
  80. package/dist/esm/useIntlayerBase.mjs +46 -1
  81. package/dist/esm/useIntlayerBase.mjs.map +1 -1
  82. package/dist/esm/vite/intlayerPlugin.mjs +1 -1
  83. package/dist/esm/vite/intlayerPlugin.mjs.map +1 -1
  84. package/package.json +14 -9
  85. package/src/index.ts +4 -3
  86. package/dist/cjs/ContentEditor/ContentEditor.cjs +0 -110
  87. package/dist/cjs/ContentEditor/ContentEditor.cjs.map +0 -1
  88. package/dist/cjs/ContentEditor/ContentEditor.d.ts +0 -8
  89. package/dist/cjs/ContentEditor/contentRender.cjs.map +0 -1
  90. package/dist/cjs/ContentEditor/contentRender.d.ts +0 -3
  91. package/dist/cjs/ContentEditor/index.cjs +0 -25
  92. package/dist/cjs/ContentEditor/index.cjs.map +0 -1
  93. package/dist/cjs/ContentEditor/index.d.ts +0 -3
  94. package/dist/cjs/IntlayerProvider.cjs +0 -37
  95. package/dist/cjs/IntlayerProvider.cjs.map +0 -1
  96. package/dist/cjs/IntlayerProvider.d.ts +0 -9
  97. package/dist/cjs/client/IntlayerClientProvider.cjs.map +0 -1
  98. package/dist/cjs/processDictionary/contentDictionary.d.cjs.map +0 -1
  99. package/dist/cjs/processDictionary/contentDictionary.d.d.ts +0 -29
  100. package/dist/esm/ContentEditor/ContentEditor.d.mts +0 -8
  101. package/dist/esm/ContentEditor/ContentEditor.mjs +0 -92
  102. package/dist/esm/ContentEditor/ContentEditor.mjs.map +0 -1
  103. package/dist/esm/ContentEditor/contentRender.d.mts +0 -3
  104. package/dist/esm/ContentEditor/contentRender.mjs +0 -14
  105. package/dist/esm/ContentEditor/contentRender.mjs.map +0 -1
  106. package/dist/esm/ContentEditor/index.d.mts +0 -3
  107. package/dist/esm/ContentEditor/index.mjs +0 -3
  108. package/dist/esm/ContentEditor/index.mjs.map +0 -1
  109. package/dist/esm/IntlayerProvider.d.mts +0 -9
  110. package/dist/esm/IntlayerProvider.mjs +0 -18
  111. package/dist/esm/IntlayerProvider.mjs.map +0 -1
  112. package/dist/esm/client/IntlayerClientProvider.mjs.map +0 -1
  113. package/dist/esm/processDictionary/contentDictionary.d.d.mts +0 -29
  114. package/dist/esm/processDictionary/contentDictionary.d.mjs +0 -1
  115. package/src/ContentEditor/ContentEditor.tsx +0 -116
  116. package/src/ContentEditor/contentRender.tsx +0 -11
  117. package/src/ContentEditor/index.tsx +0 -2
  118. package/src/IntlayerProvider.tsx +0 -24
  119. package/src/cli/react-intlayer.ts +0 -60
  120. package/src/client/IntlayerClientProvider.tsx +0 -76
  121. package/src/client/getBrowserLocale.tsx +0 -192
  122. package/src/client/index.ts +0 -16
  123. package/src/client/useContent.ts +0 -20
  124. package/src/client/useIntlayer.ts +0 -25
  125. package/src/client/useLocale.ts +0 -23
  126. package/src/client/useLocaleBase.ts +0 -24
  127. package/src/client/useLocaleCookie.ts +0 -32
  128. package/src/client/useTraduction.ts +0 -33
  129. package/src/craco/craco.config.ts +0 -11
  130. package/src/craco/intlayerCracoPlugin.ts +0 -89
  131. package/src/getEnumeration.ts +0 -46
  132. package/src/getTranslation.ts +0 -42
  133. package/src/processDictionary/contentDictionary.d.ts +0 -27
  134. package/src/processDictionary/index.ts +0 -144
  135. package/src/server/IntlayerServerProvider.tsx +0 -36
  136. package/src/server/getLocaleTranslation.ts +0 -22
  137. package/src/server/index.ts +0 -8
  138. package/src/server/serverContext.ts +0 -85
  139. package/src/server/useIntlayer.ts +0 -23
  140. package/src/server/useTraduction.ts +0 -34
  141. package/src/useIntlayerBase.ts +0 -35
  142. package/src/vite/intlayerPlugin.ts +0 -59
  143. /package/dist/esm/processDictionary/{contentDictionary.d.mjs.map → contentDictionary.mjs.map} +0 -0
@@ -27,41 +27,76 @@ var import_core = require("@intlayer/core");
27
27
  var import_react = require("react");
28
28
  var import_getEnumeration = require('../getEnumeration.cjs');
29
29
  var import_getTranslation = require('../getTranslation.cjs');
30
- const { defaultLocale } = (0, import_client.getConfiguration)().internationalization;
31
- const processTranslation = (languageContent, locale) => {
30
+ const {
31
+ internationalization: { defaultLocale }
32
+ } = (0, import_client.getConfiguration)();
33
+ const processTranslation = (languageContent, locale, dictionaryId, dictionaryPath, keyPath = []) => {
32
34
  const translationResult = (0, import_getTranslation.getTranslation)(
33
35
  languageContent,
34
36
  locale
35
37
  );
36
- return processDictionary(translationResult, locale);
38
+ const resultKeyPath = [
39
+ ...keyPath,
40
+ { type: import_core.NodeType.Translation, key: locale }
41
+ ];
42
+ return processDictionary(
43
+ translationResult,
44
+ dictionaryId,
45
+ dictionaryPath,
46
+ resultKeyPath,
47
+ locale
48
+ );
37
49
  };
38
- const processEnumeration = (enumerationContent, locale) => {
39
- return (quantity) => {
40
- const enumerationResult = (0, import_getEnumeration.getEnumeration)(
41
- enumerationContent,
42
- quantity
43
- );
44
- return processDictionary(enumerationResult, locale);
45
- };
50
+ const processEnumeration = (enumerationContent, locale, dictionaryId, dictionaryPath, keyPath = []) => (quantity) => {
51
+ const enumerationResult = (0, import_getEnumeration.getEnumeration)(
52
+ enumerationContent,
53
+ quantity
54
+ );
55
+ const matchingCondition = (0, import_core.findMatchingCondition)(
56
+ enumerationContent,
57
+ quantity
58
+ );
59
+ const resultKeyPath = [
60
+ ...keyPath,
61
+ { type: import_core.NodeType.Enumeration, key: matchingCondition.toString() }
62
+ ];
63
+ return processDictionary(
64
+ enumerationResult,
65
+ dictionaryId,
66
+ dictionaryPath,
67
+ resultKeyPath,
68
+ locale
69
+ );
46
70
  };
47
71
  const isReactNode = (node) => typeof node?.key !== "undefined" && typeof node?.props !== "undefined";
48
- const processNode = (field, locale) => {
49
- if (typeof field !== "object" || field === null) {
50
- return field;
51
- }
52
- if (field.nodeType === import_core.NodeType.Translation) {
53
- return processTranslation(field, locale);
54
- }
55
- if (field.nodeType === import_core.NodeType.Enumeration) {
56
- return processEnumeration(
57
- field,
58
- locale
59
- );
60
- }
61
- if (typeof field.type === "undefined") {
62
- return processDictionary(field, locale);
72
+ const processNode = (field, locale, dictionaryId, dictionaryPath, keyPath = []) => {
73
+ if (typeof field === "object") {
74
+ if (field.nodeType === import_core.NodeType.Translation) {
75
+ return processTranslation(
76
+ field,
77
+ locale,
78
+ dictionaryId,
79
+ dictionaryPath,
80
+ keyPath
81
+ );
82
+ }
83
+ if (field.nodeType === import_core.NodeType.Enumeration) {
84
+ return processEnumeration(
85
+ field,
86
+ locale,
87
+ dictionaryId,
88
+ dictionaryPath,
89
+ keyPath
90
+ );
91
+ }
63
92
  }
64
- return field;
93
+ return processDictionary(
94
+ field,
95
+ dictionaryId,
96
+ dictionaryPath,
97
+ keyPath,
98
+ locale
99
+ );
65
100
  };
66
101
  const createReactElement = (element) => {
67
102
  if (typeof element === "string") {
@@ -88,21 +123,67 @@ const createReactElement = (element) => {
88
123
  const { type, props } = fixedElement;
89
124
  return (0, import_react.createElement)(type ?? "div", props, ...props.children);
90
125
  };
91
- const processDictionary = (content, locale = defaultLocale) => {
126
+ const traceKeys = ["filePath", "nodeType"];
127
+ const processDictionary = (content, dictionaryId, dictionaryPath, keyPath = [], locale = defaultLocale) => {
92
128
  if (isReactNode(content)) {
93
129
  return createReactElement(
94
130
  content
95
131
  );
96
132
  }
97
133
  if (content && typeof content === "object") {
98
- const result = {};
99
- for (const key of Object.keys(content)) {
100
- const field = content[key];
101
- result[key] = processNode(field, locale);
134
+ const isArray = Array.isArray(content);
135
+ let result = {};
136
+ if (typeof content.nodeType !== "undefined") {
137
+ return processNode(
138
+ content,
139
+ locale,
140
+ dictionaryId,
141
+ dictionaryPath,
142
+ keyPath
143
+ );
144
+ } else if (isArray) {
145
+ result = content.map((field, key) => {
146
+ const resultKeyPath = [
147
+ ...keyPath,
148
+ { type: "ArrayExpression", key }
149
+ ];
150
+ return processNode(
151
+ field,
152
+ locale,
153
+ dictionaryId,
154
+ dictionaryPath,
155
+ resultKeyPath
156
+ );
157
+ });
158
+ } else {
159
+ for (const key of Object.keys(content)) {
160
+ const field = content[key];
161
+ if (traceKeys.includes(key)) {
162
+ result[key] = field;
163
+ continue;
164
+ }
165
+ const resultKeyPath = [
166
+ ...keyPath,
167
+ { type: "ObjectExpression", key }
168
+ ];
169
+ const nodeResult = processNode(
170
+ field,
171
+ locale,
172
+ dictionaryId,
173
+ dictionaryPath,
174
+ resultKeyPath
175
+ );
176
+ result[key] = nodeResult;
177
+ }
102
178
  }
103
179
  return result;
104
180
  }
105
- return content;
181
+ return {
182
+ content,
183
+ keyPath,
184
+ dictionaryId,
185
+ dictionaryPath
186
+ };
106
187
  };
107
188
  // Annotate the CommonJS export names for ESM import in node:
108
189
  0 && (module.exports = {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/processDictionary/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { type Locales, getConfiguration } from '@intlayer/config/client';\nimport {\n NodeType,\n type QuantityContent,\n type LanguageContent,\n} from '@intlayer/core';\nimport { type ReactElement, createElement, type ReactNode } from 'react';\nimport { getEnumeration } from '../getEnumeration';\nimport { getTranslation } from '../getTranslation';\nimport type {\n Content,\n ContentValue,\n TransformedContent,\n TransformedContentValue,\n} from './contentDictionary';\n\nconst { defaultLocale } = getConfiguration().internationalization;\n\nconst processTranslation = (\n languageContent: LanguageContent<ContentValue>,\n locale: Locales\n): TransformedContent => {\n const translationResult: ContentValue = getTranslation<ContentValue>(\n languageContent,\n locale\n );\n\n return processDictionary(translationResult as Content, locale);\n};\n\nconst processEnumeration = (\n enumerationContent: QuantityContent<ContentValue>,\n locale: Locales\n): TransformedContentValue => {\n return (quantity: number): TransformedContentValue => {\n const enumerationResult: ContentValue = getEnumeration<ContentValue>(\n enumerationContent,\n quantity\n );\n\n //\n return processDictionary(enumerationResult as Content, locale);\n };\n};\n\nconst isReactNode = (node: Record<string, unknown>): boolean =>\n typeof node?.key !== 'undefined' && typeof node?.props !== 'undefined';\n\nexport const processNode = (\n field: ContentValue | undefined,\n locale: Locales\n): TransformedContentValue => {\n if (typeof field !== 'object' || field === null) {\n return field;\n }\n\n if (field.nodeType === NodeType.Translation) {\n return processTranslation(field as LanguageContent<ContentValue>, locale);\n }\n\n if (field.nodeType === NodeType.Enumeration) {\n return processEnumeration(\n field satisfies QuantityContent<ContentValue>,\n locale\n );\n }\n\n if (typeof (field as any).type === 'undefined') {\n return processDictionary(field as Content, locale);\n }\n\n return field as TransformedContentValue;\n};\n\n// This function recursively creates React elements from a given JSON-like structure\nconst createReactElement = (element: ReactElement) => {\n if (typeof element === 'string') {\n // If it's a string, simply return it (used for text content)\n return element;\n }\n\n // Destructure the component properties\n\n const convertChildrenAsArray = (element: ReactElement): ReactElement => {\n if (element?.props && typeof element.props.children === 'object') {\n const childrenResult: ReactNode[] = [];\n const { children } = element.props;\n\n // Create the children elements recursively, if any\n Object.keys(children).forEach((key) => {\n childrenResult.push(createReactElement(children[key]));\n });\n\n return {\n ...element,\n props: { ...element.props, children: childrenResult },\n };\n }\n\n return {\n ...element,\n props: { ...element.props, children: element.props.children },\n };\n };\n\n const fixedElement = convertChildrenAsArray(element);\n\n const { type, props } = fixedElement;\n\n // Create and return the React element\n return createElement(type ?? 'div', props, ...props.children);\n};\n\n/**\n * Function that process a dictionary and return the result to be used in the application.\n */\nexport const processDictionary = (\n content: Content,\n locale: Locales = defaultLocale\n): TransformedContent => {\n // If it's a React element, render it\n if (isReactNode(content)) {\n return createReactElement(\n content as unknown as ReactElement\n ) as unknown as TransformedContent;\n }\n\n if (content && typeof content === 'object') {\n const result: TransformedContent = {};\n\n // List each key in the content and process it\n for (const key of Object.keys(content)) {\n const field = content[key];\n\n result[key] = processNode(field, locale);\n }\n\n return result;\n }\n\n // If it's a string, number, or function, return it\n return content;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,oBAA+C;AAC/C,kBAIO;AACP,mBAAiE;AACjE,4BAA+B;AAC/B,4BAA+B;AAQ/B,MAAM,EAAE,cAAc,QAAI,gCAAiB,EAAE;AAE7C,MAAM,qBAAqB,CACzB,iBACA,WACuB;AACvB,QAAM,wBAAkC;AAAA,IACtC;AAAA,IACA;AAAA,EACF;AAEA,SAAO,kBAAkB,mBAA8B,MAAM;AAC/D;AAEA,MAAM,qBAAqB,CACzB,oBACA,WAC4B;AAC5B,SAAO,CAAC,aAA8C;AACpD,UAAM,wBAAkC;AAAA,MACtC;AAAA,MACA;AAAA,IACF;AAGA,WAAO,kBAAkB,mBAA8B,MAAM;AAAA,EAC/D;AACF;AAEA,MAAM,cAAc,CAAC,SACnB,OAAO,MAAM,QAAQ,eAAe,OAAO,MAAM,UAAU;AAEtD,MAAM,cAAc,CACzB,OACA,WAC4B;AAC5B,MAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAC/C,WAAO;AAAA,EACT;AAEA,MAAI,MAAM,aAAa,qBAAS,aAAa;AAC3C,WAAO,mBAAmB,OAAwC,MAAM;AAAA,EAC1E;AAEA,MAAI,MAAM,aAAa,qBAAS,aAAa;AAC3C,WAAO;AAAA,MACL;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,MAAI,OAAQ,MAAc,SAAS,aAAa;AAC9C,WAAO,kBAAkB,OAAkB,MAAM;AAAA,EACnD;AAEA,SAAO;AACT;AAGA,MAAM,qBAAqB,CAAC,YAA0B;AACpD,MAAI,OAAO,YAAY,UAAU;AAE/B,WAAO;AAAA,EACT;AAIA,QAAM,yBAAyB,CAACA,aAAwC;AACtE,QAAIA,UAAS,SAAS,OAAOA,SAAQ,MAAM,aAAa,UAAU;AAChE,YAAM,iBAA8B,CAAC;AACrC,YAAM,EAAE,SAAS,IAAIA,SAAQ;AAG7B,aAAO,KAAK,QAAQ,EAAE,QAAQ,CAAC,QAAQ;AACrC,uBAAe,KAAK,mBAAmB,SAAS,GAAG,CAAC,CAAC;AAAA,MACvD,CAAC;AAED,aAAO;AAAA,QACL,GAAGA;AAAA,QACH,OAAO,EAAE,GAAGA,SAAQ,OAAO,UAAU,eAAe;AAAA,MACtD;AAAA,IACF;AAEA,WAAO;AAAA,MACL,GAAGA;AAAA,MACH,OAAO,EAAE,GAAGA,SAAQ,OAAO,UAAUA,SAAQ,MAAM,SAAS;AAAA,IAC9D;AAAA,EACF;AAEA,QAAM,eAAe,uBAAuB,OAAO;AAEnD,QAAM,EAAE,MAAM,MAAM,IAAI;AAGxB,aAAO,4BAAc,QAAQ,OAAO,OAAO,GAAG,MAAM,QAAQ;AAC9D;AAKO,MAAM,oBAAoB,CAC/B,SACA,SAAkB,kBACK;AAEvB,MAAI,YAAY,OAAO,GAAG;AACxB,WAAO;AAAA,MACL;AAAA,IACF;AAAA,EACF;AAEA,MAAI,WAAW,OAAO,YAAY,UAAU;AAC1C,UAAM,SAA6B,CAAC;AAGpC,eAAW,OAAO,OAAO,KAAK,OAAO,GAAG;AACtC,YAAM,QAAQ,QAAQ,GAAG;AAEzB,aAAO,GAAG,IAAI,YAAY,OAAO,MAAM;AAAA,IACzC;AAEA,WAAO;AAAA,EACT;AAGA,SAAO;AACT;","names":["element"]}
1
+ {"version":3,"sources":["../../../src/processDictionary/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { type Locales, getConfiguration } from '@intlayer/config/client';\nimport {\n NodeType,\n type QuantityContent,\n type LanguageContent,\n findMatchingCondition,\n type KeyPath,\n type Dictionary,\n type DictionaryValue,\n type TranslationContent,\n type EnumerationContent,\n type TypedNode,\n} from '@intlayer/core';\nimport { type ReactElement, createElement, type ReactNode } from 'react';\nimport { getEnumeration } from '../getEnumeration';\nimport { getTranslation } from '../getTranslation';\nimport type {\n TransformedContent,\n TransformedContentValue,\n} from './contentDictionary';\n\nconst {\n internationalization: { defaultLocale },\n} = getConfiguration();\n\nconst processTranslation = (\n languageContent: LanguageContent<DictionaryValue>,\n locale: Locales,\n dictionaryId: string,\n dictionaryPath: string,\n keyPath: KeyPath[] = []\n): TransformedContent => {\n const translationResult: DictionaryValue = getTranslation<DictionaryValue>(\n languageContent,\n locale\n );\n\n const resultKeyPath: KeyPath[] = [\n ...keyPath,\n { type: NodeType.Translation, key: locale },\n ];\n\n return processDictionary(\n translationResult,\n dictionaryId,\n dictionaryPath,\n resultKeyPath,\n locale\n );\n};\n\nconst processEnumeration =\n (\n enumerationContent: QuantityContent<DictionaryValue>,\n locale: Locales,\n dictionaryId: string,\n dictionaryPath: string,\n keyPath: KeyPath[] = []\n ): TransformedContentValue =>\n (quantity: number): TransformedContentValue => {\n const enumerationResult: DictionaryValue = getEnumeration<DictionaryValue>(\n enumerationContent,\n quantity\n );\n\n const matchingCondition = findMatchingCondition(\n enumerationContent,\n quantity\n );\n\n const resultKeyPath: KeyPath[] = [\n ...keyPath,\n { type: NodeType.Enumeration, key: matchingCondition.toString() },\n ];\n\n return processDictionary(\n enumerationResult,\n dictionaryId,\n dictionaryPath,\n resultKeyPath,\n locale\n ) as TransformedContentValue;\n };\n\nconst isReactNode = (node: Record<string, unknown>): boolean =>\n typeof node?.key !== 'undefined' && typeof node?.props !== 'undefined';\n\nexport const processNode = (\n field: DictionaryValue | undefined,\n locale: Locales,\n dictionaryId: string,\n dictionaryPath: string,\n keyPath: KeyPath[] = []\n): TransformedContentValue => {\n if (typeof field === 'object') {\n if (\n (field as TranslationContent<DictionaryValue>).nodeType ===\n NodeType.Translation\n ) {\n return processTranslation(\n field as LanguageContent<DictionaryValue>,\n locale,\n dictionaryId,\n dictionaryPath,\n keyPath\n ) as TransformedContentValue;\n }\n\n if (\n (field as EnumerationContent<DictionaryValue>).nodeType ===\n NodeType.Enumeration\n ) {\n return processEnumeration(\n field as QuantityContent<DictionaryValue>,\n locale,\n dictionaryId,\n dictionaryPath,\n keyPath\n );\n }\n }\n\n return processDictionary(\n field!,\n dictionaryId,\n dictionaryPath,\n keyPath,\n locale\n ) as TransformedContentValue;\n};\n\n// This function recursively creates React elements from a given JSON-like structure\nconst createReactElement = (element: ReactElement) => {\n if (typeof element === 'string') {\n // If it's a string, simply return it (used for text content)\n return element;\n }\n\n // Destructure the component properties\n\n const convertChildrenAsArray = (element: ReactElement): ReactElement => {\n if (element?.props && typeof element.props.children === 'object') {\n const childrenResult: ReactNode[] = [];\n const { children } = element.props;\n\n // Create the children elements recursively, if any\n Object.keys(children).forEach((key) => {\n childrenResult.push(createReactElement(children[key]));\n });\n\n return {\n ...element,\n props: { ...element.props, children: childrenResult },\n };\n }\n\n return {\n ...element,\n props: { ...element.props, children: element.props.children },\n };\n };\n\n const fixedElement = convertChildrenAsArray(element);\n\n const { type, props } = fixedElement;\n\n // Create and return the React element\n return createElement(type ?? 'div', props, ...props.children);\n};\n\nconst traceKeys: string[] = ['filePath', 'nodeType'];\n\n/**\n * Function that process a dictionary and return the result to be used in the application.\n */\nexport const processDictionary = (\n content: Dictionary | DictionaryValue,\n dictionaryId: string,\n dictionaryPath: string,\n keyPath: KeyPath[] = [],\n locale: Locales = defaultLocale\n // eslint-disable-next-line sonarjs/cognitive-complexity\n): TransformedContent => {\n // If it's a React element, render it\n if (isReactNode(content as Record<string, unknown>)) {\n return createReactElement(\n content as unknown as ReactElement\n ) as unknown as TransformedContent;\n }\n\n if (content && typeof content === 'object') {\n const isArray = Array.isArray(content);\n\n let result: TransformedContent = {};\n\n if (typeof (content as TypedNode).nodeType !== 'undefined') {\n return processNode(\n content as DictionaryValue,\n locale,\n dictionaryId,\n dictionaryPath,\n keyPath\n ) as TransformedContent;\n } else if (isArray) {\n // Eslint fix because promises are awaited during build stage\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n result = (content as DictionaryValue[]).map((field, key) => {\n const resultKeyPath: KeyPath[] = [\n ...keyPath,\n { type: 'ArrayExpression', key },\n ];\n\n return processNode(\n field,\n locale,\n dictionaryId,\n dictionaryPath,\n resultKeyPath\n );\n }) as TransformedContent;\n } else {\n // List each key in the content and process it\n for (const key of Object.keys(content)) {\n const field = content[key as keyof typeof content];\n\n if (traceKeys.includes(key)) {\n result[key] = field as TransformedContentValue;\n continue;\n }\n\n const resultKeyPath: KeyPath[] = [\n ...keyPath,\n { type: 'ObjectExpression', key },\n ];\n\n const nodeResult = processNode(\n field,\n locale,\n dictionaryId,\n dictionaryPath,\n resultKeyPath\n );\n\n result[key] = nodeResult;\n }\n }\n\n return result;\n }\n\n return {\n content,\n keyPath,\n dictionaryId,\n dictionaryPath,\n };\n};\n"],"mappings":"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","names":["element"]}
@@ -1,11 +1,12 @@
1
1
  import { Locales } from '@intlayer/config/client';
2
- import { ContentValue, TransformedContentValue, Content, TransformedContent } from './contentDictionary.d.js';
3
- import '@intlayer/core';
2
+ import { DictionaryValue, KeyPath, Dictionary } from '@intlayer/core';
3
+ import { TransformedContentValue, TransformedContent } from './contentDictionary.js';
4
+ import 'react';
4
5
 
5
- declare const processNode: (field: ContentValue | undefined, locale: Locales) => TransformedContentValue;
6
+ declare const processNode: (field: DictionaryValue | undefined, locale: Locales, dictionaryId: string, dictionaryPath: string, keyPath?: KeyPath[]) => TransformedContentValue;
6
7
  /**
7
8
  * Function that process a dictionary and return the result to be used in the application.
8
9
  */
9
- declare const processDictionary: (content: Content, locale?: Locales) => TransformedContent;
10
+ declare const processDictionary: (content: Dictionary | DictionaryValue, dictionaryId: string, dictionaryPath: string, keyPath?: KeyPath[], locale?: Locales) => TransformedContent;
10
11
 
11
12
  export { processDictionary, processNode };
@@ -27,14 +27,13 @@ module.exports = __toCommonJS(IntlayerServerProvider_exports);
27
27
  var import_jsx_runtime = require("react/jsx-runtime");
28
28
  var import_client = require("@intlayer/config/client");
29
29
  var import_serverContext = require('./serverContext.cjs');
30
- const IntlayerServerContext = (0, import_serverContext.createServerContext)(
31
- (0, import_client.getConfiguration)().internationalization.defaultLocale
32
- );
30
+ const { defaultLocale } = (0, import_client.getConfiguration)().internationalization;
31
+ const IntlayerServerContext = (0, import_serverContext.createServerContext)(defaultLocale);
33
32
  const useIntlayer = () => (0, import_serverContext.getServerContext)(IntlayerServerContext);
34
33
  const locale = (0, import_serverContext.getServerContext)(IntlayerServerContext);
35
34
  const IntlayerServerProvider = ({
36
35
  children,
37
- locale: locale2
36
+ locale: locale2 = defaultLocale
38
37
  }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(IntlayerServerContext.Provider, { value: locale2, children });
39
38
  // Annotate the CommonJS export names for ESM import in node:
40
39
  0 && (module.exports = {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/server/IntlayerServerProvider.tsx"],"sourcesContent":["import { type Locales, getConfiguration } from '@intlayer/config/client';\nimport type { FC, PropsWithChildren } from 'react';\nimport { createServerContext, getServerContext } from './serverContext';\n\n/**\n * Context that store the current locale on the server side\n */\nexport const IntlayerServerContext = createServerContext<Locales>(\n getConfiguration().internationalization.defaultLocale\n);\n\n/**\n * Hook that provides the current locale\n */\nexport const useIntlayer = () => getServerContext(IntlayerServerContext);\n\n/**\n * Get the current locale\n */\nexport const locale = getServerContext(IntlayerServerContext);\n\nexport type IntlayerServerProviderProps = PropsWithChildren & {\n locale: Locales;\n};\n\n/**\n * Provider that store the current locale on the server side\n */\nexport const IntlayerServerProvider: FC<IntlayerServerProviderProps> = ({\n children,\n locale,\n}) => (\n <IntlayerServerContext.Provider value={locale}>\n {children}\n </IntlayerServerContext.Provider>\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAgCE;AAhCF,oBAA+C;AAE/C,2BAAsD;AAK/C,MAAM,4BAAwB;AAAA,MACnC,gCAAiB,EAAE,qBAAqB;AAC1C;AAKO,MAAM,cAAc,UAAM,uCAAiB,qBAAqB;AAKhE,MAAM,aAAS,uCAAiB,qBAAqB;AASrD,MAAM,yBAA0D,CAAC;AAAA,EACtE;AAAA,EACA,QAAAA;AACF,MACE,4CAAC,sBAAsB,UAAtB,EAA+B,OAAOA,SACpC,UACH;","names":["locale"]}
1
+ {"version":3,"sources":["../../../src/server/IntlayerServerProvider.tsx"],"sourcesContent":["import { type Locales, getConfiguration } from '@intlayer/config/client';\nimport type { FC, PropsWithChildren } from 'react';\nimport { createServerContext, getServerContext } from './serverContext';\n\nconst { defaultLocale } = getConfiguration().internationalization;\n\n/**\n * Context that store the current locale on the server side\n */\nexport const IntlayerServerContext =\n createServerContext<Locales>(defaultLocale);\n\n/**\n * Hook that provides the current locale\n */\nexport const useIntlayer = () => getServerContext(IntlayerServerContext);\n\n/**\n * Get the current locale\n */\nexport const locale = getServerContext(IntlayerServerContext);\n\nexport type IntlayerServerProviderProps = PropsWithChildren & {\n locale?: Locales;\n};\n\n/**\n * Provider that store the current locale on the server side\n */\nexport const IntlayerServerProvider: FC<IntlayerServerProviderProps> = ({\n children,\n locale = defaultLocale,\n}) => (\n <IntlayerServerContext.Provider value={locale}>\n {children}\n </IntlayerServerContext.Provider>\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAiCE;AAjCF,oBAA+C;AAE/C,2BAAsD;AAEtD,MAAM,EAAE,cAAc,QAAI,gCAAiB,EAAE;AAKtC,MAAM,4BACX,0CAA6B,aAAa;AAKrC,MAAM,cAAc,UAAM,uCAAiB,qBAAqB;AAKhE,MAAM,aAAS,uCAAiB,qBAAqB;AASrD,MAAM,yBAA0D,CAAC;AAAA,EACtE;AAAA,EACA,QAAAA,UAAS;AACX,MACE,4CAAC,sBAAsB,UAAtB,EAA+B,OAAOA,SACpC,UACH;","names":["locale"]}
@@ -27,7 +27,7 @@ declare const useIntlayer: () => Locales | undefined;
27
27
  */
28
28
  declare const locale: Locales | undefined;
29
29
  type IntlayerServerProviderProps = PropsWithChildren & {
30
- locale: Locales;
30
+ locale?: Locales;
31
31
  };
32
32
  /**
33
33
  * Provider that store the current locale on the server side
@@ -6,4 +6,5 @@ import '@intlayer/config/client';
6
6
  import '@intlayer/core';
7
7
  import 'react';
8
8
  import '../useIntlayerBase.js';
9
+ import '@intlayer/config';
9
10
  import 'intlayer';
@@ -1,6 +1,7 @@
1
1
  import { UseIntlayer } from '../useIntlayerBase.js';
2
- import '@intlayer/config/client';
2
+ import '@intlayer/config';
3
3
  import 'intlayer';
4
+ import 'react';
4
5
 
5
6
  /**
6
7
  * On the server side, Hook that picking one dictionary by its id and return the content
@@ -28,20 +28,67 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
  var useIntlayerBase_exports = {};
30
30
  __export(useIntlayerBase_exports, {
31
+ recursiveStringifyContent: () => recursiveStringifyContent,
32
+ recursiveTransformContent: () => recursiveTransformContent,
31
33
  useIntlayerBase: () => useIntlayerBase
32
34
  });
33
35
  module.exports = __toCommonJS(useIntlayerBase_exports);
34
36
  var import_dictionaries_entry = __toESM(require("@intlayer/dictionaries-entry"));
37
+ var import_client = require("intlayer-editor/client");
35
38
  var import_processDictionary = require('./processDictionary/index.cjs');
39
+ const recursiveTransformContent = (obj) => Object.entries(obj).reduce((acc, [key, value]) => {
40
+ if (typeof value === "object" && typeof value.keyPath !== "undefined" && typeof value.dictionaryId !== "undefined" && typeof value.dictionaryPath !== "undefined") {
41
+ return {
42
+ ...acc,
43
+ [key]: (0, import_client.renderIntlayerEditor)(value)
44
+ };
45
+ } else if (typeof value === "object" && Array.isArray(value)) {
46
+ return {
47
+ ...acc,
48
+ [key]: value.map(recursiveTransformContent)
49
+ };
50
+ } else if (typeof value === "object") {
51
+ return {
52
+ ...acc,
53
+ [key]: recursiveTransformContent(value)
54
+ };
55
+ }
56
+ return acc;
57
+ }, {});
58
+ const recursiveStringifyContent = (obj) => Object.entries(obj).reduce((acc, [key, value]) => {
59
+ if (typeof value === "object" && typeof value.value !== "undefined") {
60
+ return {
61
+ ...acc,
62
+ [key]: value.value
63
+ };
64
+ } else if (typeof value === "object" && Array.isArray(value)) {
65
+ return {
66
+ ...acc,
67
+ [key]: value.map(recursiveStringifyContent)
68
+ };
69
+ } else if (typeof value === "object") {
70
+ return {
71
+ ...acc,
72
+ [key]: recursiveStringifyContent(value)
73
+ };
74
+ }
75
+ return acc;
76
+ }, {});
36
77
  const useIntlayerBase = (id, locale) => {
37
78
  const dictionary = import_dictionaries_entry.default[id];
38
- return (0, import_processDictionary.processDictionary)(
79
+ const result = (0, import_processDictionary.processDictionary)(
39
80
  dictionary,
81
+ id,
82
+ dictionary.filePath,
83
+ [],
40
84
  locale
41
85
  );
86
+ return recursiveTransformContent(result);
42
87
  };
43
88
  // Annotate the CommonJS export names for ESM import in node:
44
89
  0 && (module.exports = {
90
+ recursiveStringifyContent,
91
+ recursiveTransformContent,
45
92
  useIntlayerBase
46
93
  });
47
94
  //# sourceMappingURL=useIntlayerBase.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/useIntlayerBase.ts"],"sourcesContent":["import type { Locales } from '@intlayer/config/client';\n/**\n * @intlayer/dictionaries-entry is a package that only returns the dictionary entry path.\n * Using an external package allow to alias it in the bundle configuration (such as webpack).\n * The alias allow hot reload the app (such as nextjs) on any dictionary change.\n */\nimport dictionaries from '@intlayer/dictionaries-entry';\nimport type { IntlayerDictionaryTypesConnector } from 'intlayer';\nimport type { Content } from './processDictionary/contentDictionary';\nimport { processDictionary } from './processDictionary/index';\n\nexport type StringFallback<T> = T extends never ? string : T; // If no keys are found, return string to disable error, and accept any string as dictionary key\nexport type DictionaryKeys = StringFallback<\n keyof IntlayerDictionaryTypesConnector\n>;\n\nexport type UseIntlayer = <T extends DictionaryKeys>(\n id: T,\n locale?: Locales\n) => IntlayerDictionaryTypesConnector[T];\n\n/**\n * Hook that picking one dictionary by its id and return the content.\n */\nexport const useIntlayerBase: UseIntlayer = <T extends DictionaryKeys>(\n id: T,\n locale?: Locales\n) => {\n const dictionary: Content = dictionaries[id];\n\n return processDictionary(\n dictionary,\n locale\n ) as IntlayerDictionaryTypesConnector[T];\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAMA,gCAAyB;AAGzB,+BAAkC;AAe3B,MAAM,kBAA+B,CAC1C,IACA,WACG;AACH,QAAM,aAAsB,0BAAAA,QAAa,EAAE;AAE3C,aAAO;AAAA,IACL;AAAA,IACA;AAAA,EACF;AACF;","names":["dictionaries"]}
1
+ {"version":3,"sources":["../../src/useIntlayerBase.ts"],"sourcesContent":["import type { Locales } from '@intlayer/config';\n/**\n * @intlayer/dictionaries-entry is a package that only returns the dictionary entry path.\n * Using an external package allow to alias it in the bundle configuration (such as webpack).\n * The alias allow hot reload the app (such as nextjs) on any dictionary change.\n */\nimport type { Dictionary } from '@intlayer/core';\nimport dictionaries from '@intlayer/dictionaries-entry';\nimport type { IntlayerDictionaryTypesConnector } from 'intlayer';\nimport { renderIntlayerEditor } from 'intlayer-editor/client';\nimport type { ReactNode } from 'react';\nimport { processDictionary } from './processDictionary/index';\n\n/**\n * Provides a fallback to string type if the generic type T is never,\n * otherwise returns T. This is useful for handling cases where no keys are found.\n * Example: StringFallback<never> -> string; StringFallback<'key'> -> 'key'\n */\nexport type StringFallback<T> = T extends never ? string : T; // If no keys are found, return string to disable error, and accept any string as dictionary key\n\n/**\n * Represents the keys of the IntlayerDictionaryTypesConnector,\n * ensuring they are valid dictionary keys or fallback to string if none exist.\n *\n * Example:\n * ```ts\n * DictionaryKeys -> 'key1' | 'key2'\n * // or if IntlayerDictionaryTypesConnector is not defined,\n * DictionaryKeys -> string\n * ```\n */\nexport type DictionaryKeys = StringFallback<\n keyof IntlayerDictionaryTypesConnector\n>;\n\nexport type IntlayerNode = ReactNode & {\n value: string;\n};\n\ntype DeepTransformContent<T> = T extends object\n ? {\n [K in keyof T]: DeepTransformContent<T[K]>;\n }\n : IntlayerNode;\n\n/**\n * Excludes the 'id' and 'filePath' keys from the dictionary content,\n * as they are not part of the IntlayerDictionaryTypesConnector type.\n */\ntype ExcludeIntlayerUtilsKeys<T> = Omit<T, 'id' | 'filePath'>;\n\n/**\n * Represents the data type returned by the useIntlayer hook,\n * excluding the 'id' and 'filePath' keys from the dictionary content.\n */\ntype Data<T extends DictionaryKeys> = ExcludeIntlayerUtilsKeys<\n DeepTransformContent<IntlayerDictionaryTypesConnector[T]>\n>;\n\n/**\n * Parcourt the object. If a object has a keyPath, render the intlayer editor if editor enabled.\n */\nexport const recursiveTransformContent = (obj: object): object =>\n Object.entries(obj).reduce((acc, [key, value]) => {\n if (\n typeof value === 'object' &&\n typeof value.keyPath !== 'undefined' &&\n typeof value.dictionaryId !== 'undefined' &&\n typeof value.dictionaryPath !== 'undefined'\n ) {\n return {\n ...acc,\n [key]: renderIntlayerEditor(value),\n };\n } else if (typeof value === 'object' && Array.isArray(value)) {\n return {\n ...acc,\n [key]: value.map(recursiveTransformContent),\n };\n } else if (typeof value === 'object') {\n return {\n ...acc,\n [key]: recursiveTransformContent(value),\n };\n }\n\n return acc;\n }, {});\n\ntype DeepStrinfifyContent<T> =\n T extends React.ComponentType<unknown>\n ? string\n : T extends object\n ? {\n [K in keyof T]: DeepStrinfifyContent<T[K]>;\n }\n : T;\n\nexport const recursiveStringifyContent = <T extends object>(\n obj: T\n): DeepStrinfifyContent<T> =>\n Object.entries(obj).reduce((acc, [key, value]) => {\n if (typeof value === 'object' && typeof value.value !== 'undefined') {\n return {\n ...acc,\n [key]: value.value,\n };\n } else if (typeof value === 'object' && Array.isArray(value)) {\n return {\n ...acc,\n [key]: value.map(recursiveStringifyContent),\n };\n } else if (typeof value === 'object') {\n return {\n ...acc,\n [key]: recursiveStringifyContent(value),\n };\n }\n\n return acc;\n }, {}) as DeepStrinfifyContent<T>;\n\n/**\n * Type definition for the useIntlayer hook, which takes a dictionary ID and an optional locale,\n * and returns the deeply transformed dictionary content.\n *\n */\nexport type UseIntlayer = <T extends DictionaryKeys>(\n id: T,\n locale?: Locales\n) => Data<T>;\n\n/**\n * Hook that picks one dictionary by its ID and returns the content,\n * deeply transformed according to the dictionary structure and metadata.\n */\nexport const useIntlayerBase: UseIntlayer = <T extends DictionaryKeys>(\n id: T,\n locale?: Locales\n) => {\n const dictionary: Dictionary = dictionaries[id];\n\n const result = processDictionary(\n dictionary,\n id,\n dictionary.filePath,\n [],\n locale\n ) as object;\n\n return recursiveTransformContent(result) as Data<T>;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAOA,gCAAyB;AAEzB,oBAAqC;AAErC,+BAAkC;AAmD3B,MAAM,4BAA4B,CAAC,QACxC,OAAO,QAAQ,GAAG,EAAE,OAAO,CAAC,KAAK,CAAC,KAAK,KAAK,MAAM;AAChD,MACE,OAAO,UAAU,YACjB,OAAO,MAAM,YAAY,eACzB,OAAO,MAAM,iBAAiB,eAC9B,OAAO,MAAM,mBAAmB,aAChC;AACA,WAAO;AAAA,MACL,GAAG;AAAA,MACH,CAAC,GAAG,OAAG,oCAAqB,KAAK;AAAA,IACnC;AAAA,EACF,WAAW,OAAO,UAAU,YAAY,MAAM,QAAQ,KAAK,GAAG;AAC5D,WAAO;AAAA,MACL,GAAG;AAAA,MACH,CAAC,GAAG,GAAG,MAAM,IAAI,yBAAyB;AAAA,IAC5C;AAAA,EACF,WAAW,OAAO,UAAU,UAAU;AACpC,WAAO;AAAA,MACL,GAAG;AAAA,MACH,CAAC,GAAG,GAAG,0BAA0B,KAAK;AAAA,IACxC;AAAA,EACF;AAEA,SAAO;AACT,GAAG,CAAC,CAAC;AAWA,MAAM,4BAA4B,CACvC,QAEA,OAAO,QAAQ,GAAG,EAAE,OAAO,CAAC,KAAK,CAAC,KAAK,KAAK,MAAM;AAChD,MAAI,OAAO,UAAU,YAAY,OAAO,MAAM,UAAU,aAAa;AACnE,WAAO;AAAA,MACL,GAAG;AAAA,MACH,CAAC,GAAG,GAAG,MAAM;AAAA,IACf;AAAA,EACF,WAAW,OAAO,UAAU,YAAY,MAAM,QAAQ,KAAK,GAAG;AAC5D,WAAO;AAAA,MACL,GAAG;AAAA,MACH,CAAC,GAAG,GAAG,MAAM,IAAI,yBAAyB;AAAA,IAC5C;AAAA,EACF,WAAW,OAAO,UAAU,UAAU;AACpC,WAAO;AAAA,MACL,GAAG;AAAA,MACH,CAAC,GAAG,GAAG,0BAA0B,KAAK;AAAA,IACxC;AAAA,EACF;AAEA,SAAO;AACT,GAAG,CAAC,CAAC;AAgBA,MAAM,kBAA+B,CAC1C,IACA,WACG;AACH,QAAM,aAAyB,0BAAAA,QAAa,EAAE;AAE9C,QAAM,aAAS;AAAA,IACb;AAAA,IACA;AAAA,IACA,WAAW;AAAA,IACX,CAAC;AAAA,IACD;AAAA,EACF;AAEA,SAAO,0BAA0B,MAAM;AACzC;","names":["dictionaries"]}
@@ -1,12 +1,59 @@
1
- import { Locales } from '@intlayer/config/client';
1
+ import { Locales } from '@intlayer/config';
2
2
  import { IntlayerDictionaryTypesConnector } from 'intlayer';
3
+ import { ReactNode } from 'react';
3
4
 
5
+ /**
6
+ * Provides a fallback to string type if the generic type T is never,
7
+ * otherwise returns T. This is useful for handling cases where no keys are found.
8
+ * Example: StringFallback<never> -> string; StringFallback<'key'> -> 'key'
9
+ */
4
10
  type StringFallback<T> = T extends never ? string : T;
11
+ /**
12
+ * Represents the keys of the IntlayerDictionaryTypesConnector,
13
+ * ensuring they are valid dictionary keys or fallback to string if none exist.
14
+ *
15
+ * Example:
16
+ * ```ts
17
+ * DictionaryKeys -> 'key1' | 'key2'
18
+ * // or if IntlayerDictionaryTypesConnector is not defined,
19
+ * DictionaryKeys -> string
20
+ * ```
21
+ */
5
22
  type DictionaryKeys = StringFallback<keyof IntlayerDictionaryTypesConnector>;
6
- type UseIntlayer = <T extends DictionaryKeys>(id: T, locale?: Locales) => IntlayerDictionaryTypesConnector[T];
23
+ type IntlayerNode = ReactNode & {
24
+ value: string;
25
+ };
26
+ type DeepTransformContent<T> = T extends object ? {
27
+ [K in keyof T]: DeepTransformContent<T[K]>;
28
+ } : IntlayerNode;
29
+ /**
30
+ * Excludes the 'id' and 'filePath' keys from the dictionary content,
31
+ * as they are not part of the IntlayerDictionaryTypesConnector type.
32
+ */
33
+ type ExcludeIntlayerUtilsKeys<T> = Omit<T, 'id' | 'filePath'>;
34
+ /**
35
+ * Represents the data type returned by the useIntlayer hook,
36
+ * excluding the 'id' and 'filePath' keys from the dictionary content.
37
+ */
38
+ type Data<T extends DictionaryKeys> = ExcludeIntlayerUtilsKeys<DeepTransformContent<IntlayerDictionaryTypesConnector[T]>>;
39
+ /**
40
+ * Parcourt the object. If a object has a keyPath, render the intlayer editor if editor enabled.
41
+ */
42
+ declare const recursiveTransformContent: (obj: object) => object;
43
+ type DeepStrinfifyContent<T> = T extends React.ComponentType<unknown> ? string : T extends object ? {
44
+ [K in keyof T]: DeepStrinfifyContent<T[K]>;
45
+ } : T;
46
+ declare const recursiveStringifyContent: <T extends object>(obj: T) => DeepStrinfifyContent<T>;
47
+ /**
48
+ * Type definition for the useIntlayer hook, which takes a dictionary ID and an optional locale,
49
+ * and returns the deeply transformed dictionary content.
50
+ *
51
+ */
52
+ type UseIntlayer = <T extends DictionaryKeys>(id: T, locale?: Locales) => Data<T>;
7
53
  /**
8
- * Hook that picking one dictionary by its id and return the content.
54
+ * Hook that picks one dictionary by its ID and returns the content,
55
+ * deeply transformed according to the dictionary structure and metadata.
9
56
  */
10
57
  declare const useIntlayerBase: UseIntlayer;
11
58
 
12
- export { type DictionaryKeys, type StringFallback, type UseIntlayer, useIntlayerBase };
59
+ export { type DictionaryKeys, type IntlayerNode, type StringFallback, type UseIntlayer, recursiveStringifyContent, recursiveTransformContent, useIntlayerBase };
@@ -50,7 +50,7 @@ const intLayerPlugin = (_pluginOptions = {}) => ({
50
50
  persistent: import_meta.env === "development"
51
51
  });
52
52
  },
53
- configureServer: (server) => {
53
+ configureServer: () => {
54
54
  }
55
55
  });
56
56
  // Annotate the CommonJS export names for ESM import in node:
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/vite/intlayerPlugin.ts"],"sourcesContent":["import { join, relative, resolve } from 'path';\nimport { watch } from '@intlayer/chokidar';\nimport { getConfiguration, formatEnvVariable } from '@intlayer/config';\nimport { loadEnv, type Plugin } from 'vite';\n\n// Plugin options type definition\ntype PluginOptions = {\n // Custom options for your plugin, if any\n};\n\n/**\n *\n * A Vite plugin that integrates IntLayer configuration into the build process\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intLayerPlugin() ],\n * });\n * ```\n * */\nexport const intLayerPlugin = (_pluginOptions: PluginOptions = {}): Plugin => ({\n name: 'vite-intlayer-plugin',\n\n config: (config, { mode }) => {\n const intlayerConfig = getConfiguration();\n\n // Set all configuration values as environment variables\n const env = formatEnvVariable('vite');\n\n process.env = { ...process.env, ...loadEnv(mode, process.cwd()), ...env };\n\n const { mainDir, baseDir } = intlayerConfig.content;\n const dictionariesPath = join(mainDir, 'dictionaries.mjs');\n const relativeDictionariesPath = relative(baseDir, dictionariesPath);\n\n // Update Vite's resolve alias\n config.resolve = {\n ...config.resolve,\n alias: {\n ...config.resolve?.alias,\n '@intlayer/dictionaries-entry': resolve(relativeDictionariesPath),\n },\n };\n\n return config;\n },\n\n buildStart: () => {\n // Code to run when Vite build starts\n watch({\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n persistent: (import.meta as any).env === 'development',\n });\n },\n configureServer: (server) => {\n // Custom server configuration, if needed\n },\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAwC;AACxC,sBAAsB;AACtB,oBAAoD;AACpD,kBAAqC;AAHrC;AAqBO,MAAM,iBAAiB,CAAC,iBAAgC,CAAC,OAAe;AAAA,EAC7E,MAAM;AAAA,EAEN,QAAQ,CAAC,QAAQ,EAAE,KAAK,MAAM;AAC5B,UAAM,qBAAiB,gCAAiB;AAGxC,UAAM,UAAM,iCAAkB,MAAM;AAEpC,YAAQ,MAAM,EAAE,GAAG,QAAQ,KAAK,OAAG,qBAAQ,MAAM,QAAQ,IAAI,CAAC,GAAG,GAAG,IAAI;AAExE,UAAM,EAAE,SAAS,QAAQ,IAAI,eAAe;AAC5C,UAAM,uBAAmB,kBAAK,SAAS,kBAAkB;AACzD,UAAM,+BAA2B,sBAAS,SAAS,gBAAgB;AAGnE,WAAO,UAAU;AAAA,MACf,GAAG,OAAO;AAAA,MACV,OAAO;AAAA,QACL,GAAG,OAAO,SAAS;AAAA,QACnB,oCAAgC,qBAAQ,wBAAwB;AAAA,MAClE;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEA,YAAY,MAAM;AAEhB,+BAAM;AAAA;AAAA,MAEJ,YAAa,YAAoB,QAAQ;AAAA,IAC3C,CAAC;AAAA,EACH;AAAA,EACA,iBAAiB,CAAC,WAAW;AAAA,EAE7B;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../src/vite/intlayerPlugin.ts"],"sourcesContent":["import { join, relative, resolve } from 'path';\nimport { watch } from '@intlayer/chokidar';\nimport { getConfiguration, formatEnvVariable } from '@intlayer/config';\nimport { loadEnv, type Plugin } from 'vite';\n\n// Plugin options type definition\ntype PluginOptions = {\n // Custom options for your plugin, if any\n};\n\n/**\n *\n * A Vite plugin that integrates IntLayer configuration into the build process\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intLayerPlugin() ],\n * });\n * ```\n * */\nexport const intLayerPlugin = (_pluginOptions: PluginOptions = {}): Plugin => ({\n name: 'vite-intlayer-plugin',\n\n config: (config, { mode }) => {\n const intlayerConfig = getConfiguration();\n\n // Set all configuration values as environment variables\n const env = formatEnvVariable('vite');\n\n process.env = { ...process.env, ...loadEnv(mode, process.cwd()), ...env };\n\n const { mainDir, baseDir } = intlayerConfig.content;\n const dictionariesPath = join(mainDir, 'dictionaries.mjs');\n const relativeDictionariesPath = relative(baseDir, dictionariesPath);\n\n // Update Vite's resolve alias\n config.resolve = {\n ...config.resolve,\n alias: {\n ...config.resolve?.alias,\n '@intlayer/dictionaries-entry': resolve(relativeDictionariesPath),\n },\n };\n\n return config;\n },\n\n buildStart: () => {\n // Code to run when Vite build starts\n watch({\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n persistent: (import.meta as any).env === 'development',\n });\n },\n configureServer: () => {\n // Custom server configuration, if needed\n },\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAwC;AACxC,sBAAsB;AACtB,oBAAoD;AACpD,kBAAqC;AAHrC;AAqBO,MAAM,iBAAiB,CAAC,iBAAgC,CAAC,OAAe;AAAA,EAC7E,MAAM;AAAA,EAEN,QAAQ,CAAC,QAAQ,EAAE,KAAK,MAAM;AAC5B,UAAM,qBAAiB,gCAAiB;AAGxC,UAAM,UAAM,iCAAkB,MAAM;AAEpC,YAAQ,MAAM,EAAE,GAAG,QAAQ,KAAK,OAAG,qBAAQ,MAAM,QAAQ,IAAI,CAAC,GAAG,GAAG,IAAI;AAExE,UAAM,EAAE,SAAS,QAAQ,IAAI,eAAe;AAC5C,UAAM,uBAAmB,kBAAK,SAAS,kBAAkB;AACzD,UAAM,+BAA2B,sBAAS,SAAS,gBAAgB;AAGnE,WAAO,UAAU;AAAA,MACf,GAAG,OAAO;AAAA,MACV,OAAO;AAAA,QACL,GAAG,OAAO,SAAS;AAAA,QACnB,oCAAgC,qBAAQ,wBAAwB;AAAA,MAClE;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEA,YAAY,MAAM;AAEhB,+BAAM;AAAA;AAAA,MAEJ,YAAa,YAAoB,QAAQ;AAAA,IAC3C,CAAC;AAAA,EACH;AAAA,EACA,iBAAiB,MAAM;AAAA,EAEvB;AACF;","names":[]}
@@ -0,0 +1,6 @@
1
+ import { ContentEditionLayoutProps } from 'intlayer-editor/client';
2
+ import { FC } from 'react';
3
+
4
+ declare const ContentEditionLayout: FC<ContentEditionLayoutProps>;
5
+
6
+ export { ContentEditionLayout };
@@ -0,0 +1,15 @@
1
+ import "../chunk-ZNCO4QRF.mjs";
2
+ import { Fragment, jsx } from "react/jsx-runtime";
3
+ import { getConfiguration } from "@intlayer/config/client";
4
+ import {
5
+ ContentEditionLayout as ContentEditionLayoutBase
6
+ } from "intlayer-editor/client";
7
+ const {
8
+ editor: { enabled }
9
+ } = getConfiguration();
10
+ const BlankLayout = ({ children }) => /* @__PURE__ */ jsx(Fragment, { children });
11
+ const ContentEditionLayout = typeof ContentEditionLayoutBase === "undefined" || !enabled ? BlankLayout : ContentEditionLayoutBase;
12
+ export {
13
+ ContentEditionLayout
14
+ };
15
+ //# sourceMappingURL=ContentEditionLayout.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/client/ContentEditionLayout.tsx"],"sourcesContent":["import { getConfiguration } from '@intlayer/config/client';\nimport {\n ContentEditionLayout as ContentEditionLayoutBase,\n type ContentEditionLayoutProps,\n} from 'intlayer-editor/client';\nimport type { FC } from 'react';\n\nconst {\n editor: { enabled },\n} = getConfiguration();\n\nconst BlankLayout: FC<ContentEditionLayoutProps> = ({ children }) => (\n <>{children}</>\n);\n\n// intlayer-editor is an optional dependency. If it's not installed, return the blank layout\nexport const ContentEditionLayout: FC<ContentEditionLayoutProps> =\n typeof ContentEditionLayoutBase === 'undefined' || !enabled\n ? BlankLayout\n : ContentEditionLayoutBase;\n"],"mappings":";AAYE;AAZF,SAAS,wBAAwB;AACjC;AAAA,EACE,wBAAwB;AAAA,OAEnB;AAGP,MAAM;AAAA,EACJ,QAAQ,EAAE,QAAQ;AACpB,IAAI,iBAAiB;AAErB,MAAM,cAA6C,CAAC,EAAE,SAAS,MAC7D,gCAAG,UAAS;AAIP,MAAM,uBACX,OAAO,6BAA6B,eAAe,CAAC,UAChD,cACA;","names":[]}
@@ -14,12 +14,13 @@ declare const IntlayerClientContext: react.Context<IntlayerValue>;
14
14
  * Hook that provides the current locale
15
15
  */
16
16
  declare const useIntlayerContext: () => IntlayerValue;
17
- type IntlayerClientProviderProps = PropsWithChildren & {
17
+ type IntlayerProviderProps = PropsWithChildren & {
18
18
  locale?: Locales;
19
+ setLocale?: (locale: Locales) => void;
19
20
  };
20
21
  /**
21
22
  * Provider that store the current locale on the client side
22
23
  */
23
- declare const IntlayerClientProvider: FC<IntlayerClientProviderProps>;
24
+ declare const IntlayerProvider: FC<IntlayerProviderProps>;
24
25
 
25
- export { IntlayerClientContext, IntlayerClientProvider, type IntlayerClientProviderProps, useIntlayerContext };
26
+ export { IntlayerClientContext, IntlayerProvider, type IntlayerProviderProps, useIntlayerContext };
@@ -9,21 +9,23 @@ import {
9
9
  useState,
10
10
  useCallback
11
11
  } from "react";
12
+ import { ContentEditionLayout } from './ContentEditionLayout.mjs';
12
13
  import { localeCookie, setLocaleCookie } from './useLocaleCookie.mjs';
13
14
  const IntlayerClientContext = createContext({
14
15
  locale: localeCookie ?? getConfiguration().internationalization.defaultLocale,
15
16
  setLocale: () => null
16
17
  });
17
18
  const useIntlayerContext = () => useContext(IntlayerClientContext);
18
- const IntlayerClientProvider = ({
19
+ const IntlayerProvider = ({
19
20
  locale,
20
- children
21
+ children,
22
+ setLocale: setLocaleProp
21
23
  }) => {
22
24
  const { defaultLocale, locales: availableLocales } = getConfiguration().internationalization;
23
25
  const [currentLocale, setCurrentLocale] = useState(
24
26
  locale ?? localeCookie ?? defaultLocale
25
27
  );
26
- const setLocale = useCallback(
28
+ const setLocaleBase = useCallback(
27
29
  (newLocale) => {
28
30
  if (currentLocale.toString() === newLocale.toString())
29
31
  return;
@@ -36,15 +38,24 @@ const IntlayerClientProvider = ({
36
38
  },
37
39
  [availableLocales, currentLocale, locale]
38
40
  );
41
+ const setLocale = setLocaleProp ?? setLocaleBase;
39
42
  const value = useMemo(
40
43
  () => ({ locale: currentLocale, setLocale }),
41
44
  [currentLocale, setLocale]
42
45
  );
43
- return /* @__PURE__ */ jsx(IntlayerClientContext.Provider, { value, children });
46
+ return /* @__PURE__ */ jsx(IntlayerClientContext.Provider, { value, children: /* @__PURE__ */ jsx(
47
+ ContentEditionLayout,
48
+ {
49
+ locale: currentLocale,
50
+ setLocale,
51
+ localeList: availableLocales,
52
+ children
53
+ }
54
+ ) });
44
55
  };
45
56
  export {
46
57
  IntlayerClientContext,
47
- IntlayerClientProvider,
58
+ IntlayerProvider,
48
59
  useIntlayerContext
49
60
  };
50
- //# sourceMappingURL=IntlayerClientProvider.mjs.map
61
+ //# sourceMappingURL=IntlayerProvider.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/client/IntlayerProvider.tsx"],"sourcesContent":["'use client';\n\nimport { getConfiguration, type Locales } from '@intlayer/config/client';\nimport {\n type PropsWithChildren,\n createContext,\n useContext,\n useMemo,\n type FC,\n useState,\n useCallback,\n} from 'react';\nimport { ContentEditionLayout } from './ContentEditionLayout';\nimport { localeCookie, setLocaleCookie } from './useLocaleCookie';\n\ntype IntlayerValue = {\n locale: Locales;\n setLocale: (newLocale: Locales) => void;\n};\n\n/**\n * Context that store the current locale on the client side\n */\nexport const IntlayerClientContext = createContext<IntlayerValue>({\n locale: localeCookie ?? getConfiguration().internationalization.defaultLocale,\n setLocale: () => null,\n});\n\n/**\n * Hook that provides the current locale\n */\nexport const useIntlayerContext = () => useContext(IntlayerClientContext);\n\nexport type IntlayerProviderProps = PropsWithChildren & {\n locale?: Locales;\n setLocale?: (locale: Locales) => void;\n};\n\n/**\n * Provider that store the current locale on the client side\n */\nexport const IntlayerProvider: FC<IntlayerProviderProps> = ({\n locale,\n children,\n setLocale: setLocaleProp,\n}) => {\n const { defaultLocale, locales: availableLocales } =\n getConfiguration().internationalization;\n\n const [currentLocale, setCurrentLocale] = useState(\n locale ?? localeCookie ?? defaultLocale\n );\n\n const setLocaleBase = useCallback(\n (newLocale: Locales) => {\n if (currentLocale.toString() === newLocale.toString()) return;\n\n if (!availableLocales.includes(newLocale)) {\n console.error(`Locale ${locale} is not available`);\n return;\n }\n\n setCurrentLocale(newLocale); // Update state\n setLocaleCookie(newLocale); // Optionally set cookie for persistence\n },\n [availableLocales, currentLocale, locale]\n );\n\n const setLocale = setLocaleProp ?? setLocaleBase;\n\n const value: IntlayerValue = useMemo<IntlayerValue>(\n () => ({ locale: currentLocale, setLocale: setLocale }),\n [currentLocale, setLocale]\n );\n\n return (\n <IntlayerClientContext.Provider value={value}>\n <ContentEditionLayout\n locale={currentLocale}\n setLocale={setLocale}\n localeList={availableLocales}\n >\n {children}\n </ContentEditionLayout>\n </IntlayerClientContext.Provider>\n );\n};\n"],"mappings":";;AA6EM;AA3EN,SAAS,wBAAsC;AAC/C;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,EACA;AAAA,OACK;AACP,SAAS,4BAA4B;AACrC,SAAS,cAAc,uBAAuB;AAUvC,MAAM,wBAAwB,cAA6B;AAAA,EAChE,QAAQ,gBAAgB,iBAAiB,EAAE,qBAAqB;AAAA,EAChE,WAAW,MAAM;AACnB,CAAC;AAKM,MAAM,qBAAqB,MAAM,WAAW,qBAAqB;AAUjE,MAAM,mBAA8C,CAAC;AAAA,EAC1D;AAAA,EACA;AAAA,EACA,WAAW;AACb,MAAM;AACJ,QAAM,EAAE,eAAe,SAAS,iBAAiB,IAC/C,iBAAiB,EAAE;AAErB,QAAM,CAAC,eAAe,gBAAgB,IAAI;AAAA,IACxC,UAAU,gBAAgB;AAAA,EAC5B;AAEA,QAAM,gBAAgB;AAAA,IACpB,CAAC,cAAuB;AACtB,UAAI,cAAc,SAAS,MAAM,UAAU,SAAS;AAAG;AAEvD,UAAI,CAAC,iBAAiB,SAAS,SAAS,GAAG;AACzC,gBAAQ,MAAM,UAAU,MAAM,mBAAmB;AACjD;AAAA,MACF;AAEA,uBAAiB,SAAS;AAC1B,sBAAgB,SAAS;AAAA,IAC3B;AAAA,IACA,CAAC,kBAAkB,eAAe,MAAM;AAAA,EAC1C;AAEA,QAAM,YAAY,iBAAiB;AAEnC,QAAM,QAAuB;AAAA,IAC3B,OAAO,EAAE,QAAQ,eAAe,UAAqB;AAAA,IACrD,CAAC,eAAe,SAAS;AAAA,EAC3B;AAEA,SACE,oBAAC,sBAAsB,UAAtB,EAA+B,OAC9B;AAAA,IAAC;AAAA;AAAA,MACC,QAAQ;AAAA,MACR;AAAA,MACA,YAAY;AAAA,MAEX;AAAA;AAAA,EACH,GACF;AAEJ;","names":[]}
@@ -1,4 +1,4 @@
1
- export { IntlayerClientContext as IntlayerClient, IntlayerClientProvider, IntlayerClientProviderProps, useIntlayerContext } from './IntlayerClientProvider.mjs';
1
+ export { IntlayerClientContext, IntlayerProvider, IntlayerProviderProps, useIntlayerContext } from './IntlayerProvider.mjs';
2
2
  export { useIntlayer } from './useIntlayer.mjs';
3
3
  export { useLocaleBase } from './useLocaleBase.mjs';
4
4
  export { useLocale } from './useLocale.mjs';
@@ -8,5 +8,6 @@ export { getBrowserLocale } from './getBrowserLocale.mjs';
8
8
  import 'react';
9
9
  import '@intlayer/config/client';
10
10
  import '../useIntlayerBase.mjs';
11
+ import '@intlayer/config';
11
12
  import 'intlayer';
12
13
  import '@intlayer/core';
@@ -2,8 +2,8 @@ import "../chunk-ZNCO4QRF.mjs";
2
2
  import {
3
3
  IntlayerClientContext,
4
4
  useIntlayerContext,
5
- IntlayerClientProvider
6
- } from './IntlayerClientProvider.mjs';
5
+ IntlayerProvider
6
+ } from './IntlayerProvider.mjs';
7
7
  import { useIntlayer } from './useIntlayer.mjs';
8
8
  import { useLocaleBase } from './useLocaleBase.mjs';
9
9
  import { useLocale } from './useLocale.mjs';
@@ -15,8 +15,8 @@ import {
15
15
  } from './useLocaleCookie.mjs';
16
16
  import { getBrowserLocale } from './getBrowserLocale.mjs';
17
17
  export {
18
- IntlayerClientContext as IntlayerClient,
19
- IntlayerClientProvider,
18
+ IntlayerClientContext,
19
+ IntlayerProvider,
20
20
  getBrowserLocale,
21
21
  localeCookie,
22
22
  setLocaleCookie,