react-intlayer 1.0.1 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/{LocaleContextProvider.cjs → IntlayerProvider.cjs} +11 -11
- package/dist/cjs/IntlayerProvider.cjs.map +1 -0
- package/dist/cjs/IntlayerProvider.d.ts +9 -0
- 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/client/IntlayerClientProvider.cjs +69 -0
- package/dist/cjs/client/IntlayerClientProvider.cjs.map +1 -0
- package/dist/cjs/client/IntlayerClientProvider.d.ts +25 -0
- 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 +13 -7
- package/dist/cjs/client/index.cjs.map +1 -1
- package/dist/cjs/client/index.d.ts +3 -1
- package/dist/cjs/client/useContent.cjs +2 -2
- package/dist/cjs/client/useContent.cjs.map +1 -1
- package/dist/cjs/client/useContent.d.ts +4 -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 +5 -0
- package/dist/cjs/client/useLocale.cjs +6 -4
- package/dist/cjs/client/useLocale.cjs.map +1 -1
- package/dist/cjs/client/useLocale.d.ts +4 -0
- package/dist/cjs/client/{LocaleClientContextProvider.cjs → useLocaleBase.cjs} +24 -17
- 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/useLocaleCookie.d.ts +9 -0
- package/dist/cjs/client/useTraduction.cjs +2 -2
- package/dist/cjs/client/useTraduction.cjs.map +1 -1
- package/dist/cjs/client/useTraduction.d.ts +22 -1
- 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.map +1 -1
- package/dist/cjs/getEnumeration.d.ts +28 -0
- package/dist/cjs/getTranslation.cjs +1 -1
- package/dist/cjs/getTranslation.cjs.map +1 -1
- package/dist/cjs/getTranslation.d.ts +22 -1
- package/dist/cjs/index.cjs +8 -6
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +3 -1
- package/dist/cjs/processDictionary/index.cjs +33 -1
- package/dist/cjs/processDictionary/index.cjs.map +1 -1
- package/dist/cjs/processDictionary/index.d.ts +1 -1
- package/dist/cjs/server/{LocaleServerContextProvider.cjs → IntlayerServerProvider.cjs} +20 -14
- package/dist/cjs/server/IntlayerServerProvider.cjs.map +1 -0
- package/dist/{esm/server/LocaleServerContextProvider.d.mts → cjs/server/IntlayerServerProvider.d.ts} +17 -4
- package/dist/cjs/server/getLocaleTranslation.cjs +3 -3
- package/dist/cjs/server/getLocaleTranslation.cjs.map +1 -1
- package/dist/cjs/server/getLocaleTranslation.d.ts +3 -0
- package/dist/cjs/server/index.cjs +6 -6
- package/dist/cjs/server/index.cjs.map +1 -1
- package/dist/cjs/server/index.d.ts +1 -1
- package/dist/cjs/server/serverContext.cjs.map +1 -1
- package/dist/cjs/server/serverContext.d.ts +4 -4
- package/dist/cjs/server/useIntlayer.cjs +2 -2
- package/dist/cjs/server/useIntlayer.cjs.map +1 -1
- package/dist/cjs/server/useIntlayer.d.ts +5 -0
- package/dist/cjs/server/useTraduction.cjs +2 -2
- package/dist/cjs/server/useTraduction.cjs.map +1 -1
- package/dist/cjs/server/useTraduction.d.ts +22 -1
- package/dist/cjs/useIntlayerBase.cjs.map +1 -1
- package/dist/cjs/useIntlayerBase.d.ts +3 -6
- 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/ContentEditor/ContentEditor.mjs +1 -0
- package/dist/esm/ContentEditor/ContentEditor.mjs.map +1 -1
- package/dist/esm/ContentEditor/contentRender.mjs +1 -0
- package/dist/esm/ContentEditor/contentRender.mjs.map +1 -1
- package/dist/esm/IntlayerProvider.d.mts +9 -0
- package/dist/esm/IntlayerProvider.mjs +18 -0
- package/dist/esm/IntlayerProvider.mjs.map +1 -0
- package/dist/esm/chunk-ZNCO4QRF.mjs +9 -0
- package/dist/esm/chunk-ZNCO4QRF.mjs.map +1 -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/IntlayerClientProvider.d.mts +25 -0
- package/dist/esm/client/IntlayerClientProvider.mjs +50 -0
- package/dist/esm/client/IntlayerClientProvider.mjs.map +1 -0
- 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 +3 -1
- package/dist/esm/client/index.mjs +12 -7
- package/dist/esm/client/index.mjs.map +1 -1
- package/dist/esm/client/useContent.d.mts +4 -1
- package/dist/esm/client/useContent.mjs +3 -2
- package/dist/esm/client/useContent.mjs.map +1 -1
- package/dist/esm/client/useIntlayer.d.mts +5 -0
- package/dist/esm/client/useIntlayer.mjs +3 -2
- package/dist/esm/client/useIntlayer.mjs.map +1 -1
- package/dist/esm/client/useLocale.d.mts +4 -0
- package/dist/esm/client/useLocale.mjs +8 -5
- 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.d.mts +9 -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 +22 -1
- package/dist/esm/client/useTraduction.mjs +3 -2
- 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 +28 -0
- package/dist/esm/getEnumeration.mjs +1 -0
- package/dist/esm/getEnumeration.mjs.map +1 -1
- package/dist/esm/getTranslation.d.mts +22 -1
- package/dist/esm/getTranslation.mjs +3 -4
- package/dist/esm/getTranslation.mjs.map +1 -1
- package/dist/esm/index.d.mts +3 -1
- package/dist/esm/index.mjs +10 -7
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/processDictionary/index.d.mts +1 -1
- package/dist/esm/processDictionary/index.mjs +35 -4
- package/dist/esm/processDictionary/index.mjs.map +1 -1
- package/dist/{cjs/server/LocaleServerContextProvider.d.ts → esm/server/IntlayerServerProvider.d.mts} +17 -4
- package/dist/esm/server/IntlayerServerProvider.mjs +20 -0
- package/dist/esm/server/IntlayerServerProvider.mjs.map +1 -0
- package/dist/esm/server/getLocaleTranslation.d.mts +3 -0
- package/dist/esm/server/getLocaleTranslation.mjs +5 -4
- package/dist/esm/server/getLocaleTranslation.mjs.map +1 -1
- package/dist/esm/server/index.d.mts +1 -1
- package/dist/esm/server/index.mjs +6 -5
- package/dist/esm/server/index.mjs.map +1 -1
- package/dist/esm/server/serverContext.d.mts +4 -4
- package/dist/esm/server/serverContext.mjs +1 -0
- package/dist/esm/server/serverContext.mjs.map +1 -1
- package/dist/esm/server/useIntlayer.d.mts +5 -0
- package/dist/esm/server/useIntlayer.mjs +3 -2
- package/dist/esm/server/useIntlayer.mjs.map +1 -1
- package/dist/esm/server/useTraduction.d.mts +22 -1
- package/dist/esm/server/useTraduction.mjs +3 -2
- package/dist/esm/server/useTraduction.mjs.map +1 -1
- package/dist/esm/useIntlayerBase.d.mts +3 -6
- package/dist/esm/useIntlayerBase.mjs +1 -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 +32 -7
- package/src/IntlayerProvider.tsx +24 -0
- package/src/cli/react-intlayer.ts +60 -0
- package/src/client/IntlayerClientProvider.tsx +76 -0
- package/src/client/getBrowserLocale.tsx +192 -0
- package/src/client/index.ts +7 -5
- package/src/client/useContent.ts +5 -2
- package/src/client/useIntlayer.ts +7 -2
- package/src/client/useLocale.ts +9 -5
- package/src/client/useLocaleBase.ts +24 -0
- package/src/client/useLocaleCookie.ts +11 -6
- package/src/client/useTraduction.ts +24 -3
- package/src/craco/craco.config.ts +11 -0
- package/src/craco/intlayerCracoPlugin.ts +89 -0
- package/src/getEnumeration.ts +28 -0
- package/src/getTranslation.ts +24 -6
- package/src/index.ts +5 -5
- package/src/processDictionary/index.ts +53 -6
- package/src/server/IntlayerServerProvider.tsx +36 -0
- package/src/server/getLocaleTranslation.ts +7 -4
- package/src/server/index.ts +3 -3
- package/src/server/serverContext.ts +4 -4
- package/src/server/useIntlayer.ts +8 -2
- package/src/server/useTraduction.ts +24 -3
- package/src/useIntlayerBase.ts +4 -1
- package/src/vite/intlayerPlugin.ts +59 -0
- package/dist/cjs/LocaleContextProvider.cjs.map +0 -1
- package/dist/cjs/LocaleContextProvider.d.ts +0 -9
- package/dist/cjs/client/LocaleClientContextProvider.cjs.map +0 -1
- package/dist/cjs/client/LocaleClientContextProvider.d.ts +0 -15
- package/dist/cjs/server/LocaleServerContextProvider.cjs.map +0 -1
- package/dist/esm/LocaleContextProvider.d.mts +0 -9
- package/dist/esm/LocaleContextProvider.mjs +0 -17
- package/dist/esm/LocaleContextProvider.mjs.map +0 -1
- package/dist/esm/client/LocaleClientContextProvider.d.mts +0 -15
- package/dist/esm/client/LocaleClientContextProvider.mjs +0 -21
- package/dist/esm/client/LocaleClientContextProvider.mjs.map +0 -1
- package/dist/esm/server/LocaleServerContextProvider.mjs +0 -16
- package/dist/esm/server/LocaleServerContextProvider.mjs.map +0 -1
- package/src/LocaleContextProvider.tsx +0 -26
- package/src/client/LocaleClientContextProvider.tsx +0 -35
- package/src/server/LocaleServerContextProvider.tsx +0 -24
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/useIntlayerBase.ts"],"sourcesContent":["/**\n * @intlayer/dictionaries-entry is a package that only returns the dictionary entry path.\n * Using an external package allow to alias it in the bundle configuration (such as webpack).\n * The alias allow hot reload the app (such as nextjs) on any dictionary change.\n */\nimport
|
|
1
|
+
{"version":3,"sources":["../../src/useIntlayerBase.ts"],"sourcesContent":["import type { Locales } from '@intlayer/config/client';\n/**\n * @intlayer/dictionaries-entry is a package that only returns the dictionary entry path.\n * Using an external package allow to alias it in the bundle configuration (such as webpack).\n * The alias allow hot reload the app (such as nextjs) on any dictionary change.\n */\nimport dictionaries from '@intlayer/dictionaries-entry';\nimport type { IntlayerDictionaryTypesConnector } from 'intlayer';\nimport type { Content } from './processDictionary/contentDictionary';\nimport { processDictionary } from './processDictionary/index';\n\nexport type StringFallback<T> = T extends never ? string : T; // If no keys are found, return string to disable error, and accept any string as dictionary key\nexport type DictionaryKeys = StringFallback<\n keyof IntlayerDictionaryTypesConnector\n>;\n\nexport type UseIntlayer = <T extends DictionaryKeys>(\n id: T,\n locale?: Locales\n) => IntlayerDictionaryTypesConnector[T];\n\n/**\n * Hook that picking one dictionary by its id and return the content.\n */\nexport const useIntlayerBase: UseIntlayer = <T extends DictionaryKeys>(\n id: T,\n locale?: Locales\n) => {\n const dictionary: Content = dictionaries[id];\n\n return processDictionary(\n dictionary,\n locale\n ) as IntlayerDictionaryTypesConnector[T];\n};\n"],"mappings":";AAMA,OAAO,kBAAkB;AAGzB,SAAS,yBAAyB;AAe3B,MAAM,kBAA+B,CAC1C,IACA,WACG;AACH,QAAM,aAAsB,aAAa,EAAE;AAE3C,SAAO;AAAA,IACL;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
|
|
3
|
+
type PluginOptions = {};
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* A Vite plugin that integrates IntLayer configuration into the build process
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* // Example usage of the plugin in a Vite configuration
|
|
10
|
+
* export default defineConfig({
|
|
11
|
+
* plugins: [ intLayerPlugin() ],
|
|
12
|
+
* });
|
|
13
|
+
* ```
|
|
14
|
+
* */
|
|
15
|
+
declare const intLayerPlugin: (_pluginOptions?: PluginOptions) => Plugin;
|
|
16
|
+
|
|
17
|
+
export { intLayerPlugin };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import "../chunk-ZNCO4QRF.mjs";
|
|
2
|
+
import { join, relative, resolve } from "path";
|
|
3
|
+
import { watch } from "@intlayer/chokidar";
|
|
4
|
+
import { getConfiguration, formatEnvVariable } from "@intlayer/config";
|
|
5
|
+
import { loadEnv } from "vite";
|
|
6
|
+
const intLayerPlugin = (_pluginOptions = {}) => ({
|
|
7
|
+
name: "vite-intlayer-plugin",
|
|
8
|
+
config: (config, { mode }) => {
|
|
9
|
+
const intlayerConfig = getConfiguration();
|
|
10
|
+
const env = formatEnvVariable("vite");
|
|
11
|
+
process.env = { ...process.env, ...loadEnv(mode, process.cwd()), ...env };
|
|
12
|
+
const { mainDir, baseDir } = intlayerConfig.content;
|
|
13
|
+
const dictionariesPath = join(mainDir, "dictionaries.mjs");
|
|
14
|
+
const relativeDictionariesPath = relative(baseDir, dictionariesPath);
|
|
15
|
+
config.resolve = {
|
|
16
|
+
...config.resolve,
|
|
17
|
+
alias: {
|
|
18
|
+
...config.resolve?.alias,
|
|
19
|
+
"@intlayer/dictionaries-entry": resolve(relativeDictionariesPath)
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
return config;
|
|
23
|
+
},
|
|
24
|
+
buildStart: () => {
|
|
25
|
+
watch({
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
27
|
+
persistent: import.meta.env === "development"
|
|
28
|
+
});
|
|
29
|
+
},
|
|
30
|
+
configureServer: (server) => {
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
export {
|
|
34
|
+
intLayerPlugin
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=intlayerPlugin.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/vite/intlayerPlugin.ts"],"sourcesContent":["import { join, relative, resolve } from 'path';\nimport { watch } from '@intlayer/chokidar';\nimport { getConfiguration, formatEnvVariable } from '@intlayer/config';\nimport { loadEnv, type Plugin } from 'vite';\n\n// Plugin options type definition\ntype PluginOptions = {\n // Custom options for your plugin, if any\n};\n\n/**\n *\n * A Vite plugin that integrates IntLayer configuration into the build process\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intLayerPlugin() ],\n * });\n * ```\n * */\nexport const intLayerPlugin = (_pluginOptions: PluginOptions = {}): Plugin => ({\n name: 'vite-intlayer-plugin',\n\n config: (config, { mode }) => {\n const intlayerConfig = getConfiguration();\n\n // Set all configuration values as environment variables\n const env = formatEnvVariable('vite');\n\n process.env = { ...process.env, ...loadEnv(mode, process.cwd()), ...env };\n\n const { mainDir, baseDir } = intlayerConfig.content;\n const dictionariesPath = join(mainDir, 'dictionaries.mjs');\n const relativeDictionariesPath = relative(baseDir, dictionariesPath);\n\n // Update Vite's resolve alias\n config.resolve = {\n ...config.resolve,\n alias: {\n ...config.resolve?.alias,\n '@intlayer/dictionaries-entry': resolve(relativeDictionariesPath),\n },\n };\n\n return config;\n },\n\n buildStart: () => {\n // Code to run when Vite build starts\n watch({\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n persistent: (import.meta as any).env === 'development',\n });\n },\n configureServer: (server) => {\n // Custom server configuration, if needed\n },\n});\n"],"mappings":";AAAA,SAAS,MAAM,UAAU,eAAe;AACxC,SAAS,aAAa;AACtB,SAAS,kBAAkB,yBAAyB;AACpD,SAAS,eAA4B;AAkB9B,MAAM,iBAAiB,CAAC,iBAAgC,CAAC,OAAe;AAAA,EAC7E,MAAM;AAAA,EAEN,QAAQ,CAAC,QAAQ,EAAE,KAAK,MAAM;AAC5B,UAAM,iBAAiB,iBAAiB;AAGxC,UAAM,MAAM,kBAAkB,MAAM;AAEpC,YAAQ,MAAM,EAAE,GAAG,QAAQ,KAAK,GAAG,QAAQ,MAAM,QAAQ,IAAI,CAAC,GAAG,GAAG,IAAI;AAExE,UAAM,EAAE,SAAS,QAAQ,IAAI,eAAe;AAC5C,UAAM,mBAAmB,KAAK,SAAS,kBAAkB;AACzD,UAAM,2BAA2B,SAAS,SAAS,gBAAgB;AAGnE,WAAO,UAAU;AAAA,MACf,GAAG,OAAO;AAAA,MACV,OAAO;AAAA,QACL,GAAG,OAAO,SAAS;AAAA,QACnB,gCAAgC,QAAQ,wBAAwB;AAAA,MAClE;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEA,YAAY,MAAM;AAEhB,UAAM;AAAA;AAAA,MAEJ,YAAa,YAAoB,QAAQ;AAAA,IAC3C,CAAC;AAAA,EACH;AAAA,EACA,iBAAiB,CAAC,WAAW;AAAA,EAE7B;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-intlayer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Internationalization layer for React applications - Layer of abstraction between the business logic and the data access layer. Manage internationalization in a simple way, through TypeScript, JavaScript or JSON declaration file.",
|
|
6
6
|
"keywords": [
|
|
@@ -37,6 +37,21 @@
|
|
|
37
37
|
"require": "./dist/cjs/server/index.cjs",
|
|
38
38
|
"import": "./dist/esm/server/index.mjs"
|
|
39
39
|
},
|
|
40
|
+
"./craco-plugin": {
|
|
41
|
+
"types": "./dist/esm/craco/intlayerCracoPlugin.d.mts",
|
|
42
|
+
"require": "./dist/cjs/craco/intlayerCracoPlugin.cjs",
|
|
43
|
+
"import": "./dist/esm/craco/intlayerCracoPlugin.mjs"
|
|
44
|
+
},
|
|
45
|
+
"./craco-config": {
|
|
46
|
+
"types": "./dist/esm/craco/craco.config.d.mts",
|
|
47
|
+
"require": "./dist/cjs/craco/craco.config.cjs",
|
|
48
|
+
"import": "./dist/esm/craco/craco.config.mjs"
|
|
49
|
+
},
|
|
50
|
+
"./vite-plugin": {
|
|
51
|
+
"types": "./dist/esm/vite/intlayerPlugin.d.mts",
|
|
52
|
+
"require": "./dist/cjs/vite/intlayerPlugin.cjs",
|
|
53
|
+
"import": "./dist/esm/vite/intlayerPlugin.mjs"
|
|
54
|
+
},
|
|
40
55
|
"./package.json": "./package.json"
|
|
41
56
|
},
|
|
42
57
|
"main": "./src/index.ts",
|
|
@@ -47,28 +62,38 @@
|
|
|
47
62
|
]
|
|
48
63
|
}
|
|
49
64
|
},
|
|
65
|
+
"bin": {
|
|
66
|
+
"react-intlayer": "./dist/cjs/cli/react-intlayer.cjs"
|
|
67
|
+
},
|
|
50
68
|
"files": [
|
|
51
69
|
"./dist",
|
|
52
70
|
"./src",
|
|
53
71
|
"./package.json"
|
|
54
72
|
],
|
|
55
73
|
"dependencies": {
|
|
74
|
+
"cross-spawn": "^7.0.3",
|
|
56
75
|
"js-cookie": "^3.0.5",
|
|
57
76
|
"react-cookie": "^7.1.4",
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"@intlayer/
|
|
61
|
-
"intlayer": "^1.
|
|
77
|
+
"vite": "^5.2.0",
|
|
78
|
+
"webpack": "^5.91.0",
|
|
79
|
+
"@intlayer/chokidar": "^1.2.1",
|
|
80
|
+
"@intlayer/config": "^1.2.1",
|
|
81
|
+
"@intlayer/core": "^1.2.1",
|
|
82
|
+
"@intlayer/dictionaries-entry": "^1.2.1",
|
|
83
|
+
"@intlayer/webpack": "^1.2.1",
|
|
84
|
+
"intlayer": "^1.2.1"
|
|
62
85
|
},
|
|
63
86
|
"devDependencies": {
|
|
87
|
+
"@craco/types": "^7.1.0",
|
|
88
|
+
"@types/cross-spawn": "^6.0.6",
|
|
64
89
|
"@types/js-cookie": "^3.0.6",
|
|
65
90
|
"@types/node": "^20.12.7",
|
|
66
|
-
"@types/react": "^18.2.
|
|
91
|
+
"@types/react": "^18.2.79",
|
|
67
92
|
"react": "^18.2.0",
|
|
68
93
|
"react-dom": "^18.2.0",
|
|
69
94
|
"rimraf": "5.0.5",
|
|
70
95
|
"tsup": "^8.0.2",
|
|
71
|
-
"typescript": "^5.4.
|
|
96
|
+
"typescript": "^5.4.5",
|
|
72
97
|
"@utils/eslint-config": "^1.0.1",
|
|
73
98
|
"@utils/ts-config": "^1.0.1"
|
|
74
99
|
},
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { getConfiguration } from '@intlayer/config/client';
|
|
2
|
+
import type { FC } from 'react';
|
|
3
|
+
import {
|
|
4
|
+
IntlayerClientProvider,
|
|
5
|
+
type IntlayerClientProviderProps,
|
|
6
|
+
} from './client/IntlayerClientProvider';
|
|
7
|
+
import {
|
|
8
|
+
IntlayerServerProvider,
|
|
9
|
+
type IntlayerServerProviderProps,
|
|
10
|
+
} from './server/IntlayerServerProvider';
|
|
11
|
+
|
|
12
|
+
const { defaultLocale } = getConfiguration().internationalization;
|
|
13
|
+
|
|
14
|
+
type IntlayerProviderProps = IntlayerClientProviderProps &
|
|
15
|
+
IntlayerServerProviderProps;
|
|
16
|
+
|
|
17
|
+
export const IntlayerProvider: FC<IntlayerProviderProps> = ({
|
|
18
|
+
children,
|
|
19
|
+
locale = defaultLocale,
|
|
20
|
+
}) => (
|
|
21
|
+
<IntlayerServerProvider locale={locale}>
|
|
22
|
+
<IntlayerClientProvider locale={locale}>{children}</IntlayerClientProvider>
|
|
23
|
+
</IntlayerServerProvider>
|
|
24
|
+
);
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
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 { localeCookie, setLocaleCookie } from './useLocaleCookie';
|
|
14
|
+
|
|
15
|
+
type IntlayerValue = {
|
|
16
|
+
locale: Locales;
|
|
17
|
+
setLocale: (newLocale: Locales) => void;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Context that store the current locale on the client side
|
|
22
|
+
*/
|
|
23
|
+
export const IntlayerClientContext = createContext<IntlayerValue>({
|
|
24
|
+
locale: localeCookie ?? getConfiguration().internationalization.defaultLocale,
|
|
25
|
+
setLocale: () => null,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Hook that provides the current locale
|
|
30
|
+
*/
|
|
31
|
+
export const useIntlayerContext = () => useContext(IntlayerClientContext);
|
|
32
|
+
|
|
33
|
+
export type IntlayerClientProviderProps = PropsWithChildren & {
|
|
34
|
+
locale?: Locales;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Provider that store the current locale on the client side
|
|
39
|
+
*/
|
|
40
|
+
export const IntlayerClientProvider: FC<IntlayerClientProviderProps> = ({
|
|
41
|
+
locale,
|
|
42
|
+
children,
|
|
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
|
+
};
|
package/src/client/index.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
export type {
|
|
1
|
+
export type { IntlayerClientProviderProps } from './IntlayerClientProvider';
|
|
2
2
|
export {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from './
|
|
3
|
+
IntlayerClientContext as IntlayerClient,
|
|
4
|
+
useIntlayerContext,
|
|
5
|
+
IntlayerClientProvider,
|
|
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,11 +1,14 @@
|
|
|
1
1
|
import type { LanguageContent } from '@intlayer/core';
|
|
2
|
-
import {
|
|
2
|
+
import { useLocaleBase } from './useLocaleBase';
|
|
3
3
|
import { useTraduction } from './useTraduction';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* On the client side, hook to get the translation content based on the locale
|
|
7
|
+
*/
|
|
5
8
|
export const useContent = <Content>(
|
|
6
9
|
languageContent: LanguageContent<Content>
|
|
7
10
|
) => {
|
|
8
|
-
const { locale } =
|
|
11
|
+
const { locale } = useLocaleBase();
|
|
9
12
|
|
|
10
13
|
const content = useTraduction(languageContent);
|
|
11
14
|
|
|
@@ -7,13 +7,18 @@ import {
|
|
|
7
7
|
useIntlayerBase,
|
|
8
8
|
type UseIntlayer,
|
|
9
9
|
} from '../useIntlayerBase';
|
|
10
|
-
import {
|
|
10
|
+
import { IntlayerClientContext } from './IntlayerClientProvider';
|
|
11
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
|
+
*/
|
|
12
17
|
export const useIntlayer: UseIntlayer = <T extends DictionaryKeys>(
|
|
13
18
|
id: T,
|
|
14
19
|
locale?: Locales
|
|
15
20
|
) => {
|
|
16
|
-
const { locale: currentLocale } = useContext(
|
|
21
|
+
const { locale: currentLocale } = useContext(IntlayerClientContext);
|
|
17
22
|
const localeTarget = locale ?? currentLocale;
|
|
18
23
|
|
|
19
24
|
return useIntlayerBase(id, localeTarget);
|
package/src/client/useLocale.ts
CHANGED
|
@@ -1,19 +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
|
-
import {
|
|
7
|
-
|
|
8
|
-
const { defaultLocale, locales: availableLocales } = intlayerIntlConfiguration;
|
|
6
|
+
import { IntlayerClientContext } from './IntlayerClientProvider';
|
|
9
7
|
|
|
8
|
+
/**
|
|
9
|
+
* On the client side, hook to get the current locale and all related fields
|
|
10
|
+
*/
|
|
10
11
|
export const useLocale = () => {
|
|
11
|
-
const {
|
|
12
|
+
const { defaultLocale, locales: availableLocales } =
|
|
13
|
+
getConfiguration().internationalization;
|
|
14
|
+
const { locale, setLocale } = useContext(IntlayerClientContext);
|
|
12
15
|
|
|
13
16
|
return {
|
|
14
17
|
locale, // Current locale
|
|
15
18
|
defaultLocale, // Principal locale defined in config
|
|
16
19
|
availableLocales, // List of the available locales defined in config
|
|
17
20
|
localeList, // List of all available locales
|
|
21
|
+
setLocale, // Function to set the locale
|
|
18
22
|
};
|
|
19
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,11 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
type Locales,
|
|
4
|
-
intlayerMiddlewareConfiguration,
|
|
5
|
-
} from '@intlayer/config/client';
|
|
1
|
+
import { type Locales, getConfiguration } from '@intlayer/config/client';
|
|
6
2
|
import Cookies from 'js-cookie';
|
|
7
3
|
|
|
8
|
-
const { cookieName } =
|
|
4
|
+
const { cookieName } = getConfiguration().middleware;
|
|
9
5
|
|
|
10
6
|
const cookieAttributes: Cookies.CookieAttributes = {
|
|
11
7
|
path: '/',
|
|
@@ -15,12 +11,21 @@ const cookieAttributes: Cookies.CookieAttributes = {
|
|
|
15
11
|
sameSite: 'strict',
|
|
16
12
|
};
|
|
17
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Get the locale cookie
|
|
16
|
+
*/
|
|
18
17
|
export const localeCookie = Cookies.get(cookieName) as Locales | undefined;
|
|
19
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Set the locale cookie
|
|
21
|
+
*/
|
|
20
22
|
export const setLocaleCookie = (locale: Locales) => {
|
|
21
23
|
Cookies.set(cookieName, locale, cookieAttributes);
|
|
22
24
|
};
|
|
23
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Hook that provides the locale cookie and a function to set it
|
|
28
|
+
*/
|
|
24
29
|
export const useLocaleCookie = () => ({
|
|
25
30
|
localeCookie,
|
|
26
31
|
setLocaleCookie,
|