react-intlayer 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/client/ContentEditionLayout.cjs +6 -2
- package/dist/cjs/client/ContentEditionLayout.cjs.map +1 -1
- package/dist/cjs/client/{IntlayerClientProvider.cjs → IntlayerProvider.cjs} +11 -9
- package/dist/cjs/client/IntlayerProvider.cjs.map +1 -0
- package/dist/cjs/client/{IntlayerClientProvider.d.ts → IntlayerProvider.d.ts} +4 -3
- package/dist/cjs/client/index.cjs +6 -6
- package/dist/cjs/client/index.cjs.map +1 -1
- package/dist/cjs/client/index.d.ts +2 -1
- package/dist/cjs/client/useIntlayer.cjs +2 -2
- package/dist/cjs/client/useIntlayer.cjs.map +1 -1
- package/dist/cjs/client/useIntlayer.d.ts +2 -1
- package/dist/cjs/client/useLocale.cjs +2 -2
- package/dist/cjs/client/useLocale.cjs.map +1 -1
- package/dist/cjs/client/useLocaleBase.cjs +2 -2
- package/dist/cjs/client/useLocaleBase.cjs.map +1 -1
- package/dist/cjs/client/useTraduction.cjs +2 -2
- package/dist/cjs/client/useTraduction.cjs.map +1 -1
- package/dist/cjs/index.cjs +7 -4
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +3 -2
- package/dist/cjs/processDictionary/contentDictionary.cjs.map +1 -1
- package/dist/cjs/processDictionary/contentDictionary.d.ts +9 -9
- package/dist/cjs/processDictionary/index.cjs +67 -51
- package/dist/cjs/processDictionary/index.cjs.map +1 -1
- package/dist/cjs/processDictionary/index.d.ts +4 -4
- package/dist/cjs/server/IntlayerServerProvider.cjs +3 -4
- package/dist/cjs/server/IntlayerServerProvider.cjs.map +1 -1
- package/dist/cjs/server/IntlayerServerProvider.d.ts +1 -1
- package/dist/cjs/server/index.d.ts +1 -0
- package/dist/cjs/server/useIntlayer.d.ts +2 -1
- package/dist/cjs/useIntlayerBase.cjs +45 -1
- package/dist/cjs/useIntlayerBase.cjs.map +1 -1
- package/dist/cjs/useIntlayerBase.d.ts +51 -4
- package/dist/esm/client/ContentEditionLayout.mjs +5 -1
- package/dist/esm/client/ContentEditionLayout.mjs.map +1 -1
- package/dist/esm/client/{IntlayerClientProvider.d.mts → IntlayerProvider.d.mts} +4 -3
- package/dist/esm/client/{IntlayerClientProvider.mjs → IntlayerProvider.mjs} +7 -5
- package/dist/esm/client/IntlayerProvider.mjs.map +1 -0
- package/dist/esm/client/index.d.mts +2 -1
- package/dist/esm/client/index.mjs +4 -4
- package/dist/esm/client/index.mjs.map +1 -1
- package/dist/esm/client/useIntlayer.d.mts +2 -1
- package/dist/esm/client/useIntlayer.mjs +1 -1
- package/dist/esm/client/useIntlayer.mjs.map +1 -1
- package/dist/esm/client/useLocale.mjs +1 -1
- package/dist/esm/client/useLocale.mjs.map +1 -1
- package/dist/esm/client/useLocaleBase.mjs +1 -1
- package/dist/esm/client/useLocaleBase.mjs.map +1 -1
- package/dist/esm/client/useTraduction.mjs +1 -1
- package/dist/esm/client/useTraduction.mjs.map +1 -1
- package/dist/esm/index.d.mts +3 -2
- package/dist/esm/index.mjs +6 -4
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/processDictionary/contentDictionary.d.mts +9 -9
- package/dist/esm/processDictionary/index.d.mts +4 -4
- package/dist/esm/processDictionary/index.mjs +67 -51
- package/dist/esm/processDictionary/index.mjs.map +1 -1
- package/dist/esm/server/IntlayerServerProvider.d.mts +1 -1
- package/dist/esm/server/IntlayerServerProvider.mjs +3 -4
- package/dist/esm/server/IntlayerServerProvider.mjs.map +1 -1
- package/dist/esm/server/index.d.mts +1 -0
- package/dist/esm/server/useIntlayer.d.mts +2 -1
- package/dist/esm/useIntlayerBase.d.mts +51 -4
- package/dist/esm/useIntlayerBase.mjs +43 -1
- package/dist/esm/useIntlayerBase.mjs.map +1 -1
- package/package.json +8 -9
- package/src/index.ts +4 -3
- package/dist/cjs/IntlayerProvider.cjs +0 -37
- package/dist/cjs/IntlayerProvider.cjs.map +0 -1
- package/dist/cjs/IntlayerProvider.d.ts +0 -9
- package/dist/cjs/client/IntlayerClientProvider.cjs.map +0 -1
- package/dist/esm/IntlayerProvider.d.mts +0 -9
- package/dist/esm/IntlayerProvider.mjs +0 -18
- package/dist/esm/IntlayerProvider.mjs.map +0 -1
- package/dist/esm/client/IntlayerClientProvider.mjs.map +0 -1
- package/src/IntlayerProvider.tsx +0 -24
- package/src/cli/react-intlayer.ts +0 -60
- package/src/client/ContentEditionLayout.tsx +0 -15
- package/src/client/IntlayerClientProvider.tsx +0 -83
- package/src/client/getBrowserLocale.tsx +0 -192
- package/src/client/index.ts +0 -16
- package/src/client/useContent.ts +0 -20
- package/src/client/useIntlayer.ts +0 -25
- package/src/client/useLocale.ts +0 -23
- package/src/client/useLocaleBase.ts +0 -24
- package/src/client/useLocaleCookie.ts +0 -32
- package/src/client/useTraduction.ts +0 -33
- package/src/craco/craco.config.ts +0 -11
- package/src/craco/intlayerCracoPlugin.ts +0 -89
- package/src/getEnumeration.ts +0 -41
- package/src/getTranslation.ts +0 -37
- package/src/processDictionary/contentDictionary.ts +0 -28
- package/src/processDictionary/index.ts +0 -231
- package/src/server/IntlayerServerProvider.tsx +0 -36
- package/src/server/getLocaleTranslation.ts +0 -22
- package/src/server/index.ts +0 -8
- package/src/server/serverContext.ts +0 -85
- package/src/server/useIntlayer.ts +0 -23
- package/src/server/useTraduction.ts +0 -34
- package/src/useIntlayerBase.ts +0 -38
- package/src/vite/intlayerPlugin.ts +0 -59
|
@@ -1,60 +0,0 @@
|
|
|
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,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
|
-
};
|
package/src/client/index.ts
DELETED
|
@@ -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';
|
package/src/client/useContent.ts
DELETED
|
@@ -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
|
-
};
|
package/src/client/useLocale.ts
DELETED
|
@@ -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,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
|
-
};
|
package/src/getEnumeration.ts
DELETED
|
@@ -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
|
-
};
|