@nimpl/i18n 0.0.0-experimental-0b32faf → 0.0.0-experimental-3abcca7
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/lib/Translation.js +47 -11
- package/dist/lib/injectQuery.js +24 -11
- package/package.json +1 -1
package/dist/lib/Translation.js
CHANGED
|
@@ -5,16 +5,50 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
};
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
const react_1 = __importDefault(require("react"));
|
|
8
|
+
const parseTag = (tag) => {
|
|
9
|
+
const isSelfClosing = tag.endsWith("/>");
|
|
10
|
+
const isClosing = tag[1] === "/";
|
|
11
|
+
const start = isClosing ? 2 : 1;
|
|
12
|
+
const end = isSelfClosing
|
|
13
|
+
? tag.indexOf(" ") !== -1 && tag.indexOf(" ") < tag.length - 2
|
|
14
|
+
? tag.indexOf(" ")
|
|
15
|
+
: tag.length - 2
|
|
16
|
+
: tag.length - 1;
|
|
17
|
+
return {
|
|
18
|
+
name: tag.slice(start, end),
|
|
19
|
+
type: isSelfClosing ? "self" : isClosing ? "close" : "open",
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
const splitByTags = (text) => {
|
|
23
|
+
const parts = [];
|
|
24
|
+
const tags = [];
|
|
25
|
+
let current = "";
|
|
26
|
+
let i = 0;
|
|
27
|
+
while (i < text.length) {
|
|
28
|
+
if (text[i] === "<") {
|
|
29
|
+
const closeIndex = text.indexOf(">", i);
|
|
30
|
+
if (closeIndex !== -1) {
|
|
31
|
+
parts.push(current);
|
|
32
|
+
current = "";
|
|
33
|
+
tags.push(text.slice(i, closeIndex + 1));
|
|
34
|
+
i = closeIndex + 1;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
current += text[i];
|
|
39
|
+
i++;
|
|
40
|
+
}
|
|
41
|
+
parts.push(current);
|
|
42
|
+
return { parts, tags };
|
|
43
|
+
};
|
|
8
44
|
const Translation = ({ term, text, components }) => {
|
|
9
|
-
const parts = text
|
|
10
|
-
|
|
11
|
-
.map((el, i) => react_1.default.createElement(react_1.default.Fragment, { key: `p-${i}` }, el));
|
|
45
|
+
const { parts: textParts, tags } = splitByTags(text);
|
|
46
|
+
const parts = textParts.map((el, i) => react_1.default.createElement(react_1.default.Fragment, { key: `p-${i}` }, el));
|
|
12
47
|
if (components) {
|
|
13
|
-
const tags = text.match(/<\/?[a-zA-Z0-9]+>|<[a-zA-Z0-9]+ ?\/>/gm);
|
|
14
48
|
const openedTags = [];
|
|
15
|
-
tags
|
|
16
|
-
const tagName = tag
|
|
17
|
-
if (
|
|
49
|
+
tags.forEach((tag, tagIndex) => {
|
|
50
|
+
const { name: tagName, type: tagType } = parseTag(tag);
|
|
51
|
+
if (tagType === "self") {
|
|
18
52
|
const component = components[tagName];
|
|
19
53
|
if (component) {
|
|
20
54
|
parts.splice(tagIndex + 1, 1, react_1.default.cloneElement(component, { key: `c-${tagIndex}` }, component.props.children));
|
|
@@ -23,7 +57,7 @@ const Translation = ({ term, text, components }) => {
|
|
|
23
57
|
console.warn(`Unknown component for term "${term}" - ${tagName}`);
|
|
24
58
|
}
|
|
25
59
|
}
|
|
26
|
-
else if (
|
|
60
|
+
else if (tagType === "close") {
|
|
27
61
|
const openedTagIndex = openedTags.findIndex((i) => i.tag === tagName);
|
|
28
62
|
if (openedTagIndex !== -1) {
|
|
29
63
|
const lastOpenedIndex = openedTags.length - 1 - openedTagIndex;
|
|
@@ -35,9 +69,11 @@ const Translation = ({ term, text, components }) => {
|
|
|
35
69
|
if (component) {
|
|
36
70
|
const children = parts
|
|
37
71
|
.slice(targetTag.position + 1, tagIndex + 1)
|
|
38
|
-
.filter((
|
|
39
|
-
|
|
40
|
-
|
|
72
|
+
.filter((child) => Boolean(child &&
|
|
73
|
+
typeof child === "object" &&
|
|
74
|
+
"props" in child &&
|
|
75
|
+
child.props.children));
|
|
76
|
+
parts.splice(targetTag.position + 1, tagIndex - targetTag.position, react_1.default.cloneElement(component, { key: `${tagIndex}-${targetIndex}` }, children.length ? children : component.props.children));
|
|
41
77
|
}
|
|
42
78
|
else {
|
|
43
79
|
console.warn(`Unknown component for term "${term}" - ${targetTag}`);
|
package/dist/lib/injectQuery.js
CHANGED
|
@@ -1,17 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const injectQuery = ({ text, query, removeUnusedQueries }) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
let result = "";
|
|
5
|
+
let i = 0;
|
|
6
|
+
while (i < text.length) {
|
|
7
|
+
if (text[i] === "{" && text[i + 1] === "{") {
|
|
8
|
+
const closeIndex = text.indexOf("}}", i + 2);
|
|
9
|
+
if (closeIndex !== -1) {
|
|
10
|
+
const key = text.slice(i + 2, closeIndex);
|
|
11
|
+
const value = query[key];
|
|
12
|
+
if (value !== undefined) {
|
|
13
|
+
result += value.toString();
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
console.warn(`Query key "${key}" not found in options for "${text}"`);
|
|
17
|
+
if (!removeUnusedQueries) {
|
|
18
|
+
result += text.slice(i, closeIndex + 2);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
i = closeIndex + 2;
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
7
24
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return matched;
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
return newText;
|
|
25
|
+
result += text[i];
|
|
26
|
+
i++;
|
|
27
|
+
}
|
|
28
|
+
return result;
|
|
16
29
|
};
|
|
17
30
|
exports.default = injectQuery;
|
package/package.json
CHANGED