react-intlayer 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/dist/cjs/client/ContentEditionLayout.cjs.map +1 -1
  2. package/dist/cjs/client/{IntlayerClientProvider.cjs → IntlayerProvider.cjs} +14 -11
  3. package/dist/cjs/client/IntlayerProvider.cjs.map +1 -0
  4. package/dist/cjs/client/{IntlayerClientProvider.d.ts → IntlayerProvider.d.ts} +5 -3
  5. package/dist/cjs/client/getBrowserLocale.cjs +7 -14
  6. package/dist/cjs/client/getBrowserLocale.cjs.map +1 -1
  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 +3 -2
  10. package/dist/cjs/client/useContent.d.ts +1 -1
  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 +3 -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/useLocaleCookie.cjs.map +1 -1
  19. package/dist/cjs/client/useTraduction.cjs +2 -2
  20. package/dist/cjs/client/useTraduction.cjs.map +1 -1
  21. package/dist/cjs/index.cjs +4 -4
  22. package/dist/cjs/index.cjs.map +1 -1
  23. package/dist/cjs/index.d.ts +3 -2
  24. package/dist/cjs/processDictionary/contentDictionary.cjs.map +1 -1
  25. package/dist/cjs/processDictionary/contentDictionary.d.ts +9 -9
  26. package/dist/cjs/processDictionary/index.cjs +69 -53
  27. package/dist/cjs/processDictionary/index.cjs.map +1 -1
  28. package/dist/cjs/processDictionary/index.d.ts +4 -4
  29. package/dist/cjs/server/IntlayerServerProvider.cjs +3 -4
  30. package/dist/cjs/server/IntlayerServerProvider.cjs.map +1 -1
  31. package/dist/cjs/server/IntlayerServerProvider.d.ts +6 -7
  32. package/dist/cjs/server/index.d.ts +1 -0
  33. package/dist/cjs/server/serverContext.cjs +1 -2
  34. package/dist/cjs/server/serverContext.cjs.map +1 -1
  35. package/dist/cjs/server/useIntlayer.d.ts +3 -1
  36. package/dist/cjs/useIntlayerBase.cjs +26 -1
  37. package/dist/cjs/useIntlayerBase.cjs.map +1 -1
  38. package/dist/cjs/useIntlayerBase.d.ts +62 -4
  39. package/dist/esm/{chunk-ZNCO4QRF.mjs → chunk-ZD7AOCMD.mjs} +1 -1
  40. package/dist/esm/client/ContentEditionLayout.mjs +1 -1
  41. package/dist/esm/client/ContentEditionLayout.mjs.map +1 -1
  42. package/dist/esm/client/{IntlayerClientProvider.d.mts → IntlayerProvider.d.mts} +5 -3
  43. package/dist/esm/client/{IntlayerClientProvider.mjs → IntlayerProvider.mjs} +11 -8
  44. package/dist/esm/client/IntlayerProvider.mjs.map +1 -0
  45. package/dist/esm/client/getBrowserLocale.mjs +8 -15
  46. package/dist/esm/client/getBrowserLocale.mjs.map +1 -1
  47. package/dist/esm/client/index.d.mts +3 -2
  48. package/dist/esm/client/index.mjs +5 -5
  49. package/dist/esm/client/index.mjs.map +1 -1
  50. package/dist/esm/client/useContent.d.mts +1 -1
  51. package/dist/esm/client/useContent.mjs +1 -1
  52. package/dist/esm/client/useIntlayer.d.mts +3 -1
  53. package/dist/esm/client/useIntlayer.mjs +2 -2
  54. package/dist/esm/client/useIntlayer.mjs.map +1 -1
  55. package/dist/esm/client/useLocale.mjs +2 -2
  56. package/dist/esm/client/useLocale.mjs.map +1 -1
  57. package/dist/esm/client/useLocaleBase.mjs +2 -2
  58. package/dist/esm/client/useLocaleBase.mjs.map +1 -1
  59. package/dist/esm/client/useLocaleCookie.mjs +1 -1
  60. package/dist/esm/client/useLocaleCookie.mjs.map +1 -1
  61. package/dist/esm/client/useTraduction.mjs +2 -2
  62. package/dist/esm/client/useTraduction.mjs.map +1 -1
  63. package/dist/esm/craco/craco.config.mjs +1 -1
  64. package/dist/esm/craco/intlayerCracoPlugin.mjs +1 -1
  65. package/dist/esm/getEnumeration.mjs +1 -1
  66. package/dist/esm/getTranslation.mjs +1 -1
  67. package/dist/esm/index.d.mts +3 -2
  68. package/dist/esm/index.mjs +5 -5
  69. package/dist/esm/index.mjs.map +1 -1
  70. package/dist/esm/processDictionary/contentDictionary.d.mts +9 -9
  71. package/dist/esm/processDictionary/index.d.mts +4 -4
  72. package/dist/esm/processDictionary/index.mjs +70 -54
  73. package/dist/esm/processDictionary/index.mjs.map +1 -1
  74. package/dist/esm/server/IntlayerServerProvider.d.mts +6 -7
  75. package/dist/esm/server/IntlayerServerProvider.mjs +4 -5
  76. package/dist/esm/server/IntlayerServerProvider.mjs.map +1 -1
  77. package/dist/esm/server/getLocaleTranslation.mjs +1 -1
  78. package/dist/esm/server/index.d.mts +1 -0
  79. package/dist/esm/server/index.mjs +1 -1
  80. package/dist/esm/server/serverContext.mjs +2 -3
  81. package/dist/esm/server/serverContext.mjs.map +1 -1
  82. package/dist/esm/server/useIntlayer.d.mts +3 -1
  83. package/dist/esm/server/useIntlayer.mjs +1 -1
  84. package/dist/esm/server/useTraduction.mjs +1 -1
  85. package/dist/esm/useIntlayerBase.d.mts +62 -4
  86. package/dist/esm/useIntlayerBase.mjs +26 -2
  87. package/dist/esm/useIntlayerBase.mjs.map +1 -1
  88. package/dist/esm/vite/intlayerPlugin.mjs +1 -1
  89. package/package.json +18 -19
  90. package/src/index.ts +4 -3
  91. package/dist/cjs/IntlayerProvider.cjs +0 -37
  92. package/dist/cjs/IntlayerProvider.cjs.map +0 -1
  93. package/dist/cjs/IntlayerProvider.d.ts +0 -9
  94. package/dist/cjs/client/IntlayerClientProvider.cjs.map +0 -1
  95. package/dist/esm/IntlayerProvider.d.mts +0 -9
  96. package/dist/esm/IntlayerProvider.mjs +0 -18
  97. package/dist/esm/IntlayerProvider.mjs.map +0 -1
  98. package/dist/esm/client/IntlayerClientProvider.mjs.map +0 -1
  99. package/src/IntlayerProvider.tsx +0 -24
  100. package/src/cli/react-intlayer.ts +0 -60
  101. package/src/client/ContentEditionLayout.tsx +0 -15
  102. package/src/client/IntlayerClientProvider.tsx +0 -83
  103. package/src/client/getBrowserLocale.tsx +0 -192
  104. package/src/client/index.ts +0 -16
  105. package/src/client/useContent.ts +0 -20
  106. package/src/client/useIntlayer.ts +0 -25
  107. package/src/client/useLocale.ts +0 -23
  108. package/src/client/useLocaleBase.ts +0 -24
  109. package/src/client/useLocaleCookie.ts +0 -32
  110. package/src/client/useTraduction.ts +0 -33
  111. package/src/craco/craco.config.ts +0 -11
  112. package/src/craco/intlayerCracoPlugin.ts +0 -89
  113. package/src/getEnumeration.ts +0 -41
  114. package/src/getTranslation.ts +0 -37
  115. package/src/processDictionary/contentDictionary.ts +0 -28
  116. package/src/processDictionary/index.ts +0 -231
  117. package/src/server/IntlayerServerProvider.tsx +0 -36
  118. package/src/server/getLocaleTranslation.ts +0 -22
  119. package/src/server/index.ts +0 -8
  120. package/src/server/serverContext.ts +0 -85
  121. package/src/server/useIntlayer.ts +0 -23
  122. package/src/server/useTraduction.ts +0 -34
  123. package/src/useIntlayerBase.ts +0 -38
  124. package/src/vite/intlayerPlugin.ts +0 -59
  125. /package/dist/esm/{chunk-ZNCO4QRF.mjs.map → chunk-ZD7AOCMD.mjs.map} +0 -0
@@ -1,15 +0,0 @@
1
- import {
2
- ContentEditionLayout as ContentEditionLayoutBase,
3
- type ContentEditionLayoutProps,
4
- } from 'intlayer-editor/client';
5
- import type { FC } from 'react';
6
-
7
- const BlankLayout: FC<ContentEditionLayoutProps> = ({ children }) => (
8
- <>{children}</>
9
- );
10
-
11
- // intlayer-editor is an optional dependency. If it's not installed, return the blank layout
12
- export const ContentEditionLayout: FC<ContentEditionLayoutProps> =
13
- typeof ContentEditionLayoutBase === 'undefined'
14
- ? BlankLayout
15
- : (ContentEditionLayoutBase as typeof ContentEditionLayout);
@@ -1,83 +0,0 @@
1
- 'use client';
2
-
3
- import { getConfiguration, type Locales } from '@intlayer/config/client';
4
- import {
5
- type PropsWithChildren,
6
- createContext,
7
- useContext,
8
- useMemo,
9
- type FC,
10
- useState,
11
- useCallback,
12
- } from 'react';
13
- import { ContentEditionLayout } from './ContentEditionLayout';
14
- import { localeCookie, setLocaleCookie } from './useLocaleCookie';
15
-
16
- type IntlayerValue = {
17
- locale: Locales;
18
- setLocale: (newLocale: Locales) => void;
19
- };
20
-
21
- /**
22
- * Context that store the current locale on the client side
23
- */
24
- export const IntlayerClientContext = createContext<IntlayerValue>({
25
- locale: localeCookie ?? getConfiguration().internationalization.defaultLocale,
26
- setLocale: () => null,
27
- });
28
-
29
- /**
30
- * Hook that provides the current locale
31
- */
32
- export const useIntlayerContext = () => useContext(IntlayerClientContext);
33
-
34
- export type IntlayerClientProviderProps = PropsWithChildren & {
35
- locale?: Locales;
36
- };
37
-
38
- /**
39
- * Provider that store the current locale on the client side
40
- */
41
- export const IntlayerClientProvider: FC<IntlayerClientProviderProps> = ({
42
- locale,
43
- children,
44
- }) => {
45
- const { defaultLocale, locales: availableLocales } =
46
- getConfiguration().internationalization;
47
-
48
- const [currentLocale, setCurrentLocale] = useState(
49
- locale ?? localeCookie ?? defaultLocale
50
- );
51
-
52
- const setLocale = useCallback(
53
- (newLocale: Locales) => {
54
- if (currentLocale.toString() === newLocale.toString()) return;
55
-
56
- if (!availableLocales.includes(newLocale)) {
57
- console.error(`Locale ${locale} is not available`);
58
- return;
59
- }
60
-
61
- setCurrentLocale(newLocale); // Update state
62
- setLocaleCookie(newLocale); // Optionally set cookie for persistence
63
- },
64
- [availableLocales, currentLocale, locale]
65
- );
66
-
67
- const value: IntlayerValue = useMemo<IntlayerValue>(
68
- () => ({ locale: currentLocale, setLocale }),
69
- [currentLocale, setLocale]
70
- );
71
-
72
- return (
73
- <IntlayerClientContext.Provider value={value}>
74
- <ContentEditionLayout
75
- locale={currentLocale}
76
- setLocale={setLocale}
77
- localeList={availableLocales}
78
- >
79
- {children}
80
- </ContentEditionLayout>
81
- </IntlayerClientContext.Provider>
82
- );
83
- };
@@ -1,192 +0,0 @@
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
- };
@@ -1,16 +0,0 @@
1
- export type { IntlayerClientProviderProps } from './IntlayerClientProvider';
2
- export {
3
- IntlayerClientContext as IntlayerClient,
4
- useIntlayerContext,
5
- IntlayerClientProvider,
6
- } from './IntlayerClientProvider';
7
- export { useIntlayer } from './useIntlayer';
8
- export { useLocaleBase } from './useLocaleBase';
9
- export { useLocale } from './useLocale';
10
- export { useTraduction } from './useTraduction';
11
- export {
12
- useLocaleCookie,
13
- localeCookie,
14
- setLocaleCookie,
15
- } from './useLocaleCookie';
16
- export { getBrowserLocale } from './getBrowserLocale';
@@ -1,20 +0,0 @@
1
- import type { LanguageContent } from '@intlayer/core';
2
- import { useLocaleBase } from './useLocaleBase';
3
- import { useTraduction } from './useTraduction';
4
-
5
- /**
6
- * On the client side, hook to get the translation content based on the locale
7
- */
8
- export const useContent = <Content>(
9
- languageContent: LanguageContent<Content>
10
- ) => {
11
- const { locale } = useLocaleBase();
12
-
13
- const content = useTraduction(languageContent);
14
-
15
- return {
16
- locale,
17
- content,
18
- t: useTraduction,
19
- };
20
- };
@@ -1,25 +0,0 @@
1
- 'use client';
2
-
3
- import type { Locales } from '@intlayer/config/client';
4
- import { useContext } from 'react';
5
- import {
6
- type DictionaryKeys,
7
- useIntlayerBase,
8
- type UseIntlayer,
9
- } from '../useIntlayerBase';
10
- import { IntlayerClientContext } from './IntlayerClientProvider';
11
-
12
- /**
13
- * On the client side, Hook that picking one dictionary by its id and return the content
14
- *
15
- * If the locale is not provided, it will use the locale from the client context
16
- */
17
- export const useIntlayer: UseIntlayer = <T extends DictionaryKeys>(
18
- id: T,
19
- locale?: Locales
20
- ) => {
21
- const { locale: currentLocale } = useContext(IntlayerClientContext);
22
- const localeTarget = locale ?? currentLocale;
23
-
24
- return useIntlayerBase(id, localeTarget);
25
- };
@@ -1,23 +0,0 @@
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
- /**
9
- * On the client side, hook to get the current locale and all related fields
10
- */
11
- export const useLocale = () => {
12
- const { defaultLocale, locales: availableLocales } =
13
- getConfiguration().internationalization;
14
- const { locale, setLocale } = useContext(IntlayerClientContext);
15
-
16
- return {
17
- locale, // Current locale
18
- defaultLocale, // Principal locale defined in config
19
- availableLocales, // List of the available locales defined in config
20
- localeList, // List of all available locales
21
- setLocale, // Function to set the locale
22
- };
23
- };
@@ -1,24 +0,0 @@
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,32 +0,0 @@
1
- import { type Locales, getConfiguration } from '@intlayer/config/client';
2
- import Cookies from 'js-cookie';
3
-
4
- const { cookieName } = getConfiguration().middleware;
5
-
6
- const cookieAttributes: Cookies.CookieAttributes = {
7
- path: '/',
8
- expires: undefined,
9
- domain: undefined,
10
- secure: false,
11
- sameSite: 'strict',
12
- };
13
-
14
- /**
15
- * Get the locale cookie
16
- */
17
- export const localeCookie = Cookies.get(cookieName) as Locales | undefined;
18
-
19
- /**
20
- * Set the locale cookie
21
- */
22
- export const setLocaleCookie = (locale: Locales) => {
23
- Cookies.set(cookieName, locale, cookieAttributes);
24
- };
25
-
26
- /**
27
- * Hook that provides the locale cookie and a function to set it
28
- */
29
- export const useLocaleCookie = () => ({
30
- localeCookie,
31
- setLocaleCookie,
32
- });
@@ -1,33 +0,0 @@
1
- import type { LanguageContent } from '@intlayer/core';
2
- import { useContext } from 'react';
3
- import { getTranslation } from '../getTranslation';
4
- import { IntlayerClientContext } from './IntlayerClientProvider';
5
-
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>(
28
- languageContent: LanguageContent<Content>
29
- ): Content => {
30
- const { locale } = useContext(IntlayerClientContext);
31
-
32
- return getTranslation(languageContent, locale);
33
- };
@@ -1,11 +0,0 @@
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;
@@ -1,89 +0,0 @@
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,41 +0,0 @@
1
- import { type QuantityContent, getEnumerationContent } from '@intlayer/core';
2
-
3
- /**
4
- * Allow to pick a content based on a quantity.
5
- *
6
- * Return either the content editor, or the content itself depending on the configuration.
7
- *
8
- * Usage:
9
- *
10
- * ```ts
11
- * const content = getEnumeration({
12
- * '<=-2.3': 'You have less than -2.3',
13
- * '<1': 'You have less than one',
14
- * '2': 'You have two',
15
- * '>=3': 'You have three or more',
16
- * }, 2);
17
- * // 'You have two'
18
- * ```
19
- *
20
- * The order of the keys will define the priority of the content.
21
- *
22
- * ```ts
23
- * const content = getEnumeration({
24
- * '<4': 'You have less than four',
25
- * '2': 'You have two',
26
- * }, 2);
27
- * // 'You have less than four'
28
- * ```
29
- *
30
- */
31
- export const getEnumeration = <Content>(
32
- enumerationContent: QuantityContent<Content>,
33
- quantity: number
34
- ): Content => {
35
- const result: Content = getEnumerationContent<Content>(
36
- enumerationContent,
37
- quantity
38
- );
39
-
40
- return result;
41
- };
@@ -1,37 +0,0 @@
1
- import { type Locales, getConfiguration } from '@intlayer/config/client';
2
- import { type LanguageContent, getTranslationContent } from '@intlayer/core';
3
-
4
- /**
5
- *
6
- * Allow to pick a content based on a locale.
7
- * If not locale found, it will return the content related to the default locale.
8
- *
9
- * Return either the content editor, or the content itself depending on the configuration.
10
- *
11
- * Usage:
12
- *
13
- * ```ts
14
- * const content = getTranslation<string>({
15
- * en: 'Hello',
16
- * fr: 'Bonjour',
17
- * }, 'fr');
18
- * // 'Bonjour'
19
- * ```
20
- *
21
- * Using TypeScript:
22
- * - this function will require each locale to be defined if defined in the project configuration.
23
- * - If a locale is missing, it will make each existing locale optional and raise an error if the locale is not found.
24
- */
25
- export const getTranslation = <Content = string>(
26
- languageContent: LanguageContent<Content>,
27
- locale?: Locales
28
- ): Content => {
29
- const { defaultLocale } = getConfiguration().internationalization;
30
-
31
- const result: Content = getTranslationContent<Content>(
32
- languageContent,
33
- locale ?? defaultLocale
34
- );
35
-
36
- return result;
37
- };
@@ -1,28 +0,0 @@
1
- import type { TranslationContent } from '@intlayer/core';
2
- import type { ReactNode } from 'react';
3
-
4
- export type ContentValue =
5
- | string
6
- | {
7
- [key: string]: ContentValue;
8
- }
9
- | TranslationContent<unknown>;
10
-
11
- export type Content = Record<string, ContentValue | undefined>;
12
-
13
- export type TransformedContentValue =
14
- | string
15
- | ReactNode
16
- | {
17
- [key: string]: TransformedContentValue;
18
- }
19
- | undefined
20
- | ((quantity: number) => TransformedContentValue);
21
-
22
- export type TransformedContent =
23
- | Record<string, TransformedContentValue | undefined>
24
- | ReactNode;
25
-
26
- export type ContentDictionary = Content & {
27
- id: string;
28
- };