react-intlayer 1.2.0 → 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 (142) hide show
  1. package/dist/cjs/IntlayerProvider.cjs +1 -1
  2. package/dist/cjs/IntlayerProvider.cjs.map +1 -1
  3. package/dist/cjs/cli/react-intlayer.cjs +68 -0
  4. package/dist/cjs/cli/react-intlayer.cjs.map +1 -0
  5. package/dist/cjs/cli/react-intlayer.d.ts +1 -0
  6. package/dist/cjs/client/IntlayerClientProvider.cjs +26 -2
  7. package/dist/cjs/client/IntlayerClientProvider.cjs.map +1 -1
  8. package/dist/cjs/client/IntlayerClientProvider.d.ts +2 -1
  9. package/dist/cjs/client/getBrowserLocale.cjs +170 -0
  10. package/dist/cjs/client/getBrowserLocale.cjs.map +1 -0
  11. package/dist/cjs/client/getBrowserLocale.d.ts +26 -0
  12. package/dist/cjs/client/index.cjs +6 -0
  13. package/dist/cjs/client/index.cjs.map +1 -1
  14. package/dist/cjs/client/index.d.ts +2 -0
  15. package/dist/cjs/client/useContent.cjs +2 -2
  16. package/dist/cjs/client/useContent.cjs.map +1 -1
  17. package/dist/cjs/client/useLocale.cjs +5 -3
  18. package/dist/cjs/client/useLocale.cjs.map +1 -1
  19. package/dist/cjs/client/useLocale.d.ts +1 -0
  20. package/dist/cjs/client/useLocaleBase.cjs +49 -0
  21. package/dist/cjs/client/useLocaleBase.cjs.map +1 -0
  22. package/dist/cjs/client/useLocaleBase.d.ts +14 -0
  23. package/dist/cjs/client/useLocaleCookie.cjs +1 -1
  24. package/dist/cjs/client/useLocaleCookie.cjs.map +1 -1
  25. package/dist/cjs/client/useTraduction.cjs.map +1 -1
  26. package/dist/cjs/client/useTraduction.d.ts +6 -5
  27. package/dist/cjs/craco/craco.config.cjs +32 -0
  28. package/dist/cjs/craco/craco.config.cjs.map +1 -0
  29. package/dist/cjs/craco/craco.config.d.ts +2 -0
  30. package/dist/cjs/craco/intlayerCracoPlugin.cjs +89 -0
  31. package/dist/cjs/craco/intlayerCracoPlugin.cjs.map +1 -0
  32. package/dist/cjs/craco/intlayerCracoPlugin.d.ts +26 -0
  33. package/dist/cjs/getEnumeration.cjs.map +1 -1
  34. package/dist/cjs/getEnumeration.d.ts +8 -5
  35. package/dist/cjs/getTranslation.cjs +1 -1
  36. package/dist/cjs/getTranslation.cjs.map +1 -1
  37. package/dist/cjs/getTranslation.d.ts +3 -2
  38. package/dist/cjs/index.cjs +4 -0
  39. package/dist/cjs/index.cjs.map +1 -1
  40. package/dist/cjs/index.d.ts +2 -0
  41. package/dist/cjs/processDictionary/index.cjs +33 -1
  42. package/dist/cjs/processDictionary/index.cjs.map +1 -1
  43. package/dist/cjs/server/IntlayerServerProvider.cjs +1 -1
  44. package/dist/cjs/server/IntlayerServerProvider.cjs.map +1 -1
  45. package/dist/cjs/server/getLocaleTranslation.cjs +1 -1
  46. package/dist/cjs/server/getLocaleTranslation.cjs.map +1 -1
  47. package/dist/cjs/server/useTraduction.cjs.map +1 -1
  48. package/dist/cjs/server/useTraduction.d.ts +4 -3
  49. package/dist/cjs/vite/intlayerPlugin.cjs +60 -0
  50. package/dist/cjs/vite/intlayerPlugin.cjs.map +1 -0
  51. package/dist/cjs/vite/intlayerPlugin.d.ts +17 -0
  52. package/dist/esm/ContentEditor/ContentEditor.mjs +1 -0
  53. package/dist/esm/ContentEditor/ContentEditor.mjs.map +1 -1
  54. package/dist/esm/ContentEditor/contentRender.mjs +1 -0
  55. package/dist/esm/ContentEditor/contentRender.mjs.map +1 -1
  56. package/dist/esm/IntlayerProvider.mjs +3 -2
  57. package/dist/esm/IntlayerProvider.mjs.map +1 -1
  58. package/dist/esm/chunk-ZNCO4QRF.mjs +9 -0
  59. package/dist/esm/chunk-ZNCO4QRF.mjs.map +1 -0
  60. package/dist/esm/cli/react-intlayer.d.mts +1 -0
  61. package/dist/esm/cli/react-intlayer.mjs +45 -0
  62. package/dist/esm/cli/react-intlayer.mjs.map +1 -0
  63. package/dist/esm/client/IntlayerClientProvider.d.mts +2 -1
  64. package/dist/esm/client/IntlayerClientProvider.mjs +33 -7
  65. package/dist/esm/client/IntlayerClientProvider.mjs.map +1 -1
  66. package/dist/esm/client/getBrowserLocale.d.mts +26 -0
  67. package/dist/esm/client/getBrowserLocale.mjs +146 -0
  68. package/dist/esm/client/getBrowserLocale.mjs.map +1 -0
  69. package/dist/esm/client/index.d.mts +2 -0
  70. package/dist/esm/client/index.mjs +5 -0
  71. package/dist/esm/client/index.mjs.map +1 -1
  72. package/dist/esm/client/useContent.mjs +3 -2
  73. package/dist/esm/client/useContent.mjs.map +1 -1
  74. package/dist/esm/client/useIntlayer.mjs +1 -0
  75. package/dist/esm/client/useIntlayer.mjs.map +1 -1
  76. package/dist/esm/client/useLocale.d.mts +1 -0
  77. package/dist/esm/client/useLocale.mjs +7 -4
  78. package/dist/esm/client/useLocale.mjs.map +1 -1
  79. package/dist/esm/client/useLocaleBase.d.mts +14 -0
  80. package/dist/esm/client/useLocaleBase.mjs +26 -0
  81. package/dist/esm/client/useLocaleBase.mjs.map +1 -0
  82. package/dist/esm/client/useLocaleCookie.mjs +3 -4
  83. package/dist/esm/client/useLocaleCookie.mjs.map +1 -1
  84. package/dist/esm/client/useTraduction.d.mts +6 -5
  85. package/dist/esm/client/useTraduction.mjs +1 -0
  86. package/dist/esm/client/useTraduction.mjs.map +1 -1
  87. package/dist/esm/craco/craco.config.d.mts +2 -0
  88. package/dist/esm/craco/craco.config.mjs +17 -0
  89. package/dist/esm/craco/craco.config.mjs.map +1 -0
  90. package/dist/esm/craco/intlayerCracoPlugin.d.mts +26 -0
  91. package/dist/esm/craco/intlayerCracoPlugin.mjs +54 -0
  92. package/dist/esm/craco/intlayerCracoPlugin.mjs.map +1 -0
  93. package/dist/esm/getEnumeration.d.mts +8 -5
  94. package/dist/esm/getEnumeration.mjs +1 -0
  95. package/dist/esm/getEnumeration.mjs.map +1 -1
  96. package/dist/esm/getTranslation.d.mts +3 -2
  97. package/dist/esm/getTranslation.mjs +3 -4
  98. package/dist/esm/getTranslation.mjs.map +1 -1
  99. package/dist/esm/index.d.mts +2 -0
  100. package/dist/esm/index.mjs +6 -1
  101. package/dist/esm/index.mjs.map +1 -1
  102. package/dist/esm/processDictionary/index.mjs +35 -4
  103. package/dist/esm/processDictionary/index.mjs.map +1 -1
  104. package/dist/esm/server/IntlayerServerProvider.mjs +3 -4
  105. package/dist/esm/server/IntlayerServerProvider.mjs.map +1 -1
  106. package/dist/esm/server/getLocaleTranslation.mjs +3 -2
  107. package/dist/esm/server/getLocaleTranslation.mjs.map +1 -1
  108. package/dist/esm/server/index.mjs +1 -0
  109. package/dist/esm/server/index.mjs.map +1 -1
  110. package/dist/esm/server/serverContext.mjs +1 -0
  111. package/dist/esm/server/serverContext.mjs.map +1 -1
  112. package/dist/esm/server/useIntlayer.mjs +1 -0
  113. package/dist/esm/server/useIntlayer.mjs.map +1 -1
  114. package/dist/esm/server/useTraduction.d.mts +4 -3
  115. package/dist/esm/server/useTraduction.mjs +1 -0
  116. package/dist/esm/server/useTraduction.mjs.map +1 -1
  117. package/dist/esm/useIntlayerBase.mjs +1 -0
  118. package/dist/esm/useIntlayerBase.mjs.map +1 -1
  119. package/dist/esm/vite/intlayerPlugin.d.mts +17 -0
  120. package/dist/esm/vite/intlayerPlugin.mjs +36 -0
  121. package/dist/esm/vite/intlayerPlugin.mjs.map +1 -0
  122. package/package.json +30 -5
  123. package/src/IntlayerProvider.tsx +2 -2
  124. package/src/cli/react-intlayer.ts +60 -0
  125. package/src/client/IntlayerClientProvider.tsx +43 -12
  126. package/src/client/getBrowserLocale.tsx +192 -0
  127. package/src/client/index.ts +2 -0
  128. package/src/client/useContent.ts +2 -2
  129. package/src/client/useLocale.ts +5 -4
  130. package/src/client/useLocaleBase.ts +24 -0
  131. package/src/client/useLocaleCookie.ts +2 -5
  132. package/src/client/useTraduction.ts +6 -5
  133. package/src/craco/craco.config.ts +11 -0
  134. package/src/craco/intlayerCracoPlugin.ts +89 -0
  135. package/src/getEnumeration.ts +8 -5
  136. package/src/getTranslation.ts +5 -7
  137. package/src/index.ts +2 -1
  138. package/src/processDictionary/index.ts +52 -5
  139. package/src/server/IntlayerServerProvider.tsx +2 -5
  140. package/src/server/getLocaleTranslation.ts +2 -2
  141. package/src/server/useTraduction.ts +4 -3
  142. package/src/vite/intlayerPlugin.ts +59 -0
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * To make the setup easier, we are using craco to override the webpack configuration.
5
+ * This script is used to run the craco scripts with the custom configuration.
6
+ *
7
+ * The script is based on the original craco script from create-react-app.
8
+ */
9
+
10
+ import spawn from 'cross-spawn';
11
+
12
+ const args = process.argv.slice(2);
13
+ const scriptIndex = args.findIndex(
14
+ (x) => x === 'build' || x === 'start' || x === 'test'
15
+ );
16
+ const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
17
+
18
+ switch (script) {
19
+ case 'build':
20
+ case 'start':
21
+ case 'test': {
22
+ const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];
23
+ const scriptPath = require.resolve(`@craco/craco/dist/scripts/${script}`);
24
+ const scriptArgs = args.slice(scriptIndex + 1);
25
+ const processArgs = nodeArgs
26
+ .concat(scriptPath)
27
+ .concat([
28
+ ...scriptArgs,
29
+ '--config',
30
+ './node_modules/react-intlayer/dist/cjs/craco/craco.config.cjs',
31
+ ]);
32
+
33
+ const child = spawn.sync('node', processArgs, { stdio: 'inherit' });
34
+
35
+ if (child.signal) {
36
+ if (child.signal === 'SIGKILL') {
37
+ console.info(`
38
+ The build failed because the process exited too early.
39
+ This probably means the system ran out of memory or someone called
40
+ \`kill -9\` on the process.
41
+ `);
42
+ } else if (child.signal === 'SIGTERM') {
43
+ console.info(`
44
+ The build failed because the process exited too early.
45
+ Someone might have called \`kill\` or \`killall\`, or the system could
46
+ be shutting down.
47
+ `);
48
+ }
49
+
50
+ process.exit(1);
51
+ }
52
+
53
+ process.exit(child.status ?? undefined);
54
+ break;
55
+ }
56
+ default:
57
+ console.info(`Unknown script "${script}".`);
58
+ console.info('Perhaps you need to update craco?');
59
+ break;
60
+ }
@@ -1,26 +1,28 @@
1
1
  'use client';
2
2
 
3
- import {
4
- intlayerIntlConfiguration,
5
- type Locales,
6
- } from '@intlayer/config/client';
3
+ import { getConfiguration, type Locales } from '@intlayer/config/client';
7
4
  import {
8
5
  type PropsWithChildren,
9
6
  createContext,
10
7
  useContext,
8
+ useMemo,
11
9
  type FC,
10
+ useState,
11
+ useCallback,
12
12
  } from 'react';
13
- import { localeCookie } from './useLocaleCookie';
13
+ import { localeCookie, setLocaleCookie } from './useLocaleCookie';
14
14
 
15
15
  type IntlayerValue = {
16
16
  locale: Locales;
17
+ setLocale: (newLocale: Locales) => void;
17
18
  };
18
19
 
19
20
  /**
20
21
  * Context that store the current locale on the client side
21
22
  */
22
23
  export const IntlayerClientContext = createContext<IntlayerValue>({
23
- locale: localeCookie ?? intlayerIntlConfiguration.defaultLocale,
24
+ locale: localeCookie ?? getConfiguration().internationalization.defaultLocale,
25
+ setLocale: () => null,
24
26
  });
25
27
 
26
28
  /**
@@ -29,7 +31,7 @@ export const IntlayerClientContext = createContext<IntlayerValue>({
29
31
  export const useIntlayerContext = () => useContext(IntlayerClientContext);
30
32
 
31
33
  export type IntlayerClientProviderProps = PropsWithChildren & {
32
- locale: Locales;
34
+ locale?: Locales;
33
35
  };
34
36
 
35
37
  /**
@@ -38,8 +40,37 @@ export type IntlayerClientProviderProps = PropsWithChildren & {
38
40
  export const IntlayerClientProvider: FC<IntlayerClientProviderProps> = ({
39
41
  locale,
40
42
  children,
41
- }) => (
42
- <IntlayerClientContext.Provider value={{ locale }}>
43
- {children}
44
- </IntlayerClientContext.Provider>
45
- );
43
+ }) => {
44
+ const { defaultLocale, locales: availableLocales } =
45
+ getConfiguration().internationalization;
46
+
47
+ const [currentLocale, setCurrentLocale] = useState(
48
+ locale ?? localeCookie ?? defaultLocale
49
+ );
50
+
51
+ const setLocale = useCallback(
52
+ (newLocale: Locales) => {
53
+ if (currentLocale.toString() === newLocale.toString()) return;
54
+
55
+ if (!availableLocales.includes(newLocale)) {
56
+ console.error(`Locale ${locale} is not available`);
57
+ return;
58
+ }
59
+
60
+ setCurrentLocale(newLocale); // Update state
61
+ setLocaleCookie(newLocale); // Optionally set cookie for persistence
62
+ },
63
+ [availableLocales, currentLocale, locale]
64
+ );
65
+
66
+ const value: IntlayerValue = useMemo<IntlayerValue>(
67
+ () => ({ locale: currentLocale, setLocale }),
68
+ [currentLocale, setLocale]
69
+ );
70
+
71
+ return (
72
+ <IntlayerClientContext.Provider value={value}>
73
+ {children}
74
+ </IntlayerClientContext.Provider>
75
+ );
76
+ };
@@ -0,0 +1,192 @@
1
+ import { getConfiguration, type Locales } from '@intlayer/config/client';
2
+ import { localeList } from '@intlayer/core';
3
+
4
+ export enum LanguageDetector {
5
+ Querystring = 'querystring',
6
+ Cookie = 'cookie',
7
+ LocalStorage = 'localStorage',
8
+ SessionStorage = 'sessionStorage',
9
+ Navigator = 'navigator',
10
+ HtmlTag = 'htmlTag',
11
+ }
12
+
13
+ // Default settings for the language detector
14
+ type LanguageDetectorOptions = {
15
+ order?: LanguageDetector[];
16
+ lookupQuerystring?: string;
17
+ lookupCookie?: string;
18
+ lookupLocalStorage?: string;
19
+ lookupSessionStorage?: string;
20
+ excludeCacheFor?: string[];
21
+ htmlTag?: HTMLElement | null;
22
+ };
23
+
24
+ const getDefaultsOptions = (): LanguageDetectorOptions => {
25
+ const { middleware } = getConfiguration();
26
+
27
+ return {
28
+ order: [
29
+ LanguageDetector.Querystring,
30
+ LanguageDetector.Cookie,
31
+ LanguageDetector.Navigator,
32
+ LanguageDetector.HtmlTag,
33
+ ],
34
+ lookupQuerystring: 'locale',
35
+ lookupCookie: middleware.cookieName,
36
+ htmlTag: document.documentElement,
37
+ };
38
+ };
39
+
40
+ // Helper functions for various checks and operations
41
+ const isLocalStorageAvailable = (): boolean => {
42
+ try {
43
+ if (typeof window === 'undefined') return false;
44
+ const testKey = 'intlayer.translate.boo';
45
+ window.localStorage.setItem(testKey, 'foo');
46
+ window.localStorage.removeItem(testKey);
47
+ return true;
48
+ } catch {
49
+ return false;
50
+ }
51
+ };
52
+
53
+ const isSessionStorageAvailable = (): boolean => {
54
+ try {
55
+ if (typeof window === 'undefined') return false;
56
+ const testKey = 'intlayer.translate.boo';
57
+ window.sessionStorage.setItem(testKey, 'foo');
58
+ window.sessionStorage.removeItem(testKey);
59
+ return true;
60
+ } catch {
61
+ return false;
62
+ }
63
+ };
64
+
65
+ // Function to detect language using different detectors
66
+ const detectLanguage = (
67
+ order: string[],
68
+ options: LanguageDetectorOptions
69
+ ): Record<LanguageDetector, Locales | Locales[]> => {
70
+ const detected: Record<LanguageDetector, Locales | Locales[]> = {} as Record<
71
+ LanguageDetector,
72
+ Locales | Locales[]
73
+ >;
74
+
75
+ const queryStringDetector = () => {
76
+ if (typeof window === 'undefined') return;
77
+ const search = window.location.search || '';
78
+ const params = new URLSearchParams(search);
79
+ const value = params.get(options.lookupQuerystring ?? '');
80
+ if (value) {
81
+ detected[LanguageDetector.Querystring] = value as Locales;
82
+ }
83
+ };
84
+
85
+ const cookieDetector = () => {
86
+ if (typeof document === 'undefined') return;
87
+ const cookies = document.cookie.split(';');
88
+ const cookieName = `${options.lookupCookie ?? ''}=`;
89
+ const cookie = cookies.find((c) => c.trim().startsWith(cookieName));
90
+ if (cookie) {
91
+ const value = cookie.split('=')[1].trim();
92
+
93
+ detected[LanguageDetector.Cookie] = value as Locales;
94
+ }
95
+ };
96
+
97
+ const localStorageDetector = () => {
98
+ if (!isLocalStorageAvailable()) return;
99
+ const value = window.localStorage.getItem(options.lookupLocalStorage ?? '');
100
+ if (value) {
101
+ detected[LanguageDetector.LocalStorage] = value as Locales;
102
+ }
103
+ };
104
+
105
+ const sessionStorageDetector = () => {
106
+ if (!isSessionStorageAvailable()) return;
107
+ const value = window.sessionStorage.getItem(
108
+ options.lookupSessionStorage ?? ''
109
+ );
110
+ if (value) {
111
+ detected[LanguageDetector.SessionStorage] = value as Locales;
112
+ }
113
+ };
114
+
115
+ const navigatorDetector = () => {
116
+ if (typeof navigator === 'undefined') return;
117
+
118
+ if (navigator.language) {
119
+ detected[LanguageDetector.Navigator] = navigator.language as Locales;
120
+ }
121
+ };
122
+
123
+ const htmlTagDetector = () => {
124
+ const htmlTag = options.htmlTag;
125
+ if (htmlTag && typeof htmlTag.getAttribute === 'function') {
126
+ const lang = htmlTag.getAttribute('lang');
127
+ if (lang) {
128
+ detected[LanguageDetector.HtmlTag] = lang as Locales;
129
+ }
130
+ }
131
+ };
132
+
133
+ // Map detector names to their corresponding functions
134
+ const detectors: Record<string, () => void> = {
135
+ [LanguageDetector.Querystring]: queryStringDetector,
136
+ [LanguageDetector.Cookie]: cookieDetector,
137
+ [LanguageDetector.LocalStorage]: localStorageDetector,
138
+ [LanguageDetector.SessionStorage]: sessionStorageDetector,
139
+ [LanguageDetector.Navigator]: navigatorDetector,
140
+ [LanguageDetector.HtmlTag]: htmlTagDetector,
141
+ };
142
+
143
+ // Use the provided order to run each detector
144
+ order.forEach((detectorName) => {
145
+ detectors[detectorName]?.();
146
+ });
147
+
148
+ return detected;
149
+ };
150
+
151
+ const getFirstAvailableLocale = (
152
+ locales: Record<LanguageDetector, Locales | Locales[]>,
153
+ order: LanguageDetector[]
154
+ ): Locales => {
155
+ const { internationalization } = getConfiguration();
156
+
157
+ for (const detector of order) {
158
+ const localesArray = [locales[detector]].flat() as Locales[];
159
+
160
+ for (const locale of localesArray) {
161
+ if (
162
+ locale &&
163
+ (internationalization.locales ?? localeList).includes(locale)
164
+ ) {
165
+ return locale;
166
+ } else if (
167
+ locale?.includes('-') &&
168
+ (internationalization.locales ?? localeList).includes(
169
+ locale.split('-')[0] as Locales
170
+ )
171
+ ) {
172
+ return locale.split('-')[0] as Locales;
173
+ }
174
+ }
175
+ }
176
+
177
+ return internationalization.defaultLocale;
178
+ };
179
+
180
+ /**
181
+ * Core language detector function
182
+ * const detectedLanguages = detectLanguage(['LanguageDetector.Cookie', 'LanguageDetector.LocalStorage'], { lookupCookie: 'myCookie' });
183
+ */
184
+ export const getBrowserLocale = (
185
+ userOptions: LanguageDetectorOptions | undefined = {}
186
+ ): Locales => {
187
+ const options = { ...getDefaultsOptions(), ...userOptions };
188
+
189
+ const locales = detectLanguage(options.order ?? [], options);
190
+
191
+ return getFirstAvailableLocale(locales, options.order ?? []);
192
+ };
@@ -5,6 +5,7 @@ export {
5
5
  IntlayerClientProvider,
6
6
  } from './IntlayerClientProvider';
7
7
  export { useIntlayer } from './useIntlayer';
8
+ export { useLocaleBase } from './useLocaleBase';
8
9
  export { useLocale } from './useLocale';
9
10
  export { useTraduction } from './useTraduction';
10
11
  export {
@@ -12,3 +13,4 @@ export {
12
13
  localeCookie,
13
14
  setLocaleCookie,
14
15
  } from './useLocaleCookie';
16
+ export { getBrowserLocale } from './getBrowserLocale';
@@ -1,5 +1,5 @@
1
1
  import type { LanguageContent } from '@intlayer/core';
2
- import { useLocale } from './useLocale';
2
+ import { useLocaleBase } from './useLocaleBase';
3
3
  import { useTraduction } from './useTraduction';
4
4
 
5
5
  /**
@@ -8,7 +8,7 @@ import { useTraduction } from './useTraduction';
8
8
  export const useContent = <Content>(
9
9
  languageContent: LanguageContent<Content>
10
10
  ) => {
11
- const { locale } = useLocale();
11
+ const { locale } = useLocaleBase();
12
12
 
13
13
  const content = useTraduction(languageContent);
14
14
 
@@ -1,22 +1,23 @@
1
1
  'use client';
2
2
 
3
- import { intlayerIntlConfiguration } from '@intlayer/config/client';
3
+ import { getConfiguration } from '@intlayer/config/client';
4
4
  import { localeList } from '@intlayer/core';
5
5
  import { useContext } from 'react';
6
6
  import { IntlayerClientContext } from './IntlayerClientProvider';
7
7
 
8
- const { defaultLocale, locales: availableLocales } = intlayerIntlConfiguration;
9
-
10
8
  /**
11
9
  * On the client side, hook to get the current locale and all related fields
12
10
  */
13
11
  export const useLocale = () => {
14
- const { locale } = useContext(IntlayerClientContext);
12
+ const { defaultLocale, locales: availableLocales } =
13
+ getConfiguration().internationalization;
14
+ const { locale, setLocale } = useContext(IntlayerClientContext);
15
15
 
16
16
  return {
17
17
  locale, // Current locale
18
18
  defaultLocale, // Principal locale defined in config
19
19
  availableLocales, // List of the available locales defined in config
20
20
  localeList, // List of all available locales
21
+ setLocale, // Function to set the locale
21
22
  };
22
23
  };
@@ -0,0 +1,24 @@
1
+ 'use client';
2
+
3
+ import { getConfiguration } from '@intlayer/config/client';
4
+ import { localeList } from '@intlayer/core';
5
+ import { useContext } from 'react';
6
+ import { IntlayerClientContext } from './IntlayerClientProvider';
7
+
8
+ const { defaultLocale, locales: availableLocales } =
9
+ getConfiguration().internationalization;
10
+
11
+ /**
12
+ * On the client side, hook to get the current locale and all related fields
13
+ */
14
+ export const useLocaleBase = () => {
15
+ const { locale, setLocale } = useContext(IntlayerClientContext);
16
+
17
+ return {
18
+ locale, // Current locale
19
+ defaultLocale, // Principal locale defined in config
20
+ availableLocales, // List of the available locales defined in config
21
+ localeList, // List of all available locales
22
+ setLocale, // Function to set the locale
23
+ };
24
+ };
@@ -1,10 +1,7 @@
1
- import {
2
- type Locales,
3
- intlayerMiddlewareConfiguration,
4
- } from '@intlayer/config/client';
1
+ import { type Locales, getConfiguration } from '@intlayer/config/client';
5
2
  import Cookies from 'js-cookie';
6
3
 
7
- const { cookieName } = intlayerMiddlewareConfiguration;
4
+ const { cookieName } = getConfiguration().middleware;
8
5
 
9
6
  const cookieAttributes: Cookies.CookieAttributes = {
10
7
  path: '/',
@@ -12,12 +12,13 @@ import { IntlayerClientContext } from './IntlayerClientProvider';
12
12
  *
13
13
  * Usage:
14
14
  *
15
- * const content = getTranslation<string>({
16
- * en: 'Hello',
17
- * fr: 'Bonjour',
18
- * },
19
- * 'fr');
15
+ * ```tsx
16
+ * const content = useTraduction<string>({
17
+ * en: 'Hello',
18
+ * fr: 'Bonjour',
19
+ * }, 'fr');
20
20
  * // 'Bonjour'
21
+ * ```
21
22
  *
22
23
  * Using TypeScript:
23
24
  * - this function will require each locale to be defined if defined in the project configuration.
@@ -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
+ };
@@ -8,22 +8,25 @@ import { contentRender } from './ContentEditor/contentRender';
8
8
  *
9
9
  * Usage:
10
10
  *
11
+ * ```ts
11
12
  * const content = getEnumeration({
12
13
  * '<=-2.3': 'You have less than -2.3',
13
14
  * '<1': 'You have less than one',
14
15
  * '2': 'You have two',
15
16
  * '>=3': 'You have three or more',
16
- * },
17
- * 2);
17
+ * }, 2);
18
18
  * // 'You have two'
19
+ * ```
19
20
  *
20
21
  * The order of the keys will define the priority of the content.
21
22
  *
23
+ * ```ts
22
24
  * const content = getEnumeration({
23
- * '<4': 'You have less than four',
24
- * '2': 'You have two',
25
- * });
25
+ * '<4': 'You have less than four',
26
+ * '2': 'You have two',
27
+ * }, 2);
26
28
  * // 'You have less than four'
29
+ * ```
27
30
  *
28
31
  */
29
32
  export const getEnumeration = <Content>(
@@ -1,7 +1,4 @@
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
 
@@ -14,12 +11,13 @@ import { contentRender } from './ContentEditor/contentRender';
14
11
  *
15
12
  * Usage:
16
13
  *
14
+ * ```ts
17
15
  * const content = getTranslation<string>({
18
16
  * en: 'Hello',
19
17
  * fr: 'Bonjour',
20
- * },
21
- * 'fr');
18
+ * }, 'fr');
22
19
  * // 'Bonjour'
20
+ * ```
23
21
  *
24
22
  * Using TypeScript:
25
23
  * - this function will require each locale to be defined if defined in the project configuration.
@@ -29,7 +27,7 @@ export const getTranslation = <Content = string>(
29
27
  languageContent: LanguageContent<Content>,
30
28
  locale?: Locales
31
29
  ): Content => {
32
- const { defaultLocale } = intlayerIntlConfiguration;
30
+ const { defaultLocale } = getConfiguration().internationalization;
33
31
 
34
32
  const result: Content = getTranslationContent<Content>(
35
33
  languageContent,
package/src/index.ts CHANGED
@@ -9,5 +9,6 @@ export {
9
9
  useLocaleCookie,
10
10
  localeCookie,
11
11
  setLocaleCookie,
12
+ getBrowserLocale,
13
+ useLocaleBase,
12
14
  } from './client/index';
13
- export {} from './client/useLocaleCookie';