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,55 +1,152 @@
|
|
|
1
|
+
import "../chunk-ZNCO4QRF.mjs";
|
|
2
|
+
import { getConfiguration } from "@intlayer/config/client";
|
|
1
3
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
NodeType
|
|
4
|
+
NodeType,
|
|
5
|
+
findMatchingCondition
|
|
6
6
|
} from "@intlayer/core";
|
|
7
|
+
import { renderContentEditor } from "intlayer-editor/client";
|
|
8
|
+
import { createElement } from "react";
|
|
7
9
|
import { getEnumeration } from '../getEnumeration.mjs';
|
|
8
10
|
import { getTranslation } from '../getTranslation.mjs';
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
+
const {
|
|
12
|
+
internationalization: { defaultLocale }
|
|
13
|
+
} = getConfiguration();
|
|
14
|
+
const processTranslation = (languageContent, locale, dictionaryId, dictionaryPath, keyPath = []) => {
|
|
11
15
|
const translationResult = getTranslation(
|
|
12
16
|
languageContent,
|
|
13
17
|
locale
|
|
14
18
|
);
|
|
15
|
-
|
|
19
|
+
const resultKeyPath = [
|
|
20
|
+
...keyPath,
|
|
21
|
+
{ type: NodeType.Translation, key: locale }
|
|
22
|
+
];
|
|
23
|
+
return processDictionary(
|
|
24
|
+
translationResult,
|
|
25
|
+
dictionaryId,
|
|
26
|
+
dictionaryPath,
|
|
27
|
+
resultKeyPath,
|
|
28
|
+
locale
|
|
29
|
+
);
|
|
16
30
|
};
|
|
17
|
-
const processEnumeration = (enumerationContent, locale) => {
|
|
31
|
+
const processEnumeration = (enumerationContent, locale, dictionaryId, dictionaryPath, keyPath = []) => {
|
|
18
32
|
return (quantity) => {
|
|
19
33
|
const enumerationResult = getEnumeration(
|
|
20
34
|
enumerationContent,
|
|
21
35
|
quantity
|
|
22
36
|
);
|
|
23
|
-
|
|
37
|
+
const matchingCondition = findMatchingCondition(
|
|
38
|
+
enumerationContent,
|
|
39
|
+
quantity
|
|
40
|
+
);
|
|
41
|
+
const resultKeyPath = [
|
|
42
|
+
...keyPath,
|
|
43
|
+
{ type: NodeType.Enumeration, key: matchingCondition.toString() }
|
|
44
|
+
];
|
|
45
|
+
return processDictionary(
|
|
46
|
+
enumerationResult,
|
|
47
|
+
dictionaryId,
|
|
48
|
+
dictionaryPath,
|
|
49
|
+
resultKeyPath,
|
|
50
|
+
locale
|
|
51
|
+
);
|
|
24
52
|
};
|
|
25
53
|
};
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
54
|
+
const isReactNode = (node) => typeof node?.key !== "undefined" && typeof node?.props !== "undefined";
|
|
55
|
+
const processNode = (field, locale, dictionaryId, dictionaryPath, keyPath = []) => {
|
|
56
|
+
if (typeof field === "object") {
|
|
57
|
+
if (field.nodeType === NodeType.Translation) {
|
|
58
|
+
return processTranslation(
|
|
59
|
+
field,
|
|
60
|
+
locale,
|
|
61
|
+
dictionaryId,
|
|
62
|
+
dictionaryPath,
|
|
63
|
+
keyPath
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
if (field.nodeType === NodeType.Enumeration) {
|
|
67
|
+
return processEnumeration(
|
|
68
|
+
field,
|
|
69
|
+
locale,
|
|
70
|
+
dictionaryId,
|
|
71
|
+
dictionaryPath,
|
|
72
|
+
keyPath
|
|
73
|
+
);
|
|
74
|
+
}
|
|
29
75
|
}
|
|
30
|
-
|
|
31
|
-
|
|
76
|
+
return processDictionary(
|
|
77
|
+
field,
|
|
78
|
+
dictionaryId,
|
|
79
|
+
dictionaryPath,
|
|
80
|
+
keyPath,
|
|
81
|
+
locale
|
|
82
|
+
);
|
|
83
|
+
};
|
|
84
|
+
const createReactElement = (element) => {
|
|
85
|
+
if (typeof element === "string") {
|
|
86
|
+
return element;
|
|
32
87
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
88
|
+
const convertChildrenAsArray = (element2) => {
|
|
89
|
+
if (element2?.props && typeof element2.props.children === "object") {
|
|
90
|
+
const childrenResult = [];
|
|
91
|
+
const { children } = element2.props;
|
|
92
|
+
Object.keys(children).forEach((key) => {
|
|
93
|
+
childrenResult.push(createReactElement(children[key]));
|
|
94
|
+
});
|
|
95
|
+
return {
|
|
96
|
+
...element2,
|
|
97
|
+
props: { ...element2.props, children: childrenResult }
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
...element2,
|
|
102
|
+
props: { ...element2.props, children: element2.props.children }
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
const fixedElement = convertChildrenAsArray(element);
|
|
106
|
+
const { type, props } = fixedElement;
|
|
107
|
+
return createElement(type ?? "div", props, ...props.children);
|
|
108
|
+
};
|
|
109
|
+
const traceKeys = ["filePath", "id", "nodeType"];
|
|
110
|
+
const processDictionary = (content, dictionaryId, dictionaryPath, keyPath = [], locale = defaultLocale) => {
|
|
111
|
+
if (isReactNode(content)) {
|
|
112
|
+
return createReactElement(
|
|
113
|
+
content
|
|
37
114
|
);
|
|
38
115
|
}
|
|
39
|
-
if (typeof field.type === "undefined") {
|
|
40
|
-
return processDictionary(field, locale);
|
|
41
|
-
}
|
|
42
|
-
return field;
|
|
43
|
-
};
|
|
44
|
-
const processDictionary = (content, locale = defaultLocale) => {
|
|
45
116
|
if (content && typeof content === "object") {
|
|
46
117
|
const result = {};
|
|
47
118
|
for (const key of Object.keys(content)) {
|
|
48
119
|
const field = content[key];
|
|
49
|
-
|
|
120
|
+
if (traceKeys.includes(key)) {
|
|
121
|
+
result[key] = field;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const resultKeyPath = [
|
|
125
|
+
...keyPath,
|
|
126
|
+
{ type: "ObjectExpression", key }
|
|
127
|
+
];
|
|
128
|
+
result[key] = processNode(
|
|
129
|
+
field,
|
|
130
|
+
locale,
|
|
131
|
+
dictionaryId,
|
|
132
|
+
dictionaryPath,
|
|
133
|
+
resultKeyPath
|
|
134
|
+
);
|
|
50
135
|
}
|
|
51
136
|
return result;
|
|
52
137
|
}
|
|
138
|
+
if (typeof content === "string") {
|
|
139
|
+
try {
|
|
140
|
+
return renderContentEditor(
|
|
141
|
+
content,
|
|
142
|
+
dictionaryId,
|
|
143
|
+
dictionaryPath,
|
|
144
|
+
keyPath
|
|
145
|
+
);
|
|
146
|
+
} catch (e) {
|
|
147
|
+
return content;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
53
150
|
return content;
|
|
54
151
|
};
|
|
55
152
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/processDictionary/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport {
|
|
1
|
+
{"version":3,"sources":["../../../src/processDictionary/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { type Locales, getConfiguration } from '@intlayer/config/client';\nimport {\n NodeType,\n type QuantityContent,\n type LanguageContent,\n findMatchingCondition,\n type KeyPath,\n} from '@intlayer/core';\nimport { renderContentEditor } from 'intlayer-editor/client';\nimport { type ReactElement, createElement, type ReactNode } from 'react';\nimport { getEnumeration } from '../getEnumeration';\nimport { getTranslation } from '../getTranslation';\nimport type {\n Content,\n ContentValue,\n TransformedContent,\n TransformedContentValue,\n} from './contentDictionary';\n\nconst {\n internationalization: { defaultLocale },\n} = getConfiguration();\n\nconst processTranslation = (\n languageContent: LanguageContent<ContentValue>,\n locale: Locales,\n dictionaryId: string,\n dictionaryPath: string,\n keyPath: KeyPath[] = []\n): TransformedContent => {\n const translationResult: ContentValue = getTranslation<ContentValue>(\n languageContent,\n locale\n );\n\n const resultKeyPath: KeyPath[] = [\n ...keyPath,\n { type: NodeType.Translation, key: locale },\n ];\n\n return processDictionary(\n translationResult as Content,\n dictionaryId,\n dictionaryPath,\n resultKeyPath,\n locale\n );\n};\n\nconst processEnumeration = (\n enumerationContent: QuantityContent<ContentValue>,\n locale: Locales,\n dictionaryId: string,\n dictionaryPath: string,\n keyPath: KeyPath[] = []\n): TransformedContentValue => {\n return (quantity: number): TransformedContentValue => {\n const enumerationResult: ContentValue = getEnumeration<ContentValue>(\n enumerationContent,\n quantity\n );\n\n const matchingCondition = findMatchingCondition(\n enumerationContent,\n quantity\n );\n\n const resultKeyPath: KeyPath[] = [\n ...keyPath,\n { type: NodeType.Enumeration, key: matchingCondition.toString() },\n ];\n\n //\n return processDictionary(\n enumerationResult as Content,\n dictionaryId,\n dictionaryPath,\n resultKeyPath,\n locale\n );\n };\n};\n\nconst isReactNode = (node: Record<string, unknown>): boolean =>\n typeof node?.key !== 'undefined' && typeof node?.props !== 'undefined';\n\nexport const processNode = (\n field: ContentValue | undefined,\n locale: Locales,\n dictionaryId: string,\n dictionaryPath: string,\n keyPath: KeyPath[] = []\n): TransformedContentValue => {\n if (typeof field === 'object') {\n if (field.nodeType === NodeType.Translation) {\n return processTranslation(\n field as LanguageContent<ContentValue>,\n locale,\n dictionaryId,\n dictionaryPath,\n keyPath\n );\n }\n\n if (field.nodeType === NodeType.Enumeration) {\n return processEnumeration(\n field satisfies QuantityContent<ContentValue>,\n locale,\n dictionaryId,\n dictionaryPath,\n keyPath\n );\n }\n }\n\n return processDictionary(\n field as Content,\n dictionaryId,\n dictionaryPath,\n keyPath,\n locale\n );\n};\n\n// This function recursively creates React elements from a given JSON-like structure\nconst createReactElement = (element: ReactElement) => {\n if (typeof element === 'string') {\n // If it's a string, simply return it (used for text content)\n return element;\n }\n\n // Destructure the component properties\n\n const convertChildrenAsArray = (element: ReactElement): ReactElement => {\n if (element?.props && typeof element.props.children === 'object') {\n const childrenResult: ReactNode[] = [];\n const { children } = element.props;\n\n // Create the children elements recursively, if any\n Object.keys(children).forEach((key) => {\n childrenResult.push(createReactElement(children[key]));\n });\n\n return {\n ...element,\n props: { ...element.props, children: childrenResult },\n };\n }\n\n return {\n ...element,\n props: { ...element.props, children: element.props.children },\n };\n };\n\n const fixedElement = convertChildrenAsArray(element);\n\n const { type, props } = fixedElement;\n\n // Create and return the React element\n return createElement(type ?? 'div', props, ...props.children);\n};\n\nconst traceKeys: string[] = ['filePath', 'id', 'nodeType'];\n\n/**\n * Function that process a dictionary and return the result to be used in the application.\n */\nexport const processDictionary = (\n content: Content,\n dictionaryId: string,\n dictionaryPath: string,\n keyPath: KeyPath[] = [],\n locale: Locales = defaultLocale\n): TransformedContent => {\n // If it's a React element, render it\n if (isReactNode(content)) {\n return createReactElement(\n content as unknown as ReactElement\n ) as unknown as TransformedContent;\n }\n\n if (content && typeof content === 'object') {\n const result: TransformedContent = {};\n\n // List each key in the content and process it\n for (const key of Object.keys(content)) {\n const field = content[key];\n\n if (traceKeys.includes(key)) {\n result[key] = field as TransformedContentValue;\n continue;\n }\n\n const resultKeyPath: KeyPath[] = [\n ...keyPath,\n { type: 'ObjectExpression', key },\n ];\n\n result[key] = processNode(\n field,\n locale,\n dictionaryId,\n dictionaryPath,\n resultKeyPath\n );\n }\n\n return result;\n }\n\n if (typeof content === 'string') {\n try {\n // renderContentEditor come from intlayer-editor, which is an optional dependency.\n // intlayer-editor should be installed in the project to use the content editor.\n return renderContentEditor(\n content,\n dictionaryId,\n dictionaryPath,\n keyPath\n );\n } catch (e) {\n // If renderContentEditor not available, it will return the content as is.\n return content;\n }\n }\n\n // If it's a string, number, or function, return it\n return content;\n};\n"],"mappings":"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","names":["element"]}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
+
import "../chunk-ZNCO4QRF.mjs";
|
|
1
2
|
import { jsx } from "react/jsx-runtime";
|
|
2
|
-
import {
|
|
3
|
-
intlayerIntlConfiguration
|
|
4
|
-
} from "@intlayer/config/client";
|
|
3
|
+
import { getConfiguration } from "@intlayer/config/client";
|
|
5
4
|
import { createServerContext, getServerContext } from './serverContext.mjs';
|
|
6
5
|
const IntlayerServerContext = createServerContext(
|
|
7
|
-
|
|
6
|
+
getConfiguration().internationalization.defaultLocale
|
|
8
7
|
);
|
|
9
8
|
const useIntlayer = () => getServerContext(IntlayerServerContext);
|
|
10
9
|
const locale = getServerContext(IntlayerServerContext);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/IntlayerServerProvider.tsx"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../../src/server/IntlayerServerProvider.tsx"],"sourcesContent":["import { type Locales, getConfiguration } from '@intlayer/config/client';\nimport type { FC, PropsWithChildren } from 'react';\nimport { createServerContext, getServerContext } from './serverContext';\n\n/**\n * Context that store the current locale on the server side\n */\nexport const IntlayerServerContext = createServerContext<Locales>(\n getConfiguration().internationalization.defaultLocale\n);\n\n/**\n * Hook that provides the current locale\n */\nexport const useIntlayer = () => getServerContext(IntlayerServerContext);\n\n/**\n * Get the current locale\n */\nexport const locale = getServerContext(IntlayerServerContext);\n\nexport type IntlayerServerProviderProps = PropsWithChildren & {\n locale: Locales;\n};\n\n/**\n * Provider that store the current locale on the server side\n */\nexport const IntlayerServerProvider: FC<IntlayerServerProviderProps> = ({\n children,\n locale,\n}) => (\n <IntlayerServerContext.Provider value={locale}>\n {children}\n </IntlayerServerContext.Provider>\n);\n"],"mappings":";AAgCE;AAhCF,SAAuB,wBAAwB;AAE/C,SAAS,qBAAqB,wBAAwB;AAK/C,MAAM,wBAAwB;AAAA,EACnC,iBAAiB,EAAE,qBAAqB;AAC1C;AAKO,MAAM,cAAc,MAAM,iBAAiB,qBAAqB;AAKhE,MAAM,SAAS,iBAAiB,qBAAqB;AASrD,MAAM,yBAA0D,CAAC;AAAA,EACtE;AAAA,EACA,QAAAA;AACF,MACE,oBAAC,sBAAsB,UAAtB,EAA+B,OAAOA,SACpC,UACH;","names":["locale"]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "../chunk-ZNCO4QRF.mjs";
|
|
2
|
+
import { getConfiguration } from "@intlayer/config/client";
|
|
2
3
|
import { getTranslationContent } from "@intlayer/core";
|
|
3
4
|
import { IntlayerServerContext } from './IntlayerServerProvider.mjs';
|
|
4
5
|
import { getServerContext } from './serverContext.mjs';
|
|
@@ -6,7 +7,7 @@ const getLocaleTranslation = (languageContent) => {
|
|
|
6
7
|
const locale = getServerContext(IntlayerServerContext);
|
|
7
8
|
const content = getTranslationContent(
|
|
8
9
|
languageContent,
|
|
9
|
-
locale ??
|
|
10
|
+
locale ?? getConfiguration().internationalization.defaultLocale
|
|
10
11
|
);
|
|
11
12
|
return {
|
|
12
13
|
locale,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/getLocaleTranslation.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../../src/server/getLocaleTranslation.ts"],"sourcesContent":["import { getConfiguration } from '@intlayer/config/client';\nimport { type LanguageContent, getTranslationContent } from '@intlayer/core';\nimport { IntlayerServerContext } from './IntlayerServerProvider';\nimport { getServerContext } from './serverContext';\n\n/**\n * On the server side, get the translation content based on the locale\n */\nexport const getLocaleTranslation = <Content>(\n languageContent: LanguageContent<Content>\n) => {\n const locale = getServerContext(IntlayerServerContext);\n const content = getTranslationContent<Content>(\n languageContent,\n locale ?? getConfiguration().internationalization.defaultLocale\n );\n\n return {\n locale,\n content,\n };\n};\n"],"mappings":";AAAA,SAAS,wBAAwB;AACjC,SAA+B,6BAA6B;AAC5D,SAAS,6BAA6B;AACtC,SAAS,wBAAwB;AAK1B,MAAM,uBAAuB,CAClC,oBACG;AACH,QAAM,SAAS,iBAAiB,qBAAqB;AACrD,QAAM,UAAU;AAAA,IACd;AAAA,IACA,UAAU,iBAAiB,EAAE,qBAAqB;AAAA,EACpD;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/index.ts"],"sourcesContent":["export { getLocaleTranslation as getLocaleContent } from './getLocaleTranslation';\nexport { useTraduction } from './useTraduction';\nexport {\n IntlayerServerContext as IntlayerServer,\n locale,\n IntlayerServerProvider,\n} from './IntlayerServerProvider';\nexport { useIntlayer } from './useIntlayer';\n"],"mappings":"AAAA,SAAiC,4BAAwB;AACzD,SAAS,qBAAqB;AAC9B;AAAA,EAC2B;AAAA,EACzB;AAAA,EACA;AAAA,OACK;AACP,SAAS,mBAAmB;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../src/server/index.ts"],"sourcesContent":["export { getLocaleTranslation as getLocaleContent } from './getLocaleTranslation';\nexport { useTraduction } from './useTraduction';\nexport {\n IntlayerServerContext as IntlayerServer,\n locale,\n IntlayerServerProvider,\n} from './IntlayerServerProvider';\nexport { useIntlayer } from './useIntlayer';\n"],"mappings":";AAAA,SAAiC,4BAAwB;AACzD,SAAS,qBAAqB;AAC9B;AAAA,EAC2B;AAAA,EACzB;AAAA,EACA;AAAA,OACK;AACP,SAAS,mBAAmB;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/serverContext.ts"],"sourcesContent":["/**\n * Creates a new datastore for a given server context.\n * Attempts to closely mimic the `createContext` API.\n *\n * @example\n * const IntlayerServer = createServerContext<string | null>(null);\n *\n * <IntlayerServer.Provider value={locale}>\n * {children}\n * </IntlayerServer.Provider>\n */\n\nimport react from 'react';\n\ntype CacheType<T> = () => { value: T | undefined };\n\nconst cacheFallback = () => () => ({ value: undefined });\n\nexport const createServerContext = <T>(defaultValue?: T): ServerContext<T> => {\n throwInClient();\n\n const cache = react.cache<CacheType<T>> ?? cacheFallback;\n\n const getCache = cache(() => ({\n value: undefined,\n }));\n\n return {\n Provider: ({ children, value }) => {\n getCache().value = value;\n return children;\n },\n Consumer: ({ children }) => {\n const store = getCache();\n return children(store ? store.value : defaultValue);\n },\n _storage: getCache,\n _defaultValue: defaultValue,\n };\n};\n\n/**\n * Fetches a value present in a given server context.\n * Attempts to closely mimic the `useContext` API.\n *\n * @example\n * getServerContext(IntlayerServer);\n */\nexport const getServerContext = <T>({\n _storage,\n _defaultValue,\n}: ServerContext<T>) => {\n // throwInClient();\n const store = _storage();\n if (!store) return _defaultValue;\n return store.value;\n};\n\ntype ServerContext<T> = {\n Provider: ({\n children,\n value,\n }: {\n children: React.ReactNode;\n value: T;\n }) => React.ReactNode;\n Consumer: ({\n children,\n }: {\n children: (context: T | undefined) => React.ReactNode;\n }) => React.ReactNode;\n _storage: () => { value: T | undefined };\n _defaultValue: T | undefined;\n};\n\n/**\n * Throws if called within a client component environment.\n * Useful to help prevent mistakes.\n */\nconst throwInClient = (): void | never => {\n // If window.document is defined we're in a client component\n if (typeof window !== 'undefined') {\n throw new Error(`createServerContext only works in Server Components`);\n }\n};\n"],"mappings":"AAYA,OAAO,WAAW;AAIlB,MAAM,gBAAgB,MAAM,OAAO,EAAE,OAAO,OAAU;AAE/C,MAAM,sBAAsB,CAAI,iBAAuC;AAC5E,gBAAc;AAEd,QAAM,QAAQ,MAAM,SAAuB;AAE3C,QAAM,WAAW,MAAM,OAAO;AAAA,IAC5B,OAAO;AAAA,EACT,EAAE;AAEF,SAAO;AAAA,IACL,UAAU,CAAC,EAAE,UAAU,MAAM,MAAM;AACjC,eAAS,EAAE,QAAQ;AACnB,aAAO;AAAA,IACT;AAAA,IACA,UAAU,CAAC,EAAE,SAAS,MAAM;AAC1B,YAAM,QAAQ,SAAS;AACvB,aAAO,SAAS,QAAQ,MAAM,QAAQ,YAAY;AAAA,IACpD;AAAA,IACA,UAAU;AAAA,IACV,eAAe;AAAA,EACjB;AACF;AASO,MAAM,mBAAmB,CAAI;AAAA,EAClC;AAAA,EACA;AACF,MAAwB;AAEtB,QAAM,QAAQ,SAAS;AACvB,MAAI,CAAC;AAAO,WAAO;AACnB,SAAO,MAAM;AACf;AAuBA,MAAM,gBAAgB,MAAoB;AAExC,MAAI,OAAO,WAAW,aAAa;AACjC,UAAM,IAAI,MAAM,qDAAqD;AAAA,EACvE;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../src/server/serverContext.ts"],"sourcesContent":["/**\n * Creates a new datastore for a given server context.\n * Attempts to closely mimic the `createContext` API.\n *\n * @example\n * const IntlayerServer = createServerContext<string | null>(null);\n *\n * <IntlayerServer.Provider value={locale}>\n * {children}\n * </IntlayerServer.Provider>\n */\n\nimport react from 'react';\n\ntype CacheType<T> = () => { value: T | undefined };\n\nconst cacheFallback = () => () => ({ value: undefined });\n\nexport const createServerContext = <T>(defaultValue?: T): ServerContext<T> => {\n throwInClient();\n\n const cache = react.cache<CacheType<T>> ?? cacheFallback;\n\n const getCache = cache(() => ({\n value: undefined,\n }));\n\n return {\n Provider: ({ children, value }) => {\n getCache().value = value;\n return children;\n },\n Consumer: ({ children }) => {\n const store = getCache();\n return children(store ? store.value : defaultValue);\n },\n _storage: getCache,\n _defaultValue: defaultValue,\n };\n};\n\n/**\n * Fetches a value present in a given server context.\n * Attempts to closely mimic the `useContext` API.\n *\n * @example\n * getServerContext(IntlayerServer);\n */\nexport const getServerContext = <T>({\n _storage,\n _defaultValue,\n}: ServerContext<T>) => {\n // throwInClient();\n const store = _storage();\n if (!store) return _defaultValue;\n return store.value;\n};\n\ntype ServerContext<T> = {\n Provider: ({\n children,\n value,\n }: {\n children: React.ReactNode;\n value: T;\n }) => React.ReactNode;\n Consumer: ({\n children,\n }: {\n children: (context: T | undefined) => React.ReactNode;\n }) => React.ReactNode;\n _storage: () => { value: T | undefined };\n _defaultValue: T | undefined;\n};\n\n/**\n * Throws if called within a client component environment.\n * Useful to help prevent mistakes.\n */\nconst throwInClient = (): void | never => {\n // If window.document is defined we're in a client component\n if (typeof window !== 'undefined') {\n throw new Error(`createServerContext only works in Server Components`);\n }\n};\n"],"mappings":";AAYA,OAAO,WAAW;AAIlB,MAAM,gBAAgB,MAAM,OAAO,EAAE,OAAO,OAAU;AAE/C,MAAM,sBAAsB,CAAI,iBAAuC;AAC5E,gBAAc;AAEd,QAAM,QAAQ,MAAM,SAAuB;AAE3C,QAAM,WAAW,MAAM,OAAO;AAAA,IAC5B,OAAO;AAAA,EACT,EAAE;AAEF,SAAO;AAAA,IACL,UAAU,CAAC,EAAE,UAAU,MAAM,MAAM;AACjC,eAAS,EAAE,QAAQ;AACnB,aAAO;AAAA,IACT;AAAA,IACA,UAAU,CAAC,EAAE,SAAS,MAAM;AAC1B,YAAM,QAAQ,SAAS;AACvB,aAAO,SAAS,QAAQ,MAAM,QAAQ,YAAY;AAAA,IACpD;AAAA,IACA,UAAU;AAAA,IACV,eAAe;AAAA,EACjB;AACF;AASO,MAAM,mBAAmB,CAAI;AAAA,EAClC;AAAA,EACA;AACF,MAAwB;AAEtB,QAAM,QAAQ,SAAS;AACvB,MAAI,CAAC;AAAO,WAAO;AACnB,SAAO,MAAM;AACf;AAuBA,MAAM,gBAAgB,MAAoB;AAExC,MAAI,OAAO,WAAW,aAAa;AACjC,UAAM,IAAI,MAAM,qDAAqD;AAAA,EACvE;AACF;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/useIntlayer.ts"],"sourcesContent":["import type { Locales } from '@intlayer/config/client';\nimport {\n type DictionaryKeys,\n useIntlayerBase,\n type UseIntlayer,\n} from '../useIntlayerBase';\nimport { IntlayerServerContext } from './IntlayerServerProvider';\nimport { getServerContext } from './serverContext';\n\n/**\n * On the server side, Hook that picking one dictionary by its id and return the content\n *\n * If the locale is not provided, it will use the locale from the server context\n */\nexport const useIntlayer: UseIntlayer = <T extends DictionaryKeys>(\n id: T,\n locale?: Locales\n) => {\n const localeTarget =\n locale ?? getServerContext<Locales>(IntlayerServerContext);\n\n return useIntlayerBase(id, localeTarget);\n};\n"],"mappings":"AACA;AAAA,EAEE;AAAA,OAEK;AACP,SAAS,6BAA6B;AACtC,SAAS,wBAAwB;AAO1B,MAAM,cAA2B,CACtC,IACA,WACG;AACH,QAAM,eACJ,UAAU,iBAA0B,qBAAqB;AAE3D,SAAO,gBAAgB,IAAI,YAAY;AACzC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../src/server/useIntlayer.ts"],"sourcesContent":["import type { Locales } from '@intlayer/config/client';\nimport {\n type DictionaryKeys,\n useIntlayerBase,\n type UseIntlayer,\n} from '../useIntlayerBase';\nimport { IntlayerServerContext } from './IntlayerServerProvider';\nimport { getServerContext } from './serverContext';\n\n/**\n * On the server side, Hook that picking one dictionary by its id and return the content\n *\n * If the locale is not provided, it will use the locale from the server context\n */\nexport const useIntlayer: UseIntlayer = <T extends DictionaryKeys>(\n id: T,\n locale?: Locales\n) => {\n const localeTarget =\n locale ?? getServerContext<Locales>(IntlayerServerContext);\n\n return useIntlayerBase(id, localeTarget);\n};\n"],"mappings":";AACA;AAAA,EAEE;AAAA,OAEK;AACP,SAAS,6BAA6B;AACtC,SAAS,wBAAwB;AAO1B,MAAM,cAA2B,CACtC,IACA,WACG;AACH,QAAM,eACJ,UAAU,iBAA0B,qBAAqB;AAE3D,SAAO,gBAAgB,IAAI,YAAY;AACzC;","names":[]}
|
|
@@ -9,12 +9,13 @@ import { LanguageContent } from '@intlayer/core';
|
|
|
9
9
|
*
|
|
10
10
|
* Usage:
|
|
11
11
|
*
|
|
12
|
-
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const content = useTraduction<string>({
|
|
13
14
|
* en: 'Hello',
|
|
14
15
|
* fr: 'Bonjour',
|
|
15
|
-
* },
|
|
16
|
-
* 'fr');
|
|
16
|
+
* }, 'fr');
|
|
17
17
|
* // 'Bonjour'
|
|
18
|
+
* ```
|
|
18
19
|
*
|
|
19
20
|
* Using TypeScript:
|
|
20
21
|
* - this function will require each locale to be defined if defined in the project configuration.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/useTraduction.ts"],"sourcesContent":["import type { Locales } from '@intlayer/config/client';\nimport type { LanguageContent } from '@intlayer/core';\nimport { getTranslation } from '../getTranslation';\nimport { IntlayerServerContext } from './IntlayerServerProvider';\nimport { getServerContext } from './serverContext';\n\n/**\n * On the server side, hook to get the translation content based on the locale\n *\n * If not locale found, it will return the content related to the default locale.\n *\n * Return either the content editor, or the content itself depending on the configuration.\n *\n * Usage:\n *\n * const content =
|
|
1
|
+
{"version":3,"sources":["../../../src/server/useTraduction.ts"],"sourcesContent":["import type { Locales } from '@intlayer/config/client';\nimport type { LanguageContent } from '@intlayer/core';\nimport { getTranslation } from '../getTranslation';\nimport { IntlayerServerContext } from './IntlayerServerProvider';\nimport { getServerContext } from './serverContext';\n\n/**\n * On the server side, hook to get the translation content based on the locale\n *\n * If not locale found, it will return the content related to the default locale.\n *\n * Return either the content editor, or the content itself depending on the configuration.\n *\n * Usage:\n *\n * ```ts\n * const content = useTraduction<string>({\n * en: 'Hello',\n * fr: 'Bonjour',\n * }, 'fr');\n * // 'Bonjour'\n * ```\n *\n * Using TypeScript:\n * - this function will require each locale to be defined if defined in the project configuration.\n * - If a locale is missing, it will make each existing locale optional and raise an error if the locale is not found.\n */\nexport const useTraduction = <Content = string>(\n languageContent: LanguageContent<Content>\n) => {\n const locale = getServerContext<Locales>(IntlayerServerContext);\n\n return getTranslation(languageContent, locale);\n};\n"],"mappings":";AAEA,SAAS,sBAAsB;AAC/B,SAAS,6BAA6B;AACtC,SAAS,wBAAwB;AAuB1B,MAAM,gBAAgB,CAC3B,oBACG;AACH,QAAM,SAAS,iBAA0B,qBAAqB;AAE9D,SAAO,eAAe,iBAAiB,MAAM;AAC/C;","names":[]}
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
+
import "./chunk-ZNCO4QRF.mjs";
|
|
1
2
|
import dictionaries from "@intlayer/dictionaries-entry";
|
|
2
3
|
import { processDictionary } from './processDictionary/index.mjs';
|
|
3
4
|
const useIntlayerBase = (id, locale) => {
|
|
4
5
|
const dictionary = dictionaries[id];
|
|
5
6
|
return processDictionary(
|
|
6
7
|
dictionary,
|
|
8
|
+
id,
|
|
9
|
+
dictionary.filePath,
|
|
10
|
+
[],
|
|
7
11
|
locale
|
|
8
12
|
);
|
|
9
13
|
};
|
|
@@ -1 +1 @@
|
|
|
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":[]}
|
|
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 id,\n dictionary.filePath as string,\n [],\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,IACA,WAAW;AAAA,IACX,CAAC;AAAA,IACD;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: () => {
|
|
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: () => {\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,MAAM;AAAA,EAEvB;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-intlayer",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
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,20 +62,30 @@
|
|
|
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": "^
|
|
77
|
+
"vite": "^5.2.0",
|
|
78
|
+
"webpack": "^5.91.0",
|
|
79
|
+
"@intlayer/chokidar": "^2.0.0",
|
|
80
|
+
"@intlayer/core": "^2.0.0",
|
|
81
|
+
"@intlayer/config": "^2.0.0",
|
|
82
|
+
"@intlayer/webpack": "^2.0.0",
|
|
83
|
+
"@intlayer/dictionaries-entry": "^2.0.0",
|
|
84
|
+
"intlayer": "^2.0.0"
|
|
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
91
|
"@types/react": "^18.2.79",
|
|
@@ -74,7 +99,13 @@
|
|
|
74
99
|
},
|
|
75
100
|
"peerDependencies": {
|
|
76
101
|
"react": "^18.2.0",
|
|
77
|
-
"react-dom": "^18.2.0"
|
|
102
|
+
"react-dom": "^18.2.0",
|
|
103
|
+
"intlayer-editor": "^2.0.0"
|
|
104
|
+
},
|
|
105
|
+
"peerDependenciesMeta": {
|
|
106
|
+
"intlayer-editor": {
|
|
107
|
+
"optional": true
|
|
108
|
+
}
|
|
78
109
|
},
|
|
79
110
|
"engines": {
|
|
80
111
|
"node": ">=14.18"
|
package/src/IntlayerProvider.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getConfiguration } from '@intlayer/config/client';
|
|
2
2
|
import type { FC } from 'react';
|
|
3
3
|
import {
|
|
4
4
|
IntlayerClientProvider,
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
type IntlayerServerProviderProps,
|
|
10
10
|
} from './server/IntlayerServerProvider';
|
|
11
11
|
|
|
12
|
-
const defaultLocale =
|
|
12
|
+
const { defaultLocale } = getConfiguration().internationalization;
|
|
13
13
|
|
|
14
14
|
type IntlayerProviderProps = IntlayerClientProviderProps &
|
|
15
15
|
IntlayerServerProviderProps;
|
|
@@ -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,15 @@
|
|
|
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);
|