react-intlayer 1.0.1 → 1.2.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 (195) hide show
  1. package/dist/cjs/{LocaleContextProvider.cjs → IntlayerProvider.cjs} +11 -11
  2. package/dist/cjs/IntlayerProvider.cjs.map +1 -0
  3. package/dist/cjs/IntlayerProvider.d.ts +9 -0
  4. package/dist/cjs/cli/react-intlayer.cjs +68 -0
  5. package/dist/cjs/cli/react-intlayer.cjs.map +1 -0
  6. package/dist/cjs/cli/react-intlayer.d.ts +1 -0
  7. package/dist/cjs/client/IntlayerClientProvider.cjs +69 -0
  8. package/dist/cjs/client/IntlayerClientProvider.cjs.map +1 -0
  9. package/dist/cjs/client/IntlayerClientProvider.d.ts +25 -0
  10. package/dist/cjs/client/getBrowserLocale.cjs +170 -0
  11. package/dist/cjs/client/getBrowserLocale.cjs.map +1 -0
  12. package/dist/cjs/client/getBrowserLocale.d.ts +26 -0
  13. package/dist/cjs/client/index.cjs +13 -7
  14. package/dist/cjs/client/index.cjs.map +1 -1
  15. package/dist/cjs/client/index.d.ts +3 -1
  16. package/dist/cjs/client/useContent.cjs +2 -2
  17. package/dist/cjs/client/useContent.cjs.map +1 -1
  18. package/dist/cjs/client/useContent.d.ts +4 -1
  19. package/dist/cjs/client/useIntlayer.cjs +2 -2
  20. package/dist/cjs/client/useIntlayer.cjs.map +1 -1
  21. package/dist/cjs/client/useIntlayer.d.ts +5 -0
  22. package/dist/cjs/client/useLocale.cjs +6 -4
  23. package/dist/cjs/client/useLocale.cjs.map +1 -1
  24. package/dist/cjs/client/useLocale.d.ts +4 -0
  25. package/dist/cjs/client/{LocaleClientContextProvider.cjs → useLocaleBase.cjs} +24 -17
  26. package/dist/cjs/client/useLocaleBase.cjs.map +1 -0
  27. package/dist/cjs/client/useLocaleBase.d.ts +14 -0
  28. package/dist/cjs/client/useLocaleCookie.cjs +1 -1
  29. package/dist/cjs/client/useLocaleCookie.cjs.map +1 -1
  30. package/dist/cjs/client/useLocaleCookie.d.ts +9 -0
  31. package/dist/cjs/client/useTraduction.cjs +2 -2
  32. package/dist/cjs/client/useTraduction.cjs.map +1 -1
  33. package/dist/cjs/client/useTraduction.d.ts +22 -1
  34. package/dist/cjs/craco/craco.config.cjs +32 -0
  35. package/dist/cjs/craco/craco.config.cjs.map +1 -0
  36. package/dist/cjs/craco/craco.config.d.ts +2 -0
  37. package/dist/cjs/craco/intlayerCracoPlugin.cjs +89 -0
  38. package/dist/cjs/craco/intlayerCracoPlugin.cjs.map +1 -0
  39. package/dist/cjs/craco/intlayerCracoPlugin.d.ts +26 -0
  40. package/dist/cjs/getEnumeration.cjs.map +1 -1
  41. package/dist/cjs/getEnumeration.d.ts +28 -0
  42. package/dist/cjs/getTranslation.cjs +1 -1
  43. package/dist/cjs/getTranslation.cjs.map +1 -1
  44. package/dist/cjs/getTranslation.d.ts +22 -1
  45. package/dist/cjs/index.cjs +8 -6
  46. package/dist/cjs/index.cjs.map +1 -1
  47. package/dist/cjs/index.d.ts +3 -1
  48. package/dist/cjs/processDictionary/index.cjs +33 -1
  49. package/dist/cjs/processDictionary/index.cjs.map +1 -1
  50. package/dist/cjs/processDictionary/index.d.ts +1 -1
  51. package/dist/cjs/server/{LocaleServerContextProvider.cjs → IntlayerServerProvider.cjs} +20 -14
  52. package/dist/cjs/server/IntlayerServerProvider.cjs.map +1 -0
  53. package/dist/{esm/server/LocaleServerContextProvider.d.mts → cjs/server/IntlayerServerProvider.d.ts} +17 -4
  54. package/dist/cjs/server/getLocaleTranslation.cjs +3 -3
  55. package/dist/cjs/server/getLocaleTranslation.cjs.map +1 -1
  56. package/dist/cjs/server/getLocaleTranslation.d.ts +3 -0
  57. package/dist/cjs/server/index.cjs +6 -6
  58. package/dist/cjs/server/index.cjs.map +1 -1
  59. package/dist/cjs/server/index.d.ts +1 -1
  60. package/dist/cjs/server/serverContext.cjs.map +1 -1
  61. package/dist/cjs/server/serverContext.d.ts +4 -4
  62. package/dist/cjs/server/useIntlayer.cjs +2 -2
  63. package/dist/cjs/server/useIntlayer.cjs.map +1 -1
  64. package/dist/cjs/server/useIntlayer.d.ts +5 -0
  65. package/dist/cjs/server/useTraduction.cjs +2 -2
  66. package/dist/cjs/server/useTraduction.cjs.map +1 -1
  67. package/dist/cjs/server/useTraduction.d.ts +22 -1
  68. package/dist/cjs/useIntlayerBase.cjs.map +1 -1
  69. package/dist/cjs/useIntlayerBase.d.ts +3 -6
  70. package/dist/cjs/vite/intlayerPlugin.cjs +60 -0
  71. package/dist/cjs/vite/intlayerPlugin.cjs.map +1 -0
  72. package/dist/cjs/vite/intlayerPlugin.d.ts +17 -0
  73. package/dist/esm/ContentEditor/ContentEditor.mjs +1 -0
  74. package/dist/esm/ContentEditor/ContentEditor.mjs.map +1 -1
  75. package/dist/esm/ContentEditor/contentRender.mjs +1 -0
  76. package/dist/esm/ContentEditor/contentRender.mjs.map +1 -1
  77. package/dist/esm/IntlayerProvider.d.mts +9 -0
  78. package/dist/esm/IntlayerProvider.mjs +18 -0
  79. package/dist/esm/IntlayerProvider.mjs.map +1 -0
  80. package/dist/esm/chunk-ZNCO4QRF.mjs +9 -0
  81. package/dist/esm/chunk-ZNCO4QRF.mjs.map +1 -0
  82. package/dist/esm/cli/react-intlayer.d.mts +1 -0
  83. package/dist/esm/cli/react-intlayer.mjs +45 -0
  84. package/dist/esm/cli/react-intlayer.mjs.map +1 -0
  85. package/dist/esm/client/IntlayerClientProvider.d.mts +25 -0
  86. package/dist/esm/client/IntlayerClientProvider.mjs +50 -0
  87. package/dist/esm/client/IntlayerClientProvider.mjs.map +1 -0
  88. package/dist/esm/client/getBrowserLocale.d.mts +26 -0
  89. package/dist/esm/client/getBrowserLocale.mjs +146 -0
  90. package/dist/esm/client/getBrowserLocale.mjs.map +1 -0
  91. package/dist/esm/client/index.d.mts +3 -1
  92. package/dist/esm/client/index.mjs +12 -7
  93. package/dist/esm/client/index.mjs.map +1 -1
  94. package/dist/esm/client/useContent.d.mts +4 -1
  95. package/dist/esm/client/useContent.mjs +3 -2
  96. package/dist/esm/client/useContent.mjs.map +1 -1
  97. package/dist/esm/client/useIntlayer.d.mts +5 -0
  98. package/dist/esm/client/useIntlayer.mjs +3 -2
  99. package/dist/esm/client/useIntlayer.mjs.map +1 -1
  100. package/dist/esm/client/useLocale.d.mts +4 -0
  101. package/dist/esm/client/useLocale.mjs +8 -5
  102. package/dist/esm/client/useLocale.mjs.map +1 -1
  103. package/dist/esm/client/useLocaleBase.d.mts +14 -0
  104. package/dist/esm/client/useLocaleBase.mjs +26 -0
  105. package/dist/esm/client/useLocaleBase.mjs.map +1 -0
  106. package/dist/esm/client/useLocaleCookie.d.mts +9 -0
  107. package/dist/esm/client/useLocaleCookie.mjs +3 -4
  108. package/dist/esm/client/useLocaleCookie.mjs.map +1 -1
  109. package/dist/esm/client/useTraduction.d.mts +22 -1
  110. package/dist/esm/client/useTraduction.mjs +3 -2
  111. package/dist/esm/client/useTraduction.mjs.map +1 -1
  112. package/dist/esm/craco/craco.config.d.mts +2 -0
  113. package/dist/esm/craco/craco.config.mjs +17 -0
  114. package/dist/esm/craco/craco.config.mjs.map +1 -0
  115. package/dist/esm/craco/intlayerCracoPlugin.d.mts +26 -0
  116. package/dist/esm/craco/intlayerCracoPlugin.mjs +54 -0
  117. package/dist/esm/craco/intlayerCracoPlugin.mjs.map +1 -0
  118. package/dist/esm/getEnumeration.d.mts +28 -0
  119. package/dist/esm/getEnumeration.mjs +1 -0
  120. package/dist/esm/getEnumeration.mjs.map +1 -1
  121. package/dist/esm/getTranslation.d.mts +22 -1
  122. package/dist/esm/getTranslation.mjs +3 -4
  123. package/dist/esm/getTranslation.mjs.map +1 -1
  124. package/dist/esm/index.d.mts +3 -1
  125. package/dist/esm/index.mjs +10 -7
  126. package/dist/esm/index.mjs.map +1 -1
  127. package/dist/esm/processDictionary/index.d.mts +1 -1
  128. package/dist/esm/processDictionary/index.mjs +35 -4
  129. package/dist/esm/processDictionary/index.mjs.map +1 -1
  130. package/dist/{cjs/server/LocaleServerContextProvider.d.ts → esm/server/IntlayerServerProvider.d.mts} +17 -4
  131. package/dist/esm/server/IntlayerServerProvider.mjs +20 -0
  132. package/dist/esm/server/IntlayerServerProvider.mjs.map +1 -0
  133. package/dist/esm/server/getLocaleTranslation.d.mts +3 -0
  134. package/dist/esm/server/getLocaleTranslation.mjs +5 -4
  135. package/dist/esm/server/getLocaleTranslation.mjs.map +1 -1
  136. package/dist/esm/server/index.d.mts +1 -1
  137. package/dist/esm/server/index.mjs +6 -5
  138. package/dist/esm/server/index.mjs.map +1 -1
  139. package/dist/esm/server/serverContext.d.mts +4 -4
  140. package/dist/esm/server/serverContext.mjs +1 -0
  141. package/dist/esm/server/serverContext.mjs.map +1 -1
  142. package/dist/esm/server/useIntlayer.d.mts +5 -0
  143. package/dist/esm/server/useIntlayer.mjs +3 -2
  144. package/dist/esm/server/useIntlayer.mjs.map +1 -1
  145. package/dist/esm/server/useTraduction.d.mts +22 -1
  146. package/dist/esm/server/useTraduction.mjs +3 -2
  147. package/dist/esm/server/useTraduction.mjs.map +1 -1
  148. package/dist/esm/useIntlayerBase.d.mts +3 -6
  149. package/dist/esm/useIntlayerBase.mjs +1 -0
  150. package/dist/esm/useIntlayerBase.mjs.map +1 -1
  151. package/dist/esm/vite/intlayerPlugin.d.mts +17 -0
  152. package/dist/esm/vite/intlayerPlugin.mjs +36 -0
  153. package/dist/esm/vite/intlayerPlugin.mjs.map +1 -0
  154. package/package.json +32 -7
  155. package/src/IntlayerProvider.tsx +24 -0
  156. package/src/cli/react-intlayer.ts +60 -0
  157. package/src/client/IntlayerClientProvider.tsx +76 -0
  158. package/src/client/getBrowserLocale.tsx +192 -0
  159. package/src/client/index.ts +7 -5
  160. package/src/client/useContent.ts +5 -2
  161. package/src/client/useIntlayer.ts +7 -2
  162. package/src/client/useLocale.ts +9 -5
  163. package/src/client/useLocaleBase.ts +24 -0
  164. package/src/client/useLocaleCookie.ts +11 -6
  165. package/src/client/useTraduction.ts +24 -3
  166. package/src/craco/craco.config.ts +11 -0
  167. package/src/craco/intlayerCracoPlugin.ts +89 -0
  168. package/src/getEnumeration.ts +28 -0
  169. package/src/getTranslation.ts +24 -6
  170. package/src/index.ts +5 -5
  171. package/src/processDictionary/index.ts +53 -6
  172. package/src/server/IntlayerServerProvider.tsx +36 -0
  173. package/src/server/getLocaleTranslation.ts +7 -4
  174. package/src/server/index.ts +3 -3
  175. package/src/server/serverContext.ts +4 -4
  176. package/src/server/useIntlayer.ts +8 -2
  177. package/src/server/useTraduction.ts +24 -3
  178. package/src/useIntlayerBase.ts +4 -1
  179. package/src/vite/intlayerPlugin.ts +59 -0
  180. package/dist/cjs/LocaleContextProvider.cjs.map +0 -1
  181. package/dist/cjs/LocaleContextProvider.d.ts +0 -9
  182. package/dist/cjs/client/LocaleClientContextProvider.cjs.map +0 -1
  183. package/dist/cjs/client/LocaleClientContextProvider.d.ts +0 -15
  184. package/dist/cjs/server/LocaleServerContextProvider.cjs.map +0 -1
  185. package/dist/esm/LocaleContextProvider.d.mts +0 -9
  186. package/dist/esm/LocaleContextProvider.mjs +0 -17
  187. package/dist/esm/LocaleContextProvider.mjs.map +0 -1
  188. package/dist/esm/client/LocaleClientContextProvider.d.mts +0 -15
  189. package/dist/esm/client/LocaleClientContextProvider.mjs +0 -21
  190. package/dist/esm/client/LocaleClientContextProvider.mjs.map +0 -1
  191. package/dist/esm/server/LocaleServerContextProvider.mjs +0 -16
  192. package/dist/esm/server/LocaleServerContextProvider.mjs.map +0 -1
  193. package/src/LocaleContextProvider.tsx +0 -26
  194. package/src/client/LocaleClientContextProvider.tsx +0 -35
  195. package/src/server/LocaleServerContextProvider.tsx +0 -24
@@ -1,12 +1,33 @@
1
1
  import type { LanguageContent } from '@intlayer/core';
2
2
  import { useContext } from 'react';
3
3
  import { getTranslation } from '../getTranslation';
4
- import { LocaleClientContext } from './LocaleClientContextProvider';
4
+ import { IntlayerClientContext } from './IntlayerClientProvider';
5
5
 
6
- export const useTraduction = <Content>(
6
+ /**
7
+ * On the client side, Hook that picking one dictionary by its id and return the content.
8
+ *
9
+ * If not locale found, it will return the content related to the default locale.
10
+ *
11
+ * Return either the content editor, or the content itself depending on the configuration.
12
+ *
13
+ * Usage:
14
+ *
15
+ * ```tsx
16
+ * const content = useTraduction<string>({
17
+ * en: 'Hello',
18
+ * fr: 'Bonjour',
19
+ * }, 'fr');
20
+ * // 'Bonjour'
21
+ * ```
22
+ *
23
+ * Using TypeScript:
24
+ * - this function will require each locale to be defined if defined in the project configuration.
25
+ * - If a locale is missing, it will make each existing locale optional and raise an error if the locale is not found.
26
+ */
27
+ export const useTraduction = <Content = string>(
7
28
  languageContent: LanguageContent<Content>
8
29
  ): Content => {
9
- const { locale } = useContext(LocaleClientContext);
30
+ const { locale } = useContext(IntlayerClientContext);
10
31
 
11
32
  return getTranslation(languageContent, locale);
12
33
  };
@@ -0,0 +1,11 @@
1
+ import type { CracoConfig } from '@craco/types';
2
+ import * as intlayerPlugin from './intlayerCracoPlugin';
3
+
4
+ // Usage Example
5
+ module.exports = {
6
+ plugins: [
7
+ {
8
+ plugin: intlayerPlugin,
9
+ },
10
+ ],
11
+ } satisfies CracoConfig;
@@ -0,0 +1,89 @@
1
+ import { resolve, relative, join } from 'path';
2
+ import type {
3
+ CracoConfig,
4
+ CracoConfigOverride,
5
+ CracoPlugin,
6
+ WebpackConfigOverride,
7
+ } from '@craco/types';
8
+ import { getConfiguration, formatEnvVariable } from '@intlayer/config';
9
+ import { IntLayerPlugin as IntLayerWebpackPlugin } from '@intlayer/webpack';
10
+ import webpack, { type Configuration as WebpackConfig } from 'webpack';
11
+
12
+ // Get IntLayer configuration
13
+ const intlayerConfig = getConfiguration();
14
+
15
+ // Format environment variables
16
+ const env: Record<string, string> = formatEnvVariable('react_app');
17
+
18
+ // Custom CRACO plugin function to override webpack configuration
19
+ export const overrideWebpackConfig = ({
20
+ webpackConfig,
21
+ }: WebpackConfigOverride): WebpackConfig => {
22
+ webpackConfig.externals = {
23
+ ...(typeof webpackConfig.externals === 'object'
24
+ ? webpackConfig.externals
25
+ : {}),
26
+ esbuild: 'esbuild',
27
+ module: 'module',
28
+ fs: 'fs',
29
+ vm: 'vm',
30
+ };
31
+
32
+ (webpackConfig.module?.rules ?? []).push({
33
+ test: /\.node$/,
34
+ use: 'node-loader',
35
+ });
36
+
37
+ // You can add any custom CRACO plugins here if needed
38
+ // config.plugins.push(new CustomCracoPlugin());
39
+
40
+ return webpackConfig;
41
+ };
42
+
43
+ // Return a CRACO configuration object
44
+ export const overrideCracoConfig = ({
45
+ cracoConfig,
46
+ }: CracoConfigOverride): CracoConfig => {
47
+ const { mainDir, baseDir } = intlayerConfig.content;
48
+
49
+ const dictionariesPath = join(mainDir, 'dictionaries.mjs');
50
+ const relativeDictionariesPath = relative(baseDir, dictionariesPath);
51
+
52
+ return {
53
+ ...cracoConfig,
54
+ webpack: {
55
+ ...cracoConfig.webpack,
56
+ plugins: {
57
+ ...cracoConfig.webpack?.plugins,
58
+ add: [new webpack.EnvironmentPlugin(env), new IntLayerWebpackPlugin()],
59
+ },
60
+ alias: {
61
+ ...cracoConfig.webpack?.alias,
62
+ '@intlayer/dictionaries-entry': resolve('./', relativeDictionariesPath),
63
+ },
64
+ },
65
+ };
66
+ };
67
+
68
+ /**
69
+ * A CRACO plugin that adds the IntLayer configuration to the webpack configuration and sets the environment variables.
70
+ *
71
+ * Usage:
72
+ *
73
+ * ```ts
74
+ * const cracoConfig: CracoConfig = {
75
+ * plugins: [
76
+ * {
77
+ * plugin: intlayerCracoPlugin(),
78
+ * },
79
+ * ],
80
+ * };
81
+ *
82
+ * export default cracoConfig;
83
+ * ```
84
+ *
85
+ */
86
+ export const plugin: CracoPlugin = {
87
+ overrideCracoConfig,
88
+ overrideWebpackConfig,
89
+ };
@@ -1,6 +1,34 @@
1
1
  import { type QuantityContent, getEnumerationContent } from '@intlayer/core';
2
2
  import { contentRender } from './ContentEditor/contentRender';
3
3
 
4
+ /**
5
+ * Allow to pick a content based on a quantity.
6
+ *
7
+ * Return either the content editor, or the content itself depending on the configuration.
8
+ *
9
+ * Usage:
10
+ *
11
+ * ```ts
12
+ * const content = getEnumeration({
13
+ * '<=-2.3': 'You have less than -2.3',
14
+ * '<1': 'You have less than one',
15
+ * '2': 'You have two',
16
+ * '>=3': 'You have three or more',
17
+ * }, 2);
18
+ * // 'You have two'
19
+ * ```
20
+ *
21
+ * The order of the keys will define the priority of the content.
22
+ *
23
+ * ```ts
24
+ * const content = getEnumeration({
25
+ * '<4': 'You have less than four',
26
+ * '2': 'You have two',
27
+ * }, 2);
28
+ * // 'You have less than four'
29
+ * ```
30
+ *
31
+ */
4
32
  export const getEnumeration = <Content>(
5
33
  enumerationContent: QuantityContent<Content>,
6
34
  quantity: number
@@ -1,15 +1,33 @@
1
- import {
2
- type Locales,
3
- intlayerIntlConfiguration,
4
- } from '@intlayer/config/client';
1
+ import { type Locales, getConfiguration } from '@intlayer/config/client';
5
2
  import { type LanguageContent, getTranslationContent } from '@intlayer/core';
6
3
  import { contentRender } from './ContentEditor/contentRender';
7
4
 
8
- export const getTranslation = <Content>(
5
+ /**
6
+ *
7
+ * Allow to pick a content based on a locale.
8
+ * If not locale found, it will return the content related to the default locale.
9
+ *
10
+ * Return either the content editor, or the content itself depending on the configuration.
11
+ *
12
+ * Usage:
13
+ *
14
+ * ```ts
15
+ * const content = getTranslation<string>({
16
+ * en: 'Hello',
17
+ * fr: 'Bonjour',
18
+ * }, 'fr');
19
+ * // 'Bonjour'
20
+ * ```
21
+ *
22
+ * Using TypeScript:
23
+ * - this function will require each locale to be defined if defined in the project configuration.
24
+ * - If a locale is missing, it will make each existing locale optional and raise an error if the locale is not found.
25
+ */
26
+ export const getTranslation = <Content = string>(
9
27
  languageContent: LanguageContent<Content>,
10
28
  locale?: Locales
11
29
  ): Content => {
12
- const { defaultLocale } = intlayerIntlConfiguration;
30
+ const { defaultLocale } = getConfiguration().internationalization;
13
31
 
14
32
  const result: Content = getTranslationContent<Content>(
15
33
  languageContent,
package/src/index.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  export { getTranslation } from './getTranslation';
2
2
  export {
3
- LocaleClientContextProvider,
4
- type LocaleClientContextProviderProps,
5
- LocaleClientContext,
6
- useLocaleContext,
3
+ IntlayerClientProvider,
4
+ type IntlayerClientProviderProps,
5
+ IntlayerClient,
7
6
  useIntlayer,
8
7
  useLocale,
9
8
  useTraduction,
10
9
  useLocaleCookie,
11
10
  localeCookie,
12
11
  setLocaleCookie,
12
+ getBrowserLocale,
13
+ useLocaleBase,
13
14
  } from './client/index';
14
- export {} from './client/useLocaleCookie';
@@ -1,13 +1,11 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import {
3
- type Locales,
4
- intlayerIntlConfiguration,
5
- } from '@intlayer/config/client';
2
+ import { type Locales, getConfiguration } from '@intlayer/config/client';
6
3
  import {
7
4
  NodeType,
8
5
  type QuantityContent,
9
6
  type LanguageContent,
10
7
  } from '@intlayer/core';
8
+ import { type ReactElement, createElement, type ReactNode } from 'react';
11
9
  import { getEnumeration } from '../getEnumeration';
12
10
  import { getTranslation } from '../getTranslation';
13
11
  import type {
@@ -17,7 +15,7 @@ import type {
17
15
  TransformedContentValue,
18
16
  } from './contentDictionary';
19
17
 
20
- const defaultLocale = intlayerIntlConfiguration.defaultLocale;
18
+ const { defaultLocale } = getConfiguration().internationalization;
21
19
 
22
20
  const processTranslation = (
23
21
  languageContent: LanguageContent<ContentValue>,
@@ -46,6 +44,9 @@ const processEnumeration = (
46
44
  };
47
45
  };
48
46
 
47
+ const isReactNode = (node: Record<string, unknown>): boolean =>
48
+ typeof node?.key !== 'undefined' && typeof node?.props !== 'undefined';
49
+
49
50
  export const processNode = (
50
51
  field: ContentValue | undefined,
51
52
  locale: Locales
@@ -72,13 +73,59 @@ export const processNode = (
72
73
  return field as TransformedContentValue;
73
74
  };
74
75
 
76
+ // This function recursively creates React elements from a given JSON-like structure
77
+ const createReactElement = (element: ReactElement) => {
78
+ if (typeof element === 'string') {
79
+ // If it's a string, simply return it (used for text content)
80
+ return element;
81
+ }
82
+
83
+ // Destructure the component properties
84
+
85
+ const convertChildrenAsArray = (element: ReactElement): ReactElement => {
86
+ if (element?.props && typeof element.props.children === 'object') {
87
+ const childrenResult: ReactNode[] = [];
88
+ const { children } = element.props;
89
+
90
+ // Create the children elements recursively, if any
91
+ Object.keys(children).forEach((key) => {
92
+ childrenResult.push(createReactElement(children[key]));
93
+ });
94
+
95
+ return {
96
+ ...element,
97
+ props: { ...element.props, children: childrenResult },
98
+ };
99
+ }
100
+
101
+ return {
102
+ ...element,
103
+ props: { ...element.props, children: element.props.children },
104
+ };
105
+ };
106
+
107
+ const fixedElement = convertChildrenAsArray(element);
108
+
109
+ const { type, props } = fixedElement;
110
+
111
+ // Create and return the React element
112
+ return createElement(type ?? 'div', props, ...props.children);
113
+ };
114
+
75
115
  /**
76
- * Function to replace the multi lingual content with content in the current locale
116
+ * Function that process a dictionary and return the result to be used in the application.
77
117
  */
78
118
  export const processDictionary = (
79
119
  content: Content,
80
120
  locale: Locales = defaultLocale
81
121
  ): TransformedContent => {
122
+ // If it's a React element, render it
123
+ if (isReactNode(content)) {
124
+ return createReactElement(
125
+ content as unknown as ReactElement
126
+ ) as unknown as TransformedContent;
127
+ }
128
+
82
129
  if (content && typeof content === 'object') {
83
130
  const result: TransformedContent = {};
84
131
 
@@ -0,0 +1,36 @@
1
+ import { type Locales, getConfiguration } from '@intlayer/config/client';
2
+ import type { FC, PropsWithChildren } from 'react';
3
+ import { createServerContext, getServerContext } from './serverContext';
4
+
5
+ /**
6
+ * Context that store the current locale on the server side
7
+ */
8
+ export const IntlayerServerContext = createServerContext<Locales>(
9
+ getConfiguration().internationalization.defaultLocale
10
+ );
11
+
12
+ /**
13
+ * Hook that provides the current locale
14
+ */
15
+ export const useIntlayer = () => getServerContext(IntlayerServerContext);
16
+
17
+ /**
18
+ * Get the current locale
19
+ */
20
+ export const locale = getServerContext(IntlayerServerContext);
21
+
22
+ export type IntlayerServerProviderProps = PropsWithChildren & {
23
+ locale: Locales;
24
+ };
25
+
26
+ /**
27
+ * Provider that store the current locale on the server side
28
+ */
29
+ export const IntlayerServerProvider: FC<IntlayerServerProviderProps> = ({
30
+ children,
31
+ locale,
32
+ }) => (
33
+ <IntlayerServerContext.Provider value={locale}>
34
+ {children}
35
+ </IntlayerServerContext.Provider>
36
+ );
@@ -1,15 +1,18 @@
1
- import { intlayerIntlConfiguration } from '@intlayer/config/client';
1
+ import { getConfiguration } from '@intlayer/config/client';
2
2
  import { type LanguageContent, getTranslationContent } from '@intlayer/core';
3
- import { LocaleServerContext } from './LocaleServerContextProvider';
3
+ import { IntlayerServerContext } from './IntlayerServerProvider';
4
4
  import { getServerContext } from './serverContext';
5
5
 
6
+ /**
7
+ * On the server side, get the translation content based on the locale
8
+ */
6
9
  export const getLocaleTranslation = <Content>(
7
10
  languageContent: LanguageContent<Content>
8
11
  ) => {
9
- const locale = getServerContext(LocaleServerContext);
12
+ const locale = getServerContext(IntlayerServerContext);
10
13
  const content = getTranslationContent<Content>(
11
14
  languageContent,
12
- locale ?? intlayerIntlConfiguration.defaultLocale
15
+ locale ?? getConfiguration().internationalization.defaultLocale
13
16
  );
14
17
 
15
18
  return {
@@ -1,8 +1,8 @@
1
1
  export { getLocaleTranslation as getLocaleContent } from './getLocaleTranslation';
2
2
  export { useTraduction } from './useTraduction';
3
3
  export {
4
- LocaleServerContext,
4
+ IntlayerServerContext as IntlayerServer,
5
5
  locale,
6
- LocaleServerContextProvider,
7
- } from './LocaleServerContextProvider';
6
+ IntlayerServerProvider,
7
+ } from './IntlayerServerProvider';
8
8
  export { useIntlayer } from './useIntlayer';
@@ -3,11 +3,11 @@
3
3
  * Attempts to closely mimic the `createContext` API.
4
4
  *
5
5
  * @example
6
- * const LocaleServerContext = createServerContext<string | null>(null);
6
+ * const IntlayerServer = createServerContext<string | null>(null);
7
7
  *
8
- * <LocaleServerContext.Provider value={locale}>
8
+ * <IntlayerServer.Provider value={locale}>
9
9
  * {children}
10
- * </LocaleServerContext.Provider>
10
+ * </IntlayerServer.Provider>
11
11
  */
12
12
 
13
13
  import react from 'react';
@@ -44,7 +44,7 @@ export const createServerContext = <T>(defaultValue?: T): ServerContext<T> => {
44
44
  * Attempts to closely mimic the `useContext` API.
45
45
  *
46
46
  * @example
47
- * getServerContext(LocaleServerContext);
47
+ * getServerContext(IntlayerServer);
48
48
  */
49
49
  export const getServerContext = <T>({
50
50
  _storage,
@@ -4,14 +4,20 @@ import {
4
4
  useIntlayerBase,
5
5
  type UseIntlayer,
6
6
  } from '../useIntlayerBase';
7
- import { LocaleServerContext } from './LocaleServerContextProvider';
7
+ import { IntlayerServerContext } from './IntlayerServerProvider';
8
8
  import { getServerContext } from './serverContext';
9
9
 
10
+ /**
11
+ * On the server side, Hook that picking one dictionary by its id and return the content
12
+ *
13
+ * If the locale is not provided, it will use the locale from the server context
14
+ */
10
15
  export const useIntlayer: UseIntlayer = <T extends DictionaryKeys>(
11
16
  id: T,
12
17
  locale?: Locales
13
18
  ) => {
14
- const localeTarget = locale ?? getServerContext<Locales>(LocaleServerContext);
19
+ const localeTarget =
20
+ locale ?? getServerContext<Locales>(IntlayerServerContext);
15
21
 
16
22
  return useIntlayerBase(id, localeTarget);
17
23
  };
@@ -1,13 +1,34 @@
1
1
  import type { Locales } from '@intlayer/config/client';
2
2
  import type { LanguageContent } from '@intlayer/core';
3
3
  import { getTranslation } from '../getTranslation';
4
- import { LocaleServerContext } from './LocaleServerContextProvider';
4
+ import { IntlayerServerContext } from './IntlayerServerProvider';
5
5
  import { getServerContext } from './serverContext';
6
6
 
7
- export const useTraduction = <Content>(
7
+ /**
8
+ * On the server side, hook to get the translation content based on the locale
9
+ *
10
+ * If not locale found, it will return the content related to the default locale.
11
+ *
12
+ * Return either the content editor, or the content itself depending on the configuration.
13
+ *
14
+ * Usage:
15
+ *
16
+ * ```ts
17
+ * const content = useTraduction<string>({
18
+ * en: 'Hello',
19
+ * fr: 'Bonjour',
20
+ * }, 'fr');
21
+ * // 'Bonjour'
22
+ * ```
23
+ *
24
+ * Using TypeScript:
25
+ * - this function will require each locale to be defined if defined in the project configuration.
26
+ * - If a locale is missing, it will make each existing locale optional and raise an error if the locale is not found.
27
+ */
28
+ export const useTraduction = <Content = string>(
8
29
  languageContent: LanguageContent<Content>
9
30
  ) => {
10
- const locale = getServerContext<Locales>(LocaleServerContext);
31
+ const locale = getServerContext<Locales>(IntlayerServerContext);
11
32
 
12
33
  return getTranslation(languageContent, locale);
13
34
  };
@@ -1,9 +1,9 @@
1
+ import type { Locales } from '@intlayer/config/client';
1
2
  /**
2
3
  * @intlayer/dictionaries-entry is a package that only returns the dictionary entry path.
3
4
  * Using an external package allow to alias it in the bundle configuration (such as webpack).
4
5
  * The alias allow hot reload the app (such as nextjs) on any dictionary change.
5
6
  */
6
- import type { Locales } from '@intlayer/config/client';
7
7
  import dictionaries from '@intlayer/dictionaries-entry';
8
8
  import type { IntlayerDictionaryTypesConnector } from 'intlayer';
9
9
  import type { Content } from './processDictionary/contentDictionary';
@@ -19,6 +19,9 @@ export type UseIntlayer = <T extends DictionaryKeys>(
19
19
  locale?: Locales
20
20
  ) => IntlayerDictionaryTypesConnector[T];
21
21
 
22
+ /**
23
+ * Hook that picking one dictionary by its id and return the content.
24
+ */
22
25
  export const useIntlayerBase: UseIntlayer = <T extends DictionaryKeys>(
23
26
  id: T,
24
27
  locale?: Locales
@@ -0,0 +1,59 @@
1
+ import { join, relative, resolve } from 'path';
2
+ import { watch } from '@intlayer/chokidar';
3
+ import { getConfiguration, formatEnvVariable } from '@intlayer/config';
4
+ import { loadEnv, type Plugin } from 'vite';
5
+
6
+ // Plugin options type definition
7
+ type PluginOptions = {
8
+ // Custom options for your plugin, if any
9
+ };
10
+
11
+ /**
12
+ *
13
+ * A Vite plugin that integrates IntLayer configuration into the build process
14
+ *
15
+ * ```ts
16
+ * // Example usage of the plugin in a Vite configuration
17
+ * export default defineConfig({
18
+ * plugins: [ intLayerPlugin() ],
19
+ * });
20
+ * ```
21
+ * */
22
+ export const intLayerPlugin = (_pluginOptions: PluginOptions = {}): Plugin => ({
23
+ name: 'vite-intlayer-plugin',
24
+
25
+ config: (config, { mode }) => {
26
+ const intlayerConfig = getConfiguration();
27
+
28
+ // Set all configuration values as environment variables
29
+ const env = formatEnvVariable('vite');
30
+
31
+ process.env = { ...process.env, ...loadEnv(mode, process.cwd()), ...env };
32
+
33
+ const { mainDir, baseDir } = intlayerConfig.content;
34
+ const dictionariesPath = join(mainDir, 'dictionaries.mjs');
35
+ const relativeDictionariesPath = relative(baseDir, dictionariesPath);
36
+
37
+ // Update Vite's resolve alias
38
+ config.resolve = {
39
+ ...config.resolve,
40
+ alias: {
41
+ ...config.resolve?.alias,
42
+ '@intlayer/dictionaries-entry': resolve(relativeDictionariesPath),
43
+ },
44
+ };
45
+
46
+ return config;
47
+ },
48
+
49
+ buildStart: () => {
50
+ // Code to run when Vite build starts
51
+ watch({
52
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
+ persistent: (import.meta as any).env === 'development',
54
+ });
55
+ },
56
+ configureServer: (server) => {
57
+ // Custom server configuration, if needed
58
+ },
59
+ });
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/LocaleContextProvider.tsx"],"sourcesContent":["import { intlayerIntlConfiguration } from '@intlayer/config/client';\nimport React, { type FC } from 'react';\nimport {\n LocaleClientContextProvider,\n type LocaleClientContextProviderProps,\n} from './client/LocaleClientContextProvider';\nimport {\n LocaleServerContextProvider,\n type LocaleServerContextProviderProps,\n} from './server/LocaleServerContextProvider';\n\nconst defaultLocale = intlayerIntlConfiguration.defaultLocale;\n\ntype LocaleContextProviderProps = LocaleClientContextProviderProps &\n LocaleServerContextProviderProps;\n\nexport const LocaleContextProvider: FC<LocaleContextProviderProps> = ({\n children,\n locale = defaultLocale,\n}) => (\n <LocaleServerContextProvider locale={locale}>\n <LocaleClientContextProvider locale={locale}>\n {children}\n </LocaleClientContextProvider>\n </LocaleServerContextProvider>\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAqBI;AArBJ,oBAA0C;AAE1C,yCAGO;AACP,yCAGO;AAEP,MAAM,gBAAgB,wCAA0B;AAKzC,MAAM,wBAAwD,CAAC;AAAA,EACpE;AAAA,EACA,SAAS;AACX,MACE,4CAAC,kEAA4B,QAC3B,sDAAC,kEAA4B,QAC1B,UACH,GACF;","names":[]}
@@ -1,9 +0,0 @@
1
- import { FC } from 'react';
2
- import { LocaleClientContextProviderProps } from './client/LocaleClientContextProvider.js';
3
- import { LocaleServerContextProviderProps } from './server/LocaleServerContextProvider.js';
4
- import '@intlayer/config/client';
5
-
6
- type LocaleContextProviderProps = LocaleClientContextProviderProps & LocaleServerContextProviderProps;
7
- declare const LocaleContextProvider: FC<LocaleContextProviderProps>;
8
-
9
- export { LocaleContextProvider };
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/client/LocaleClientContextProvider.tsx"],"sourcesContent":["'use client';\n\nimport {\n intlayerIntlConfiguration,\n type Locales,\n} from '@intlayer/config/client';\nimport {\n type PropsWithChildren,\n createContext,\n useContext,\n type FC,\n} from 'react';\nimport { localeCookie } from './useLocaleCookie';\n\ntype LocaleContextValue = {\n locale: Locales;\n};\n\nexport const LocaleClientContext = createContext<LocaleContextValue>({\n locale: localeCookie ?? intlayerIntlConfiguration.defaultLocale,\n});\n\nexport const useLocaleContext = () => useContext(LocaleClientContext);\n\nexport type LocaleClientContextProviderProps = PropsWithChildren & {\n locale: Locales;\n};\n\nexport const LocaleClientContextProvider: FC<\n LocaleClientContextProviderProps\n> = ({ locale, children }) => (\n <LocaleClientContext.Provider value={{ locale }}>\n {children}\n </LocaleClientContext.Provider>\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA+BE;AA7BF,oBAGO;AACP,mBAKO;AACP,6BAA6B;AAMtB,MAAM,0BAAsB,4BAAkC;AAAA,EACnE,QAAQ,uCAAgB,wCAA0B;AACpD,CAAC;AAEM,MAAM,mBAAmB,UAAM,yBAAW,mBAAmB;AAM7D,MAAM,8BAET,CAAC,EAAE,QAAQ,SAAS,MACtB,4CAAC,oBAAoB,UAApB,EAA6B,OAAO,EAAE,OAAO,GAC3C,UACH;","names":[]}
@@ -1,15 +0,0 @@
1
- import * as react from 'react';
2
- import { PropsWithChildren, FC } from 'react';
3
- import { Locales } from '@intlayer/config/client';
4
-
5
- type LocaleContextValue = {
6
- locale: Locales;
7
- };
8
- declare const LocaleClientContext: react.Context<LocaleContextValue>;
9
- declare const useLocaleContext: () => LocaleContextValue;
10
- type LocaleClientContextProviderProps = PropsWithChildren & {
11
- locale: Locales;
12
- };
13
- declare const LocaleClientContextProvider: FC<LocaleClientContextProviderProps>;
14
-
15
- export { LocaleClientContext, LocaleClientContextProvider, type LocaleClientContextProviderProps, useLocaleContext };
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/server/LocaleServerContextProvider.tsx"],"sourcesContent":["import {\n type Locales,\n intlayerIntlConfiguration,\n} from '@intlayer/config/client';\nimport type { FC, PropsWithChildren } from 'react';\nimport { createServerContext, getServerContext } from './serverContext';\n\nexport const LocaleServerContext = createServerContext<Locales>(\n intlayerIntlConfiguration.defaultLocale\n);\n\nexport const locale = getServerContext(LocaleServerContext);\n\nexport type LocaleServerContextProviderProps = PropsWithChildren & {\n locale: Locales;\n};\n\nexport const LocaleServerContextProvider: FC<\n LocaleServerContextProviderProps\n> = ({ children, locale }) => (\n <LocaleServerContext.Provider value={locale}>\n {children}\n </LocaleServerContext.Provider>\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAoBE;AApBF,oBAGO;AAEP,2BAAsD;AAE/C,MAAM,0BAAsB;AAAA,EACjC,wCAA0B;AAC5B;AAEO,MAAM,aAAS,uCAAiB,mBAAmB;AAMnD,MAAM,8BAET,CAAC,EAAE,UAAU,QAAAA,QAAO,MACtB,4CAAC,oBAAoB,UAApB,EAA6B,OAAOA,SAClC,UACH;","names":["locale"]}
@@ -1,9 +0,0 @@
1
- import { FC } from 'react';
2
- import { LocaleClientContextProviderProps } from './client/LocaleClientContextProvider.mjs';
3
- import { LocaleServerContextProviderProps } from './server/LocaleServerContextProvider.mjs';
4
- import '@intlayer/config/client';
5
-
6
- type LocaleContextProviderProps = LocaleClientContextProviderProps & LocaleServerContextProviderProps;
7
- declare const LocaleContextProvider: FC<LocaleContextProviderProps>;
8
-
9
- export { LocaleContextProvider };
@@ -1,17 +0,0 @@
1
- import { jsx } from "react/jsx-runtime";
2
- import { intlayerIntlConfiguration } from "@intlayer/config/client";
3
- import {
4
- LocaleClientContextProvider
5
- } from './client/LocaleClientContextProvider.mjs';
6
- import {
7
- LocaleServerContextProvider
8
- } from './server/LocaleServerContextProvider.mjs';
9
- const defaultLocale = intlayerIntlConfiguration.defaultLocale;
10
- const LocaleContextProvider = ({
11
- children,
12
- locale = defaultLocale
13
- }) => /* @__PURE__ */ jsx(LocaleServerContextProvider, { locale, children: /* @__PURE__ */ jsx(LocaleClientContextProvider, { locale, children }) });
14
- export {
15
- LocaleContextProvider
16
- };
17
- //# sourceMappingURL=LocaleContextProvider.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/LocaleContextProvider.tsx"],"sourcesContent":["import { intlayerIntlConfiguration } from '@intlayer/config/client';\nimport React, { type FC } from 'react';\nimport {\n LocaleClientContextProvider,\n type LocaleClientContextProviderProps,\n} from './client/LocaleClientContextProvider';\nimport {\n LocaleServerContextProvider,\n type LocaleServerContextProviderProps,\n} from './server/LocaleServerContextProvider';\n\nconst defaultLocale = intlayerIntlConfiguration.defaultLocale;\n\ntype LocaleContextProviderProps = LocaleClientContextProviderProps &\n LocaleServerContextProviderProps;\n\nexport const LocaleContextProvider: FC<LocaleContextProviderProps> = ({\n children,\n locale = defaultLocale,\n}) => (\n <LocaleServerContextProvider locale={locale}>\n <LocaleClientContextProvider locale={locale}>\n {children}\n </LocaleClientContextProvider>\n </LocaleServerContextProvider>\n);\n"],"mappings":"AAqBI;AArBJ,SAAS,iCAAiC;AAE1C;AAAA,EACE;AAAA,OAEK;AACP;AAAA,EACE;AAAA,OAEK;AAEP,MAAM,gBAAgB,0BAA0B;AAKzC,MAAM,wBAAwD,CAAC;AAAA,EACpE;AAAA,EACA,SAAS;AACX,MACE,oBAAC,+BAA4B,QAC3B,8BAAC,+BAA4B,QAC1B,UACH,GACF;","names":[]}
@@ -1,15 +0,0 @@
1
- import * as react from 'react';
2
- import { PropsWithChildren, FC } from 'react';
3
- import { Locales } from '@intlayer/config/client';
4
-
5
- type LocaleContextValue = {
6
- locale: Locales;
7
- };
8
- declare const LocaleClientContext: react.Context<LocaleContextValue>;
9
- declare const useLocaleContext: () => LocaleContextValue;
10
- type LocaleClientContextProviderProps = PropsWithChildren & {
11
- locale: Locales;
12
- };
13
- declare const LocaleClientContextProvider: FC<LocaleClientContextProviderProps>;
14
-
15
- export { LocaleClientContext, LocaleClientContextProvider, type LocaleClientContextProviderProps, useLocaleContext };