zmdms-utils 0.0.97 → 0.0.99
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/es/request.d.ts +11 -0
- package/dist/es/request.js +98 -58
- package/dist/es/src/locales/i18n.js +5 -4
- package/dist/es/src/locales/t-in-non-react.js +5 -4
- package/dist/es/src/locales/use-translation.js +2 -1
- package/dist/index.js +2 -2
- package/package.json +3 -1
- package/src/request.ts +46 -7
- package/dist/es/node_modules/html-parse-stringify/dist/html-parse-stringify.module.js +0 -5
- package/dist/es/node_modules/i18next/dist/esm/i18next.js +0 -2242
- package/dist/es/node_modules/react-i18next/dist/es/Trans.js +0 -47
- package/dist/es/node_modules/react-i18next/dist/es/TransWithoutContext.js +0 -312
- package/dist/es/node_modules/react-i18next/dist/es/context.js +0 -18
- package/dist/es/node_modules/react-i18next/dist/es/defaults.js +0 -21
- package/dist/es/node_modules/react-i18next/dist/es/i18nInstance.js +0 -7
- package/dist/es/node_modules/react-i18next/dist/es/initReactI18next.js +0 -12
- package/dist/es/node_modules/react-i18next/dist/es/unescape.js +0 -27
- package/dist/es/node_modules/react-i18next/dist/es/useTranslation.js +0 -112
- package/dist/es/node_modules/react-i18next/dist/es/utils.js +0 -63
- package/dist/es/node_modules/void-elements/index.js +0 -23
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { useContext } from 'react';
|
|
2
|
-
import { Trans as Trans$1 } from './TransWithoutContext.js';
|
|
3
|
-
export { nodesToString } from './TransWithoutContext.js';
|
|
4
|
-
import { I18nContext } from './context.js';
|
|
5
|
-
import { getI18n } from './i18nInstance.js';
|
|
6
|
-
|
|
7
|
-
function Trans({
|
|
8
|
-
children,
|
|
9
|
-
count,
|
|
10
|
-
parent,
|
|
11
|
-
i18nKey,
|
|
12
|
-
context,
|
|
13
|
-
tOptions = {},
|
|
14
|
-
values,
|
|
15
|
-
defaults,
|
|
16
|
-
components,
|
|
17
|
-
ns,
|
|
18
|
-
i18n: i18nFromProps,
|
|
19
|
-
t: tFromProps,
|
|
20
|
-
shouldUnescape,
|
|
21
|
-
...additionalProps
|
|
22
|
-
}) {
|
|
23
|
-
const {
|
|
24
|
-
i18n: i18nFromContext,
|
|
25
|
-
defaultNS: defaultNSFromContext
|
|
26
|
-
} = useContext(I18nContext) || {};
|
|
27
|
-
const i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
28
|
-
const t = tFromProps || i18n?.t.bind(i18n);
|
|
29
|
-
return Trans$1({
|
|
30
|
-
children,
|
|
31
|
-
count,
|
|
32
|
-
parent,
|
|
33
|
-
i18nKey,
|
|
34
|
-
context,
|
|
35
|
-
tOptions,
|
|
36
|
-
values,
|
|
37
|
-
defaults,
|
|
38
|
-
components,
|
|
39
|
-
ns: ns || t?.ns || defaultNSFromContext || i18n?.options?.defaultNS,
|
|
40
|
-
i18n,
|
|
41
|
-
t: tFromProps,
|
|
42
|
-
shouldUnescape,
|
|
43
|
-
...additionalProps
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export { Trans };
|
|
@@ -1,312 +0,0 @@
|
|
|
1
|
-
import { createElement, isValidElement, cloneElement, Children, Fragment } from 'react';
|
|
2
|
-
import c from '../../../html-parse-stringify/dist/html-parse-stringify.module.js';
|
|
3
|
-
import { warnOnce, isString, warn, isObject } from './utils.js';
|
|
4
|
-
import { getDefaults } from './defaults.js';
|
|
5
|
-
import { getI18n } from './i18nInstance.js';
|
|
6
|
-
|
|
7
|
-
const hasChildren = (node, checkLength) => {
|
|
8
|
-
if (!node) return false;
|
|
9
|
-
const base = node.props?.children ?? node.children;
|
|
10
|
-
if (checkLength) return base.length > 0;
|
|
11
|
-
return !!base;
|
|
12
|
-
};
|
|
13
|
-
const getChildren = node => {
|
|
14
|
-
if (!node) return [];
|
|
15
|
-
const children = node.props?.children ?? node.children;
|
|
16
|
-
return node.props?.i18nIsDynamicList ? getAsArray(children) : children;
|
|
17
|
-
};
|
|
18
|
-
const hasValidReactChildren = children => Array.isArray(children) && children.every(isValidElement);
|
|
19
|
-
const getAsArray = data => Array.isArray(data) ? data : [data];
|
|
20
|
-
const mergeProps = (source, target) => {
|
|
21
|
-
const newTarget = {
|
|
22
|
-
...target
|
|
23
|
-
};
|
|
24
|
-
newTarget.props = Object.assign(source.props, target.props);
|
|
25
|
-
return newTarget;
|
|
26
|
-
};
|
|
27
|
-
const nodesToString = (children, i18nOptions, i18n, i18nKey) => {
|
|
28
|
-
if (!children) return '';
|
|
29
|
-
let stringNode = '';
|
|
30
|
-
const childrenArray = getAsArray(children);
|
|
31
|
-
const keepArray = i18nOptions?.transSupportBasicHtmlNodes ? i18nOptions.transKeepBasicHtmlNodesFor ?? [] : [];
|
|
32
|
-
childrenArray.forEach((child, childIndex) => {
|
|
33
|
-
if (isString(child)) {
|
|
34
|
-
stringNode += `${child}`;
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
if (isValidElement(child)) {
|
|
38
|
-
const {
|
|
39
|
-
props,
|
|
40
|
-
type
|
|
41
|
-
} = child;
|
|
42
|
-
const childPropsCount = Object.keys(props).length;
|
|
43
|
-
const shouldKeepChild = keepArray.indexOf(type) > -1;
|
|
44
|
-
const childChildren = props.children;
|
|
45
|
-
if (!childChildren && shouldKeepChild && !childPropsCount) {
|
|
46
|
-
stringNode += `<${type}/>`;
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
if (!childChildren && (!shouldKeepChild || childPropsCount) || props.i18nIsDynamicList) {
|
|
50
|
-
stringNode += `<${childIndex}></${childIndex}>`;
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
if (shouldKeepChild && childPropsCount === 1 && isString(childChildren)) {
|
|
54
|
-
stringNode += `<${type}>${childChildren}</${type}>`;
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
const content = nodesToString(childChildren, i18nOptions, i18n, i18nKey);
|
|
58
|
-
stringNode += `<${childIndex}>${content}</${childIndex}>`;
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
if (child === null) {
|
|
62
|
-
warn(i18n, 'TRANS_NULL_VALUE', `Passed in a null value as child`, {
|
|
63
|
-
i18nKey
|
|
64
|
-
});
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
if (isObject(child)) {
|
|
68
|
-
const {
|
|
69
|
-
format,
|
|
70
|
-
...clone
|
|
71
|
-
} = child;
|
|
72
|
-
const keys = Object.keys(clone);
|
|
73
|
-
if (keys.length === 1) {
|
|
74
|
-
const value = format ? `${keys[0]}, ${format}` : keys[0];
|
|
75
|
-
stringNode += `{{${value}}}`;
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
warn(i18n, 'TRANS_INVALID_OBJ', `Invalid child - Object should only have keys {{ value, format }} (format is optional).`, {
|
|
79
|
-
i18nKey,
|
|
80
|
-
child
|
|
81
|
-
});
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
warn(i18n, 'TRANS_INVALID_VAR', `Passed in a variable like {number} - pass variables for interpolation as full objects like {{number}}.`, {
|
|
85
|
-
i18nKey,
|
|
86
|
-
child
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
return stringNode;
|
|
90
|
-
};
|
|
91
|
-
const renderNodes = (children, targetString, i18n, i18nOptions, combinedTOpts, shouldUnescape) => {
|
|
92
|
-
if (targetString === '') return [];
|
|
93
|
-
const keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
|
|
94
|
-
const emptyChildrenButNeedsHandling = targetString && new RegExp(keepArray.map(keep => `<${keep}`).join('|')).test(targetString);
|
|
95
|
-
if (!children && !emptyChildrenButNeedsHandling && !shouldUnescape) return [targetString];
|
|
96
|
-
const data = {};
|
|
97
|
-
const getData = childs => {
|
|
98
|
-
const childrenArray = getAsArray(childs);
|
|
99
|
-
childrenArray.forEach(child => {
|
|
100
|
-
if (isString(child)) return;
|
|
101
|
-
if (hasChildren(child)) getData(getChildren(child));else if (isObject(child) && !isValidElement(child)) Object.assign(data, child);
|
|
102
|
-
});
|
|
103
|
-
};
|
|
104
|
-
getData(children);
|
|
105
|
-
const ast = c.parse(`<0>${targetString}</0>`);
|
|
106
|
-
const opts = {
|
|
107
|
-
...data,
|
|
108
|
-
...combinedTOpts
|
|
109
|
-
};
|
|
110
|
-
const renderInner = (child, node, rootReactNode) => {
|
|
111
|
-
const childs = getChildren(child);
|
|
112
|
-
const mappedChildren = mapAST(childs, node.children, rootReactNode);
|
|
113
|
-
return hasValidReactChildren(childs) && mappedChildren.length === 0 || child.props?.i18nIsDynamicList ? childs : mappedChildren;
|
|
114
|
-
};
|
|
115
|
-
const pushTranslatedJSX = (child, inner, mem, i, isVoid) => {
|
|
116
|
-
if (child.dummy) {
|
|
117
|
-
child.children = inner;
|
|
118
|
-
mem.push(cloneElement(child, {
|
|
119
|
-
key: i
|
|
120
|
-
}, isVoid ? undefined : inner));
|
|
121
|
-
} else {
|
|
122
|
-
mem.push(...Children.map([child], c => {
|
|
123
|
-
const props = {
|
|
124
|
-
...c.props
|
|
125
|
-
};
|
|
126
|
-
delete props.i18nIsDynamicList;
|
|
127
|
-
return createElement(c.type, {
|
|
128
|
-
...props,
|
|
129
|
-
key: i,
|
|
130
|
-
ref: c.ref
|
|
131
|
-
}, isVoid ? null : inner);
|
|
132
|
-
}));
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
const mapAST = (reactNode, astNode, rootReactNode) => {
|
|
136
|
-
const reactNodes = getAsArray(reactNode);
|
|
137
|
-
const astNodes = getAsArray(astNode);
|
|
138
|
-
return astNodes.reduce((mem, node, i) => {
|
|
139
|
-
const translationContent = node.children?.[0]?.content && i18n.services.interpolator.interpolate(node.children[0].content, opts, i18n.language);
|
|
140
|
-
if (node.type === 'tag') {
|
|
141
|
-
let tmp = reactNodes[parseInt(node.name, 10)];
|
|
142
|
-
if (rootReactNode.length === 1 && !tmp) tmp = rootReactNode[0][node.name];
|
|
143
|
-
if (!tmp) tmp = {};
|
|
144
|
-
const child = Object.keys(node.attrs).length !== 0 ? mergeProps({
|
|
145
|
-
props: node.attrs
|
|
146
|
-
}, tmp) : tmp;
|
|
147
|
-
const isElement = isValidElement(child);
|
|
148
|
-
const isValidTranslationWithChildren = isElement && hasChildren(node, true) && !node.voidElement;
|
|
149
|
-
const isEmptyTransWithHTML = emptyChildrenButNeedsHandling && isObject(child) && child.dummy && !isElement;
|
|
150
|
-
const isKnownComponent = isObject(children) && Object.hasOwnProperty.call(children, node.name);
|
|
151
|
-
if (isString(child)) {
|
|
152
|
-
const value = i18n.services.interpolator.interpolate(child, opts, i18n.language);
|
|
153
|
-
mem.push(value);
|
|
154
|
-
} else if (hasChildren(child) || isValidTranslationWithChildren) {
|
|
155
|
-
const inner = renderInner(child, node, rootReactNode);
|
|
156
|
-
pushTranslatedJSX(child, inner, mem, i);
|
|
157
|
-
} else if (isEmptyTransWithHTML) {
|
|
158
|
-
const inner = mapAST(reactNodes, node.children, rootReactNode);
|
|
159
|
-
pushTranslatedJSX(child, inner, mem, i);
|
|
160
|
-
} else if (Number.isNaN(parseFloat(node.name))) {
|
|
161
|
-
if (isKnownComponent) {
|
|
162
|
-
const inner = renderInner(child, node, rootReactNode);
|
|
163
|
-
pushTranslatedJSX(child, inner, mem, i, node.voidElement);
|
|
164
|
-
} else if (i18nOptions.transSupportBasicHtmlNodes && keepArray.indexOf(node.name) > -1) {
|
|
165
|
-
if (node.voidElement) {
|
|
166
|
-
mem.push(createElement(node.name, {
|
|
167
|
-
key: `${node.name}-${i}`
|
|
168
|
-
}));
|
|
169
|
-
} else {
|
|
170
|
-
const inner = mapAST(reactNodes, node.children, rootReactNode);
|
|
171
|
-
mem.push(createElement(node.name, {
|
|
172
|
-
key: `${node.name}-${i}`
|
|
173
|
-
}, inner));
|
|
174
|
-
}
|
|
175
|
-
} else if (node.voidElement) {
|
|
176
|
-
mem.push(`<${node.name} />`);
|
|
177
|
-
} else {
|
|
178
|
-
const inner = mapAST(reactNodes, node.children, rootReactNode);
|
|
179
|
-
mem.push(`<${node.name}>${inner}</${node.name}>`);
|
|
180
|
-
}
|
|
181
|
-
} else if (isObject(child) && !isElement) {
|
|
182
|
-
const content = node.children[0] ? translationContent : null;
|
|
183
|
-
if (content) mem.push(content);
|
|
184
|
-
} else {
|
|
185
|
-
pushTranslatedJSX(child, translationContent, mem, i, node.children.length !== 1 || !translationContent);
|
|
186
|
-
}
|
|
187
|
-
} else if (node.type === 'text') {
|
|
188
|
-
const wrapTextNodes = i18nOptions.transWrapTextNodes;
|
|
189
|
-
const content = shouldUnescape ? i18nOptions.unescape(i18n.services.interpolator.interpolate(node.content, opts, i18n.language)) : i18n.services.interpolator.interpolate(node.content, opts, i18n.language);
|
|
190
|
-
if (wrapTextNodes) {
|
|
191
|
-
mem.push(createElement(wrapTextNodes, {
|
|
192
|
-
key: `${node.name}-${i}`
|
|
193
|
-
}, content));
|
|
194
|
-
} else {
|
|
195
|
-
mem.push(content);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
return mem;
|
|
199
|
-
}, []);
|
|
200
|
-
};
|
|
201
|
-
const result = mapAST([{
|
|
202
|
-
dummy: true,
|
|
203
|
-
children: children || []
|
|
204
|
-
}], ast, getAsArray(children || []));
|
|
205
|
-
return getChildren(result[0]);
|
|
206
|
-
};
|
|
207
|
-
const fixComponentProps = (component, index, translation) => {
|
|
208
|
-
const componentKey = component.key || index;
|
|
209
|
-
const comp = cloneElement(component, {
|
|
210
|
-
key: componentKey
|
|
211
|
-
});
|
|
212
|
-
if (!comp.props || !comp.props.children || translation.indexOf(`${index}/>`) < 0 && translation.indexOf(`${index} />`) < 0) {
|
|
213
|
-
return comp;
|
|
214
|
-
}
|
|
215
|
-
function Componentized() {
|
|
216
|
-
return createElement(Fragment, null, comp);
|
|
217
|
-
}
|
|
218
|
-
return createElement(Componentized, {
|
|
219
|
-
key: componentKey
|
|
220
|
-
});
|
|
221
|
-
};
|
|
222
|
-
const generateArrayComponents = (components, translation) => components.map((c, index) => fixComponentProps(c, index, translation));
|
|
223
|
-
const generateObjectComponents = (components, translation) => {
|
|
224
|
-
const componentMap = {};
|
|
225
|
-
Object.keys(components).forEach(c => {
|
|
226
|
-
Object.assign(componentMap, {
|
|
227
|
-
[c]: fixComponentProps(components[c], c, translation)
|
|
228
|
-
});
|
|
229
|
-
});
|
|
230
|
-
return componentMap;
|
|
231
|
-
};
|
|
232
|
-
const generateComponents = (components, translation, i18n, i18nKey) => {
|
|
233
|
-
if (!components) return null;
|
|
234
|
-
if (Array.isArray(components)) {
|
|
235
|
-
return generateArrayComponents(components, translation);
|
|
236
|
-
}
|
|
237
|
-
if (isObject(components)) {
|
|
238
|
-
return generateObjectComponents(components, translation);
|
|
239
|
-
}
|
|
240
|
-
warnOnce(i18n, 'TRANS_INVALID_COMPONENTS', `<Trans /> "components" prop expects an object or array`, {
|
|
241
|
-
i18nKey
|
|
242
|
-
});
|
|
243
|
-
return null;
|
|
244
|
-
};
|
|
245
|
-
function Trans({
|
|
246
|
-
children,
|
|
247
|
-
count,
|
|
248
|
-
parent,
|
|
249
|
-
i18nKey,
|
|
250
|
-
context,
|
|
251
|
-
tOptions = {},
|
|
252
|
-
values,
|
|
253
|
-
defaults,
|
|
254
|
-
components,
|
|
255
|
-
ns,
|
|
256
|
-
i18n: i18nFromProps,
|
|
257
|
-
t: tFromProps,
|
|
258
|
-
shouldUnescape,
|
|
259
|
-
...additionalProps
|
|
260
|
-
}) {
|
|
261
|
-
const i18n = i18nFromProps || getI18n();
|
|
262
|
-
if (!i18n) {
|
|
263
|
-
warnOnce(i18n, 'NO_I18NEXT_INSTANCE', `Trans: You need to pass in an i18next instance using i18nextReactModule`, {
|
|
264
|
-
i18nKey
|
|
265
|
-
});
|
|
266
|
-
return children;
|
|
267
|
-
}
|
|
268
|
-
const t = tFromProps || i18n.t.bind(i18n) || (k => k);
|
|
269
|
-
const reactI18nextOptions = {
|
|
270
|
-
...getDefaults(),
|
|
271
|
-
...i18n.options?.react
|
|
272
|
-
};
|
|
273
|
-
let namespaces = ns || t.ns || i18n.options?.defaultNS;
|
|
274
|
-
namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
|
|
275
|
-
const nodeAsString = nodesToString(children, reactI18nextOptions, i18n, i18nKey);
|
|
276
|
-
const defaultValue = defaults || nodeAsString || reactI18nextOptions.transEmptyNodeValue || i18nKey;
|
|
277
|
-
const {
|
|
278
|
-
hashTransKey
|
|
279
|
-
} = reactI18nextOptions;
|
|
280
|
-
const key = i18nKey || (hashTransKey ? hashTransKey(nodeAsString || defaultValue) : nodeAsString || defaultValue);
|
|
281
|
-
if (i18n.options?.interpolation?.defaultVariables) {
|
|
282
|
-
values = values && Object.keys(values).length > 0 ? {
|
|
283
|
-
...values,
|
|
284
|
-
...i18n.options.interpolation.defaultVariables
|
|
285
|
-
} : {
|
|
286
|
-
...i18n.options.interpolation.defaultVariables
|
|
287
|
-
};
|
|
288
|
-
}
|
|
289
|
-
const interpolationOverride = values || count !== undefined && !i18n.options?.interpolation?.alwaysFormat || !children ? tOptions.interpolation : {
|
|
290
|
-
interpolation: {
|
|
291
|
-
...tOptions.interpolation,
|
|
292
|
-
prefix: '#$?',
|
|
293
|
-
suffix: '?$#'
|
|
294
|
-
}
|
|
295
|
-
};
|
|
296
|
-
const combinedTOpts = {
|
|
297
|
-
...tOptions,
|
|
298
|
-
context: context || tOptions.context,
|
|
299
|
-
count,
|
|
300
|
-
...values,
|
|
301
|
-
...interpolationOverride,
|
|
302
|
-
defaultValue,
|
|
303
|
-
ns: namespaces
|
|
304
|
-
};
|
|
305
|
-
const translation = key ? t(key, combinedTOpts) : defaultValue;
|
|
306
|
-
const generatedComponents = generateComponents(components, translation, i18n, i18nKey);
|
|
307
|
-
const content = renderNodes(generatedComponents || children, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape);
|
|
308
|
-
const useAsParent = parent ?? reactI18nextOptions.defaultTransParent;
|
|
309
|
-
return useAsParent ? createElement(useAsParent, additionalProps, content) : content;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
export { Trans, nodesToString };
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { createContext } from 'react';
|
|
2
|
-
|
|
3
|
-
const I18nContext = createContext();
|
|
4
|
-
class ReportNamespaces {
|
|
5
|
-
constructor() {
|
|
6
|
-
this.usedNamespaces = {};
|
|
7
|
-
}
|
|
8
|
-
addUsedNamespaces(namespaces) {
|
|
9
|
-
namespaces.forEach(ns => {
|
|
10
|
-
if (!this.usedNamespaces[ns]) this.usedNamespaces[ns] = true;
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
getUsedNamespaces() {
|
|
14
|
-
return Object.keys(this.usedNamespaces);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export { I18nContext, ReportNamespaces };
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { unescape } from './unescape.js';
|
|
2
|
-
|
|
3
|
-
let defaultOptions = {
|
|
4
|
-
bindI18n: 'languageChanged',
|
|
5
|
-
bindI18nStore: '',
|
|
6
|
-
transEmptyNodeValue: '',
|
|
7
|
-
transSupportBasicHtmlNodes: true,
|
|
8
|
-
transWrapTextNodes: '',
|
|
9
|
-
transKeepBasicHtmlNodesFor: ['br', 'strong', 'i', 'p'],
|
|
10
|
-
useSuspense: true,
|
|
11
|
-
unescape
|
|
12
|
-
};
|
|
13
|
-
const setDefaults = (options = {}) => {
|
|
14
|
-
defaultOptions = {
|
|
15
|
-
...defaultOptions,
|
|
16
|
-
...options
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
const getDefaults = () => defaultOptions;
|
|
20
|
-
|
|
21
|
-
export { getDefaults, setDefaults };
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { setDefaults } from './defaults.js';
|
|
2
|
-
import { setI18n } from './i18nInstance.js';
|
|
3
|
-
|
|
4
|
-
const initReactI18next = {
|
|
5
|
-
type: '3rdParty',
|
|
6
|
-
init(instance) {
|
|
7
|
-
setDefaults(instance.options.react);
|
|
8
|
-
setI18n(instance);
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export { initReactI18next };
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
const matchHtmlEntity = /&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34|nbsp|#160|copy|#169|reg|#174|hellip|#8230|#x2F|#47);/g;
|
|
2
|
-
const htmlEntities = {
|
|
3
|
-
'&': '&',
|
|
4
|
-
'&': '&',
|
|
5
|
-
'<': '<',
|
|
6
|
-
'<': '<',
|
|
7
|
-
'>': '>',
|
|
8
|
-
'>': '>',
|
|
9
|
-
''': "'",
|
|
10
|
-
''': "'",
|
|
11
|
-
'"': '"',
|
|
12
|
-
'"': '"',
|
|
13
|
-
' ': ' ',
|
|
14
|
-
' ': ' ',
|
|
15
|
-
'©': '©',
|
|
16
|
-
'©': '©',
|
|
17
|
-
'®': '®',
|
|
18
|
-
'®': '®',
|
|
19
|
-
'…': '…',
|
|
20
|
-
'…': '…',
|
|
21
|
-
'/': '/',
|
|
22
|
-
'/': '/'
|
|
23
|
-
};
|
|
24
|
-
const unescapeHtmlEntity = m => htmlEntities[m];
|
|
25
|
-
const unescape = text => text.replace(matchHtmlEntity, unescapeHtmlEntity);
|
|
26
|
-
|
|
27
|
-
export { unescape };
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { useContext, useState, useRef, useEffect, useCallback } from 'react';
|
|
2
|
-
import { I18nContext, ReportNamespaces } from './context.js';
|
|
3
|
-
import { warnOnce, hasLoadedNamespace, loadLanguages, loadNamespaces, isString, isObject } from './utils.js';
|
|
4
|
-
import { getI18n } from './i18nInstance.js';
|
|
5
|
-
import { getDefaults } from './defaults.js';
|
|
6
|
-
|
|
7
|
-
const usePrevious = (value, ignore) => {
|
|
8
|
-
const ref = useRef();
|
|
9
|
-
useEffect(() => {
|
|
10
|
-
ref.current = ignore ? ref.current : value;
|
|
11
|
-
}, [value, ignore]);
|
|
12
|
-
return ref.current;
|
|
13
|
-
};
|
|
14
|
-
const alwaysNewT = (i18n, language, namespace, keyPrefix) => i18n.getFixedT(language, namespace, keyPrefix);
|
|
15
|
-
const useMemoizedT = (i18n, language, namespace, keyPrefix) => useCallback(alwaysNewT(i18n, language, namespace, keyPrefix), [i18n, language, namespace, keyPrefix]);
|
|
16
|
-
const useTranslation = (ns, props = {}) => {
|
|
17
|
-
const {
|
|
18
|
-
i18n: i18nFromProps
|
|
19
|
-
} = props;
|
|
20
|
-
const {
|
|
21
|
-
i18n: i18nFromContext,
|
|
22
|
-
defaultNS: defaultNSFromContext
|
|
23
|
-
} = useContext(I18nContext) || {};
|
|
24
|
-
const i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
25
|
-
if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new ReportNamespaces();
|
|
26
|
-
if (!i18n) {
|
|
27
|
-
warnOnce(i18n, 'NO_I18NEXT_INSTANCE', 'useTranslation: You will need to pass in an i18next instance by using initReactI18next');
|
|
28
|
-
const notReadyT = (k, optsOrDefaultValue) => {
|
|
29
|
-
if (isString(optsOrDefaultValue)) return optsOrDefaultValue;
|
|
30
|
-
if (isObject(optsOrDefaultValue) && isString(optsOrDefaultValue.defaultValue)) return optsOrDefaultValue.defaultValue;
|
|
31
|
-
return Array.isArray(k) ? k[k.length - 1] : k;
|
|
32
|
-
};
|
|
33
|
-
const retNotReady = [notReadyT, {}, false];
|
|
34
|
-
retNotReady.t = notReadyT;
|
|
35
|
-
retNotReady.i18n = {};
|
|
36
|
-
retNotReady.ready = false;
|
|
37
|
-
return retNotReady;
|
|
38
|
-
}
|
|
39
|
-
if (i18n.options.react?.wait) warnOnce(i18n, 'DEPRECATED_OPTION', 'useTranslation: It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.');
|
|
40
|
-
const i18nOptions = {
|
|
41
|
-
...getDefaults(),
|
|
42
|
-
...i18n.options.react,
|
|
43
|
-
...props
|
|
44
|
-
};
|
|
45
|
-
const {
|
|
46
|
-
useSuspense,
|
|
47
|
-
keyPrefix
|
|
48
|
-
} = i18nOptions;
|
|
49
|
-
let namespaces = ns || defaultNSFromContext || i18n.options?.defaultNS;
|
|
50
|
-
namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
|
|
51
|
-
i18n.reportNamespaces.addUsedNamespaces?.(namespaces);
|
|
52
|
-
const ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(n => hasLoadedNamespace(n, i18n, i18nOptions));
|
|
53
|
-
const memoGetT = useMemoizedT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
|
|
54
|
-
const getT = () => memoGetT;
|
|
55
|
-
const getNewT = () => alwaysNewT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
|
|
56
|
-
const [t, setT] = useState(getT);
|
|
57
|
-
let joinedNS = namespaces.join();
|
|
58
|
-
if (props.lng) joinedNS = `${props.lng}${joinedNS}`;
|
|
59
|
-
const previousJoinedNS = usePrevious(joinedNS);
|
|
60
|
-
const isMounted = useRef(true);
|
|
61
|
-
useEffect(() => {
|
|
62
|
-
const {
|
|
63
|
-
bindI18n,
|
|
64
|
-
bindI18nStore
|
|
65
|
-
} = i18nOptions;
|
|
66
|
-
isMounted.current = true;
|
|
67
|
-
if (!ready && !useSuspense) {
|
|
68
|
-
if (props.lng) {
|
|
69
|
-
loadLanguages(i18n, props.lng, namespaces, () => {
|
|
70
|
-
if (isMounted.current) setT(getNewT);
|
|
71
|
-
});
|
|
72
|
-
} else {
|
|
73
|
-
loadNamespaces(i18n, namespaces, () => {
|
|
74
|
-
if (isMounted.current) setT(getNewT);
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
if (ready && previousJoinedNS && previousJoinedNS !== joinedNS && isMounted.current) {
|
|
79
|
-
setT(getNewT);
|
|
80
|
-
}
|
|
81
|
-
const boundReset = () => {
|
|
82
|
-
if (isMounted.current) setT(getNewT);
|
|
83
|
-
};
|
|
84
|
-
if (bindI18n) i18n?.on(bindI18n, boundReset);
|
|
85
|
-
if (bindI18nStore) i18n?.store.on(bindI18nStore, boundReset);
|
|
86
|
-
return () => {
|
|
87
|
-
isMounted.current = false;
|
|
88
|
-
if (i18n) bindI18n?.split(' ').forEach(e => i18n.off(e, boundReset));
|
|
89
|
-
if (bindI18nStore && i18n) bindI18nStore.split(' ').forEach(e => i18n.store.off(e, boundReset));
|
|
90
|
-
};
|
|
91
|
-
}, [i18n, joinedNS]);
|
|
92
|
-
useEffect(() => {
|
|
93
|
-
if (isMounted.current && ready) {
|
|
94
|
-
setT(getT);
|
|
95
|
-
}
|
|
96
|
-
}, [i18n, keyPrefix, ready]);
|
|
97
|
-
const ret = [t, i18n, ready];
|
|
98
|
-
ret.t = t;
|
|
99
|
-
ret.i18n = i18n;
|
|
100
|
-
ret.ready = ready;
|
|
101
|
-
if (ready) return ret;
|
|
102
|
-
if (!ready && !useSuspense) return ret;
|
|
103
|
-
throw new Promise(resolve => {
|
|
104
|
-
if (props.lng) {
|
|
105
|
-
loadLanguages(i18n, props.lng, namespaces, () => resolve());
|
|
106
|
-
} else {
|
|
107
|
-
loadNamespaces(i18n, namespaces, () => resolve());
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
export { useTranslation };
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
const warn = (i18n, code, msg, rest) => {
|
|
2
|
-
const args = [msg, {
|
|
3
|
-
code,
|
|
4
|
-
...(rest || {})
|
|
5
|
-
}];
|
|
6
|
-
if (i18n?.services?.logger?.forward) {
|
|
7
|
-
return i18n.services.logger.forward(args, 'warn', 'react-i18next::', true);
|
|
8
|
-
}
|
|
9
|
-
if (isString(args[0])) args[0] = `react-i18next:: ${args[0]}`;
|
|
10
|
-
if (i18n?.services?.logger?.warn) {
|
|
11
|
-
i18n.services.logger.warn(...args);
|
|
12
|
-
} else if (console?.warn) {
|
|
13
|
-
console.warn(...args);
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
const alreadyWarned = {};
|
|
17
|
-
const warnOnce = (i18n, code, msg, rest) => {
|
|
18
|
-
if (isString(msg) && alreadyWarned[msg]) return;
|
|
19
|
-
if (isString(msg)) alreadyWarned[msg] = new Date();
|
|
20
|
-
warn(i18n, code, msg, rest);
|
|
21
|
-
};
|
|
22
|
-
const loadedClb = (i18n, cb) => () => {
|
|
23
|
-
if (i18n.isInitialized) {
|
|
24
|
-
cb();
|
|
25
|
-
} else {
|
|
26
|
-
const initialized = () => {
|
|
27
|
-
setTimeout(() => {
|
|
28
|
-
i18n.off('initialized', initialized);
|
|
29
|
-
}, 0);
|
|
30
|
-
cb();
|
|
31
|
-
};
|
|
32
|
-
i18n.on('initialized', initialized);
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
const loadNamespaces = (i18n, ns, cb) => {
|
|
36
|
-
i18n.loadNamespaces(ns, loadedClb(i18n, cb));
|
|
37
|
-
};
|
|
38
|
-
const loadLanguages = (i18n, lng, ns, cb) => {
|
|
39
|
-
if (isString(ns)) ns = [ns];
|
|
40
|
-
if (i18n.options.preload && i18n.options.preload.indexOf(lng) > -1) return loadNamespaces(i18n, ns, cb);
|
|
41
|
-
ns.forEach(n => {
|
|
42
|
-
if (i18n.options.ns.indexOf(n) < 0) i18n.options.ns.push(n);
|
|
43
|
-
});
|
|
44
|
-
i18n.loadLanguages(lng, loadedClb(i18n, cb));
|
|
45
|
-
};
|
|
46
|
-
const hasLoadedNamespace = (ns, i18n, options = {}) => {
|
|
47
|
-
if (!i18n.languages || !i18n.languages.length) {
|
|
48
|
-
warnOnce(i18n, 'NO_LANGUAGES', 'i18n.languages were undefined or empty', {
|
|
49
|
-
languages: i18n.languages
|
|
50
|
-
});
|
|
51
|
-
return true;
|
|
52
|
-
}
|
|
53
|
-
return i18n.hasLoadedNamespace(ns, {
|
|
54
|
-
lng: options.lng,
|
|
55
|
-
precheck: (i18nInstance, loadNotPending) => {
|
|
56
|
-
if (options.bindI18n?.indexOf('languageChanging') > -1 && i18nInstance.services.backendConnector.backend && i18nInstance.isLanguageChangingTo && !loadNotPending(i18nInstance.isLanguageChangingTo, ns)) return false;
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
};
|
|
60
|
-
const isString = obj => typeof obj === 'string';
|
|
61
|
-
const isObject = obj => typeof obj === 'object' && obj !== null;
|
|
62
|
-
|
|
63
|
-
export { hasLoadedNamespace, isObject, isString, loadLanguages, loadNamespaces, warn, warnOnce };
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file automatically generated from `pre-publish.js`.
|
|
3
|
-
* Do not manually edit.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
var voidElements = {
|
|
7
|
-
"area": true,
|
|
8
|
-
"base": true,
|
|
9
|
-
"br": true,
|
|
10
|
-
"col": true,
|
|
11
|
-
"embed": true,
|
|
12
|
-
"hr": true,
|
|
13
|
-
"img": true,
|
|
14
|
-
"input": true,
|
|
15
|
-
"link": true,
|
|
16
|
-
"meta": true,
|
|
17
|
-
"param": true,
|
|
18
|
-
"source": true,
|
|
19
|
-
"track": true,
|
|
20
|
-
"wbr": true
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export { voidElements as default };
|