react-intlayer 1.2.0 → 2.0.0
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.
- package/dist/cjs/IntlayerProvider.cjs +1 -1
- package/dist/cjs/IntlayerProvider.cjs.map +1 -1
- package/dist/cjs/cli/react-intlayer.cjs +68 -0
- package/dist/cjs/cli/react-intlayer.cjs.map +1 -0
- package/dist/cjs/cli/react-intlayer.d.ts +1 -0
- package/dist/cjs/{ContentEditor/contentRender.cjs → client/ContentEditionLayout.cjs} +9 -14
- package/dist/cjs/client/ContentEditionLayout.cjs.map +1 -0
- package/dist/cjs/client/ContentEditionLayout.d.ts +6 -0
- package/dist/cjs/client/IntlayerClientProvider.cjs +35 -2
- package/dist/cjs/client/IntlayerClientProvider.cjs.map +1 -1
- package/dist/cjs/client/IntlayerClientProvider.d.ts +2 -1
- package/dist/cjs/client/getBrowserLocale.cjs +170 -0
- package/dist/cjs/client/getBrowserLocale.cjs.map +1 -0
- package/dist/cjs/client/getBrowserLocale.d.ts +26 -0
- package/dist/cjs/client/index.cjs +6 -0
- package/dist/cjs/client/index.cjs.map +1 -1
- package/dist/cjs/client/index.d.ts +2 -0
- package/dist/cjs/client/useContent.cjs +2 -2
- package/dist/cjs/client/useContent.cjs.map +1 -1
- package/dist/cjs/client/useContent.d.ts +1 -2
- package/dist/cjs/client/useLocale.cjs +5 -3
- package/dist/cjs/client/useLocale.cjs.map +1 -1
- package/dist/cjs/client/useLocale.d.ts +1 -0
- package/dist/cjs/client/useLocaleBase.cjs +49 -0
- package/dist/cjs/client/useLocaleBase.cjs.map +1 -0
- package/dist/cjs/client/useLocaleBase.d.ts +14 -0
- package/dist/cjs/client/useLocaleCookie.cjs +1 -1
- package/dist/cjs/client/useLocaleCookie.cjs.map +1 -1
- package/dist/cjs/client/useTraduction.cjs.map +1 -1
- package/dist/cjs/client/useTraduction.d.ts +6 -5
- package/dist/cjs/craco/craco.config.cjs +32 -0
- package/dist/cjs/craco/craco.config.cjs.map +1 -0
- package/dist/cjs/craco/craco.config.d.ts +2 -0
- package/dist/cjs/craco/intlayerCracoPlugin.cjs +89 -0
- package/dist/cjs/craco/intlayerCracoPlugin.cjs.map +1 -0
- package/dist/cjs/craco/intlayerCracoPlugin.d.ts +26 -0
- package/dist/cjs/getEnumeration.cjs +0 -4
- package/dist/cjs/getEnumeration.cjs.map +1 -1
- package/dist/cjs/getEnumeration.d.ts +8 -5
- package/dist/cjs/getTranslation.cjs +1 -5
- package/dist/cjs/getTranslation.cjs.map +1 -1
- package/dist/cjs/getTranslation.d.ts +3 -2
- package/dist/cjs/index.cjs +4 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/processDictionary/{contentDictionary.d.cjs → contentDictionary.cjs} +3 -3
- package/dist/cjs/processDictionary/contentDictionary.cjs.map +1 -0
- package/dist/cjs/processDictionary/contentDictionary.d.ts +16 -0
- package/dist/cjs/processDictionary/index.cjs +118 -21
- package/dist/cjs/processDictionary/index.cjs.map +1 -1
- package/dist/cjs/processDictionary/index.d.ts +5 -4
- package/dist/cjs/server/IntlayerServerProvider.cjs +1 -1
- package/dist/cjs/server/IntlayerServerProvider.cjs.map +1 -1
- package/dist/cjs/server/getLocaleTranslation.cjs +1 -1
- package/dist/cjs/server/getLocaleTranslation.cjs.map +1 -1
- package/dist/cjs/server/useTraduction.cjs.map +1 -1
- package/dist/cjs/server/useTraduction.d.ts +4 -3
- package/dist/cjs/useIntlayerBase.cjs +3 -0
- package/dist/cjs/useIntlayerBase.cjs.map +1 -1
- package/dist/cjs/vite/intlayerPlugin.cjs +60 -0
- package/dist/cjs/vite/intlayerPlugin.cjs.map +1 -0
- package/dist/cjs/vite/intlayerPlugin.d.ts +17 -0
- package/dist/esm/IntlayerProvider.mjs +3 -2
- package/dist/esm/IntlayerProvider.mjs.map +1 -1
- package/dist/esm/chunk-ZNCO4QRF.mjs +9 -0
- package/dist/esm/cli/react-intlayer.d.mts +1 -0
- package/dist/esm/cli/react-intlayer.mjs +45 -0
- package/dist/esm/cli/react-intlayer.mjs.map +1 -0
- package/dist/esm/client/ContentEditionLayout.d.mts +6 -0
- package/dist/esm/client/ContentEditionLayout.mjs +11 -0
- package/dist/esm/client/ContentEditionLayout.mjs.map +1 -0
- package/dist/esm/client/IntlayerClientProvider.d.mts +2 -1
- package/dist/esm/client/IntlayerClientProvider.mjs +42 -7
- package/dist/esm/client/IntlayerClientProvider.mjs.map +1 -1
- package/dist/esm/client/getBrowserLocale.d.mts +26 -0
- package/dist/esm/client/getBrowserLocale.mjs +146 -0
- package/dist/esm/client/getBrowserLocale.mjs.map +1 -0
- package/dist/esm/client/index.d.mts +2 -0
- package/dist/esm/client/index.mjs +5 -0
- package/dist/esm/client/index.mjs.map +1 -1
- package/dist/esm/client/useContent.d.mts +1 -2
- package/dist/esm/client/useContent.mjs +3 -2
- package/dist/esm/client/useContent.mjs.map +1 -1
- package/dist/esm/client/useIntlayer.mjs +1 -0
- package/dist/esm/client/useIntlayer.mjs.map +1 -1
- package/dist/esm/client/useLocale.d.mts +1 -0
- package/dist/esm/client/useLocale.mjs +7 -4
- package/dist/esm/client/useLocale.mjs.map +1 -1
- package/dist/esm/client/useLocaleBase.d.mts +14 -0
- package/dist/esm/client/useLocaleBase.mjs +26 -0
- package/dist/esm/client/useLocaleBase.mjs.map +1 -0
- package/dist/esm/client/useLocaleCookie.mjs +3 -4
- package/dist/esm/client/useLocaleCookie.mjs.map +1 -1
- package/dist/esm/client/useTraduction.d.mts +6 -5
- package/dist/esm/client/useTraduction.mjs +1 -0
- package/dist/esm/client/useTraduction.mjs.map +1 -1
- package/dist/esm/craco/craco.config.d.mts +2 -0
- package/dist/esm/craco/craco.config.mjs +17 -0
- package/dist/esm/craco/craco.config.mjs.map +1 -0
- package/dist/esm/craco/intlayerCracoPlugin.d.mts +26 -0
- package/dist/esm/craco/intlayerCracoPlugin.mjs +54 -0
- package/dist/esm/craco/intlayerCracoPlugin.mjs.map +1 -0
- package/dist/esm/getEnumeration.d.mts +8 -5
- package/dist/esm/getEnumeration.mjs +1 -4
- package/dist/esm/getEnumeration.mjs.map +1 -1
- package/dist/esm/getTranslation.d.mts +3 -2
- package/dist/esm/getTranslation.mjs +3 -8
- package/dist/esm/getTranslation.mjs.map +1 -1
- package/dist/esm/index.d.mts +2 -0
- package/dist/esm/index.mjs +6 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/processDictionary/contentDictionary.d.mts +16 -0
- package/dist/esm/processDictionary/contentDictionary.mjs +1 -0
- package/dist/esm/processDictionary/contentDictionary.mjs.map +1 -0
- package/dist/esm/processDictionary/index.d.mts +5 -4
- package/dist/esm/processDictionary/index.mjs +122 -25
- package/dist/esm/processDictionary/index.mjs.map +1 -1
- package/dist/esm/server/IntlayerServerProvider.mjs +3 -4
- package/dist/esm/server/IntlayerServerProvider.mjs.map +1 -1
- package/dist/esm/server/getLocaleTranslation.mjs +3 -2
- package/dist/esm/server/getLocaleTranslation.mjs.map +1 -1
- package/dist/esm/server/index.mjs +1 -0
- package/dist/esm/server/index.mjs.map +1 -1
- package/dist/esm/server/serverContext.mjs +1 -0
- package/dist/esm/server/serverContext.mjs.map +1 -1
- package/dist/esm/server/useIntlayer.mjs +1 -0
- package/dist/esm/server/useIntlayer.mjs.map +1 -1
- package/dist/esm/server/useTraduction.d.mts +4 -3
- package/dist/esm/server/useTraduction.mjs +1 -0
- package/dist/esm/server/useTraduction.mjs.map +1 -1
- package/dist/esm/useIntlayerBase.mjs +4 -0
- package/dist/esm/useIntlayerBase.mjs.map +1 -1
- package/dist/esm/vite/intlayerPlugin.d.mts +17 -0
- package/dist/esm/vite/intlayerPlugin.mjs +36 -0
- package/dist/esm/vite/intlayerPlugin.mjs.map +1 -0
- package/package.json +37 -6
- package/src/IntlayerProvider.tsx +2 -2
- package/src/cli/react-intlayer.ts +60 -0
- package/src/client/ContentEditionLayout.tsx +15 -0
- package/src/client/IntlayerClientProvider.tsx +50 -12
- package/src/client/getBrowserLocale.tsx +192 -0
- package/src/client/index.ts +2 -0
- package/src/client/useContent.ts +2 -2
- package/src/client/useLocale.ts +5 -4
- package/src/client/useLocaleBase.ts +24 -0
- package/src/client/useLocaleCookie.ts +2 -5
- package/src/client/useTraduction.ts +6 -5
- package/src/craco/craco.config.ts +11 -0
- package/src/craco/intlayerCracoPlugin.ts +89 -0
- package/src/getEnumeration.ts +8 -10
- package/src/getTranslation.ts +5 -12
- package/src/index.ts +2 -1
- package/src/processDictionary/{contentDictionary.d.ts → contentDictionary.ts} +6 -5
- package/src/processDictionary/index.ts +159 -25
- package/src/server/IntlayerServerProvider.tsx +2 -5
- package/src/server/getLocaleTranslation.ts +2 -2
- package/src/server/useTraduction.ts +4 -3
- package/src/useIntlayerBase.ts +3 -0
- package/src/vite/intlayerPlugin.ts +59 -0
- package/dist/cjs/ContentEditor/ContentEditor.cjs +0 -110
- package/dist/cjs/ContentEditor/ContentEditor.cjs.map +0 -1
- package/dist/cjs/ContentEditor/ContentEditor.d.ts +0 -8
- package/dist/cjs/ContentEditor/contentRender.cjs.map +0 -1
- package/dist/cjs/ContentEditor/contentRender.d.ts +0 -3
- package/dist/cjs/ContentEditor/index.cjs +0 -25
- package/dist/cjs/ContentEditor/index.cjs.map +0 -1
- package/dist/cjs/ContentEditor/index.d.ts +0 -3
- package/dist/cjs/processDictionary/contentDictionary.d.cjs.map +0 -1
- package/dist/cjs/processDictionary/contentDictionary.d.d.ts +0 -29
- package/dist/esm/ContentEditor/ContentEditor.d.mts +0 -8
- package/dist/esm/ContentEditor/ContentEditor.mjs +0 -91
- package/dist/esm/ContentEditor/ContentEditor.mjs.map +0 -1
- package/dist/esm/ContentEditor/contentRender.d.mts +0 -3
- package/dist/esm/ContentEditor/contentRender.mjs +0 -13
- package/dist/esm/ContentEditor/contentRender.mjs.map +0 -1
- package/dist/esm/ContentEditor/index.d.mts +0 -3
- package/dist/esm/ContentEditor/index.mjs +0 -3
- package/dist/esm/ContentEditor/index.mjs.map +0 -1
- package/dist/esm/processDictionary/contentDictionary.d.d.mts +0 -29
- package/dist/esm/processDictionary/contentDictionary.d.mjs +0 -1
- package/src/ContentEditor/ContentEditor.tsx +0 -116
- package/src/ContentEditor/contentRender.tsx +0 -11
- package/src/ContentEditor/index.tsx +0 -2
- /package/dist/esm/{processDictionary/contentDictionary.d.mjs.map → chunk-ZNCO4QRF.mjs.map} +0 -0
|
@@ -1,26 +1,29 @@
|
|
|
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 {
|
|
13
|
+
import { ContentEditionLayout } from './ContentEditionLayout';
|
|
14
|
+
import { localeCookie, setLocaleCookie } from './useLocaleCookie';
|
|
14
15
|
|
|
15
16
|
type IntlayerValue = {
|
|
16
17
|
locale: Locales;
|
|
18
|
+
setLocale: (newLocale: Locales) => void;
|
|
17
19
|
};
|
|
18
20
|
|
|
19
21
|
/**
|
|
20
22
|
* Context that store the current locale on the client side
|
|
21
23
|
*/
|
|
22
24
|
export const IntlayerClientContext = createContext<IntlayerValue>({
|
|
23
|
-
locale: localeCookie ??
|
|
25
|
+
locale: localeCookie ?? getConfiguration().internationalization.defaultLocale,
|
|
26
|
+
setLocale: () => null,
|
|
24
27
|
});
|
|
25
28
|
|
|
26
29
|
/**
|
|
@@ -29,7 +32,7 @@ export const IntlayerClientContext = createContext<IntlayerValue>({
|
|
|
29
32
|
export const useIntlayerContext = () => useContext(IntlayerClientContext);
|
|
30
33
|
|
|
31
34
|
export type IntlayerClientProviderProps = PropsWithChildren & {
|
|
32
|
-
locale
|
|
35
|
+
locale?: Locales;
|
|
33
36
|
};
|
|
34
37
|
|
|
35
38
|
/**
|
|
@@ -38,8 +41,43 @@ export type IntlayerClientProviderProps = PropsWithChildren & {
|
|
|
38
41
|
export const IntlayerClientProvider: FC<IntlayerClientProviderProps> = ({
|
|
39
42
|
locale,
|
|
40
43
|
children,
|
|
41
|
-
}) =>
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
+
};
|
|
@@ -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
|
+
};
|
package/src/client/index.ts
CHANGED
|
@@ -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';
|
package/src/client/useContent.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { LanguageContent } from '@intlayer/core';
|
|
2
|
-
import {
|
|
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 } =
|
|
11
|
+
const { locale } = useLocaleBase();
|
|
12
12
|
|
|
13
13
|
const content = useTraduction(languageContent);
|
|
14
14
|
|
package/src/client/useLocale.ts
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import {
|
|
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 {
|
|
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 } =
|
|
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
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
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,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
|
+
};
|
package/src/getEnumeration.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { type QuantityContent, getEnumerationContent } from '@intlayer/core';
|
|
2
|
-
import { contentRender } from './ContentEditor/contentRender';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Allow to pick a content based on a quantity.
|
|
@@ -8,22 +7,25 @@ import { contentRender } from './ContentEditor/contentRender';
|
|
|
8
7
|
*
|
|
9
8
|
* Usage:
|
|
10
9
|
*
|
|
10
|
+
* ```ts
|
|
11
11
|
* const content = getEnumeration({
|
|
12
12
|
* '<=-2.3': 'You have less than -2.3',
|
|
13
13
|
* '<1': 'You have less than one',
|
|
14
14
|
* '2': 'You have two',
|
|
15
15
|
* '>=3': 'You have three or more',
|
|
16
|
-
* },
|
|
17
|
-
* 2);
|
|
16
|
+
* }, 2);
|
|
18
17
|
* // 'You have two'
|
|
18
|
+
* ```
|
|
19
19
|
*
|
|
20
20
|
* The order of the keys will define the priority of the content.
|
|
21
21
|
*
|
|
22
|
+
* ```ts
|
|
22
23
|
* const content = getEnumeration({
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* });
|
|
24
|
+
* '<4': 'You have less than four',
|
|
25
|
+
* '2': 'You have two',
|
|
26
|
+
* }, 2);
|
|
26
27
|
* // 'You have less than four'
|
|
28
|
+
* ```
|
|
27
29
|
*
|
|
28
30
|
*/
|
|
29
31
|
export const getEnumeration = <Content>(
|
|
@@ -35,9 +37,5 @@ export const getEnumeration = <Content>(
|
|
|
35
37
|
quantity
|
|
36
38
|
);
|
|
37
39
|
|
|
38
|
-
if (typeof result === 'string') {
|
|
39
|
-
return contentRender(result) as Content;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
40
|
return result;
|
|
43
41
|
};
|
package/src/getTranslation.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
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
|
-
import { contentRender } from './ContentEditor/contentRender';
|
|
7
3
|
|
|
8
4
|
/**
|
|
9
5
|
*
|
|
@@ -14,12 +10,13 @@ import { contentRender } from './ContentEditor/contentRender';
|
|
|
14
10
|
*
|
|
15
11
|
* Usage:
|
|
16
12
|
*
|
|
13
|
+
* ```ts
|
|
17
14
|
* const content = getTranslation<string>({
|
|
18
15
|
* en: 'Hello',
|
|
19
16
|
* fr: 'Bonjour',
|
|
20
|
-
* },
|
|
21
|
-
* 'fr');
|
|
17
|
+
* }, 'fr');
|
|
22
18
|
* // 'Bonjour'
|
|
19
|
+
* ```
|
|
23
20
|
*
|
|
24
21
|
* Using TypeScript:
|
|
25
22
|
* - this function will require each locale to be defined if defined in the project configuration.
|
|
@@ -29,16 +26,12 @@ export const getTranslation = <Content = string>(
|
|
|
29
26
|
languageContent: LanguageContent<Content>,
|
|
30
27
|
locale?: Locales
|
|
31
28
|
): Content => {
|
|
32
|
-
const { defaultLocale } =
|
|
29
|
+
const { defaultLocale } = getConfiguration().internationalization;
|
|
33
30
|
|
|
34
31
|
const result: Content = getTranslationContent<Content>(
|
|
35
32
|
languageContent,
|
|
36
33
|
locale ?? defaultLocale
|
|
37
34
|
);
|
|
38
35
|
|
|
39
|
-
if (typeof result === 'string') {
|
|
40
|
-
return contentRender(result) as Content;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
36
|
return result;
|
|
44
37
|
};
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { TranslationContent } from '@intlayer/core';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
2
3
|
|
|
3
4
|
export type ContentValue =
|
|
4
5
|
| string
|
|
@@ -11,16 +12,16 @@ export type Content = Record<string, ContentValue | undefined>;
|
|
|
11
12
|
|
|
12
13
|
export type TransformedContentValue =
|
|
13
14
|
| string
|
|
15
|
+
| ReactNode
|
|
14
16
|
| {
|
|
15
17
|
[key: string]: TransformedContentValue;
|
|
16
18
|
}
|
|
17
19
|
| undefined
|
|
18
20
|
| ((quantity: number) => TransformedContentValue);
|
|
19
21
|
|
|
20
|
-
export type TransformedContent =
|
|
21
|
-
string,
|
|
22
|
-
|
|
23
|
-
>;
|
|
22
|
+
export type TransformedContent =
|
|
23
|
+
| Record<string, TransformedContentValue | undefined>
|
|
24
|
+
| ReactNode;
|
|
24
25
|
|
|
25
26
|
export type ContentDictionary = Content & {
|
|
26
27
|
id: string;
|