react-i18next 16.0.1 → 16.1.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/CHANGELOG.md +4 -0
- package/dist/commonjs/IcuTrans.js +35 -0
- package/dist/commonjs/IcuTransUtils/TranslationParserError.js +18 -0
- package/dist/commonjs/IcuTransUtils/htmlEntityDecoder.js +218 -0
- package/dist/commonjs/IcuTransUtils/index.js +49 -0
- package/dist/commonjs/IcuTransUtils/renderTranslation.js +114 -0
- package/dist/commonjs/IcuTransUtils/tokenizer.js +58 -0
- package/dist/commonjs/IcuTransWithoutContext.js +56 -0
- package/dist/es/IcuTrans.js +29 -0
- package/dist/es/IcuTransUtils/TranslationParserError.js +11 -0
- package/dist/es/IcuTransUtils/htmlEntityDecoder.js +211 -0
- package/dist/es/IcuTransUtils/index.js +4 -0
- package/dist/es/IcuTransUtils/renderTranslation.js +106 -0
- package/dist/es/IcuTransUtils/tokenizer.js +51 -0
- package/dist/es/IcuTransWithoutContext.js +49 -0
- package/dist/es/package.json +1 -1
- package/icu.macro.js +170 -48
- package/package.json +6 -3
- package/src/IcuTrans.js +103 -0
- package/src/IcuTransUtils/TranslationParserError.js +24 -0
- package/src/IcuTransUtils/htmlEntityDecoder.js +264 -0
- package/src/IcuTransUtils/index.js +4 -0
- package/src/IcuTransUtils/renderTranslation.js +215 -0
- package/src/IcuTransUtils/tokenizer.js +78 -0
- package/src/IcuTransWithoutContext.js +146 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
### 16.1.0
|
|
2
|
+
|
|
3
|
+
- Introduce `IcuTrans` component [1869](https://github.com/i18next/react-i18next/issues/1869)
|
|
4
|
+
|
|
1
5
|
### 16.0.1
|
|
2
6
|
|
|
3
7
|
- fix: Using <Trans> component with named tags throws error when Selector API is enabled [1867](https://github.com/i18next/react-i18next/issues/1867) with [1868](https://github.com/i18next/react-i18next/pull/1868)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.IcuTrans = IcuTrans;
|
|
7
|
+
var _react = require("react");
|
|
8
|
+
var _IcuTransWithoutContext = require("./IcuTransWithoutContext.js");
|
|
9
|
+
var _context = require("./context.js");
|
|
10
|
+
function IcuTrans({
|
|
11
|
+
i18nKey,
|
|
12
|
+
defaultTranslation,
|
|
13
|
+
content,
|
|
14
|
+
ns,
|
|
15
|
+
values = {},
|
|
16
|
+
i18n: i18nFromProps,
|
|
17
|
+
t: tFromProps
|
|
18
|
+
}) {
|
|
19
|
+
const {
|
|
20
|
+
i18n: i18nFromContext,
|
|
21
|
+
defaultNS: defaultNSFromContext
|
|
22
|
+
} = (0, _react.useContext)(_context.I18nContext) || {};
|
|
23
|
+
const i18n = i18nFromProps || i18nFromContext || (0, _context.getI18n)();
|
|
24
|
+
const t = tFromProps || i18n?.t.bind(i18n);
|
|
25
|
+
return (0, _IcuTransWithoutContext.IcuTransWithoutContext)({
|
|
26
|
+
i18nKey,
|
|
27
|
+
defaultTranslation,
|
|
28
|
+
content,
|
|
29
|
+
ns: ns || t?.ns || defaultNSFromContext || i18n?.options?.defaultNS,
|
|
30
|
+
values,
|
|
31
|
+
i18n,
|
|
32
|
+
t: tFromProps
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
IcuTrans.displayName = 'IcuTrans';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.TranslationParserError = void 0;
|
|
7
|
+
class TranslationParserError extends Error {
|
|
8
|
+
constructor(message, position, translationString) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'TranslationParserError';
|
|
11
|
+
this.position = position;
|
|
12
|
+
this.translationString = translationString;
|
|
13
|
+
if (Error.captureStackTrace) {
|
|
14
|
+
Error.captureStackTrace(this, TranslationParserError);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.TranslationParserError = TranslationParserError;
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.decodeHtmlEntities = void 0;
|
|
7
|
+
const commonEntities = {
|
|
8
|
+
' ': '\u00A0',
|
|
9
|
+
'&': '&',
|
|
10
|
+
'<': '<',
|
|
11
|
+
'>': '>',
|
|
12
|
+
'"': '"',
|
|
13
|
+
''': "'",
|
|
14
|
+
'©': '©',
|
|
15
|
+
'®': '®',
|
|
16
|
+
'™': '™',
|
|
17
|
+
'…': '…',
|
|
18
|
+
'–': '–',
|
|
19
|
+
'—': '—',
|
|
20
|
+
'‘': '\u2018',
|
|
21
|
+
'’': '\u2019',
|
|
22
|
+
'‚': '\u201A',
|
|
23
|
+
'“': '\u201C',
|
|
24
|
+
'”': '\u201D',
|
|
25
|
+
'„': '\u201E',
|
|
26
|
+
'†': '†',
|
|
27
|
+
'‡': '‡',
|
|
28
|
+
'•': '•',
|
|
29
|
+
'′': '′',
|
|
30
|
+
'″': '″',
|
|
31
|
+
'‹': '‹',
|
|
32
|
+
'›': '›',
|
|
33
|
+
'§': '§',
|
|
34
|
+
'¶': '¶',
|
|
35
|
+
'·': '·',
|
|
36
|
+
' ': '\u2002',
|
|
37
|
+
' ': '\u2003',
|
|
38
|
+
' ': '\u2009',
|
|
39
|
+
'€': '€',
|
|
40
|
+
'£': '£',
|
|
41
|
+
'¥': '¥',
|
|
42
|
+
'¢': '¢',
|
|
43
|
+
'¤': '¤',
|
|
44
|
+
'×': '×',
|
|
45
|
+
'÷': '÷',
|
|
46
|
+
'−': '−',
|
|
47
|
+
'±': '±',
|
|
48
|
+
'≠': '≠',
|
|
49
|
+
'≤': '≤',
|
|
50
|
+
'≥': '≥',
|
|
51
|
+
'≈': '≈',
|
|
52
|
+
'≡': '≡',
|
|
53
|
+
'∞': '∞',
|
|
54
|
+
'∫': '∫',
|
|
55
|
+
'∑': '∑',
|
|
56
|
+
'∏': '∏',
|
|
57
|
+
'√': '√',
|
|
58
|
+
'∂': '∂',
|
|
59
|
+
'‰': '‰',
|
|
60
|
+
'°': '°',
|
|
61
|
+
'µ': 'µ',
|
|
62
|
+
'←': '←',
|
|
63
|
+
'↑': '↑',
|
|
64
|
+
'→': '→',
|
|
65
|
+
'↓': '↓',
|
|
66
|
+
'↔': '↔',
|
|
67
|
+
'↵': '↵',
|
|
68
|
+
'⇐': '⇐',
|
|
69
|
+
'⇑': '⇑',
|
|
70
|
+
'⇒': '⇒',
|
|
71
|
+
'⇓': '⇓',
|
|
72
|
+
'⇔': '⇔',
|
|
73
|
+
'α': 'α',
|
|
74
|
+
'β': 'β',
|
|
75
|
+
'γ': 'γ',
|
|
76
|
+
'δ': 'δ',
|
|
77
|
+
'ε': 'ε',
|
|
78
|
+
'ζ': 'ζ',
|
|
79
|
+
'η': 'η',
|
|
80
|
+
'θ': 'θ',
|
|
81
|
+
'ι': 'ι',
|
|
82
|
+
'κ': 'κ',
|
|
83
|
+
'λ': 'λ',
|
|
84
|
+
'μ': 'μ',
|
|
85
|
+
'ν': 'ν',
|
|
86
|
+
'ξ': 'ξ',
|
|
87
|
+
'ο': 'ο',
|
|
88
|
+
'π': 'π',
|
|
89
|
+
'ρ': 'ρ',
|
|
90
|
+
'σ': 'σ',
|
|
91
|
+
'τ': 'τ',
|
|
92
|
+
'υ': 'υ',
|
|
93
|
+
'φ': 'φ',
|
|
94
|
+
'χ': 'χ',
|
|
95
|
+
'ψ': 'ψ',
|
|
96
|
+
'ω': 'ω',
|
|
97
|
+
'Α': 'Α',
|
|
98
|
+
'Β': 'Β',
|
|
99
|
+
'Γ': 'Γ',
|
|
100
|
+
'Δ': 'Δ',
|
|
101
|
+
'Ε': 'Ε',
|
|
102
|
+
'Ζ': 'Ζ',
|
|
103
|
+
'Η': 'Η',
|
|
104
|
+
'Θ': 'Θ',
|
|
105
|
+
'Ι': 'Ι',
|
|
106
|
+
'Κ': 'Κ',
|
|
107
|
+
'Λ': 'Λ',
|
|
108
|
+
'Μ': 'Μ',
|
|
109
|
+
'Ν': 'Ν',
|
|
110
|
+
'Ξ': 'Ξ',
|
|
111
|
+
'Ο': 'Ο',
|
|
112
|
+
'Π': 'Π',
|
|
113
|
+
'Ρ': 'Ρ',
|
|
114
|
+
'Σ': 'Σ',
|
|
115
|
+
'Τ': 'Τ',
|
|
116
|
+
'Υ': 'Υ',
|
|
117
|
+
'Φ': 'Φ',
|
|
118
|
+
'Χ': 'Χ',
|
|
119
|
+
'Ψ': 'Ψ',
|
|
120
|
+
'Ω': 'Ω',
|
|
121
|
+
'À': 'À',
|
|
122
|
+
'Á': 'Á',
|
|
123
|
+
'Â': 'Â',
|
|
124
|
+
'Ã': 'Ã',
|
|
125
|
+
'Ä': 'Ä',
|
|
126
|
+
'Å': 'Å',
|
|
127
|
+
'Æ': 'Æ',
|
|
128
|
+
'Ç': 'Ç',
|
|
129
|
+
'È': 'È',
|
|
130
|
+
'É': 'É',
|
|
131
|
+
'Ê': 'Ê',
|
|
132
|
+
'Ë': 'Ë',
|
|
133
|
+
'Ì': 'Ì',
|
|
134
|
+
'Í': 'Í',
|
|
135
|
+
'Î': 'Î',
|
|
136
|
+
'Ï': 'Ï',
|
|
137
|
+
'Ð': 'Ð',
|
|
138
|
+
'Ñ': 'Ñ',
|
|
139
|
+
'Ò': 'Ò',
|
|
140
|
+
'Ó': 'Ó',
|
|
141
|
+
'Ô': 'Ô',
|
|
142
|
+
'Õ': 'Õ',
|
|
143
|
+
'Ö': 'Ö',
|
|
144
|
+
'Ø': 'Ø',
|
|
145
|
+
'Ù': 'Ù',
|
|
146
|
+
'Ú': 'Ú',
|
|
147
|
+
'Û': 'Û',
|
|
148
|
+
'Ü': 'Ü',
|
|
149
|
+
'Ý': 'Ý',
|
|
150
|
+
'Þ': 'Þ',
|
|
151
|
+
'ß': 'ß',
|
|
152
|
+
'à': 'à',
|
|
153
|
+
'á': 'á',
|
|
154
|
+
'â': 'â',
|
|
155
|
+
'ã': 'ã',
|
|
156
|
+
'ä': 'ä',
|
|
157
|
+
'å': 'å',
|
|
158
|
+
'æ': 'æ',
|
|
159
|
+
'ç': 'ç',
|
|
160
|
+
'è': 'è',
|
|
161
|
+
'é': 'é',
|
|
162
|
+
'ê': 'ê',
|
|
163
|
+
'ë': 'ë',
|
|
164
|
+
'ì': 'ì',
|
|
165
|
+
'í': 'í',
|
|
166
|
+
'î': 'î',
|
|
167
|
+
'ï': 'ï',
|
|
168
|
+
'ð': 'ð',
|
|
169
|
+
'ñ': 'ñ',
|
|
170
|
+
'ò': 'ò',
|
|
171
|
+
'ó': 'ó',
|
|
172
|
+
'ô': 'ô',
|
|
173
|
+
'õ': 'õ',
|
|
174
|
+
'ö': 'ö',
|
|
175
|
+
'ø': 'ø',
|
|
176
|
+
'ù': 'ù',
|
|
177
|
+
'ú': 'ú',
|
|
178
|
+
'û': 'û',
|
|
179
|
+
'ü': 'ü',
|
|
180
|
+
'ý': 'ý',
|
|
181
|
+
'þ': 'þ',
|
|
182
|
+
'ÿ': 'ÿ',
|
|
183
|
+
'¡': '¡',
|
|
184
|
+
'¿': '¿',
|
|
185
|
+
'ƒ': 'ƒ',
|
|
186
|
+
'ˆ': 'ˆ',
|
|
187
|
+
'˜': '˜',
|
|
188
|
+
'Œ': 'Œ',
|
|
189
|
+
'œ': 'œ',
|
|
190
|
+
'Š': 'Š',
|
|
191
|
+
'š': 'š',
|
|
192
|
+
'Ÿ': 'Ÿ',
|
|
193
|
+
'ª': 'ª',
|
|
194
|
+
'º': 'º',
|
|
195
|
+
'¯': '¯',
|
|
196
|
+
'´': '´',
|
|
197
|
+
'¸': '¸',
|
|
198
|
+
'¹': '¹',
|
|
199
|
+
'²': '²',
|
|
200
|
+
'³': '³',
|
|
201
|
+
'¼': '¼',
|
|
202
|
+
'½': '½',
|
|
203
|
+
'¾': '¾',
|
|
204
|
+
'♠': '♠',
|
|
205
|
+
'♣': '♣',
|
|
206
|
+
'♥': '♥',
|
|
207
|
+
'♦': '♦',
|
|
208
|
+
'◊': '◊',
|
|
209
|
+
'‾': '‾',
|
|
210
|
+
'⁄': '⁄',
|
|
211
|
+
'℘': '℘',
|
|
212
|
+
'ℑ': 'ℑ',
|
|
213
|
+
'ℜ': 'ℜ',
|
|
214
|
+
'ℵ': 'ℵ'
|
|
215
|
+
};
|
|
216
|
+
const entityPattern = new RegExp(Object.keys(commonEntities).map(entity => entity.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|'), 'g');
|
|
217
|
+
const decodeHtmlEntities = text => text.replace(entityPattern, match => commonEntities[match]).replace(/&#(\d+);/g, (_, num) => String.fromCharCode(parseInt(num, 10))).replace(/&#x([0-9a-fA-F]+);/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)));
|
|
218
|
+
exports.decodeHtmlEntities = decodeHtmlEntities;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _TranslationParserError = require("./TranslationParserError");
|
|
7
|
+
Object.keys(_TranslationParserError).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _TranslationParserError[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _TranslationParserError[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
var _htmlEntityDecoder = require("./htmlEntityDecoder");
|
|
18
|
+
Object.keys(_htmlEntityDecoder).forEach(function (key) {
|
|
19
|
+
if (key === "default" || key === "__esModule") return;
|
|
20
|
+
if (key in exports && exports[key] === _htmlEntityDecoder[key]) return;
|
|
21
|
+
Object.defineProperty(exports, key, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function () {
|
|
24
|
+
return _htmlEntityDecoder[key];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
var _tokenizer = require("./tokenizer");
|
|
29
|
+
Object.keys(_tokenizer).forEach(function (key) {
|
|
30
|
+
if (key === "default" || key === "__esModule") return;
|
|
31
|
+
if (key in exports && exports[key] === _tokenizer[key]) return;
|
|
32
|
+
Object.defineProperty(exports, key, {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function () {
|
|
35
|
+
return _tokenizer[key];
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
var _renderTranslation = require("./renderTranslation");
|
|
40
|
+
Object.keys(_renderTranslation).forEach(function (key) {
|
|
41
|
+
if (key === "default" || key === "__esModule") return;
|
|
42
|
+
if (key in exports && exports[key] === _renderTranslation[key]) return;
|
|
43
|
+
Object.defineProperty(exports, key, {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
get: function () {
|
|
46
|
+
return _renderTranslation[key];
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
});
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.renderTranslation = void 0;
|
|
8
|
+
var _react = _interopRequireDefault(require("react"));
|
|
9
|
+
var _TranslationParserError = require("./TranslationParserError");
|
|
10
|
+
var _tokenizer = require("./tokenizer");
|
|
11
|
+
var _htmlEntityDecoder = require("./htmlEntityDecoder");
|
|
12
|
+
const renderDeclarationNode = (declaration, children, childDeclarations) => {
|
|
13
|
+
const {
|
|
14
|
+
type,
|
|
15
|
+
props = {}
|
|
16
|
+
} = declaration;
|
|
17
|
+
if (props.children && Array.isArray(props.children) && childDeclarations) {
|
|
18
|
+
const {
|
|
19
|
+
children: _childrenToRemove,
|
|
20
|
+
...propsWithoutChildren
|
|
21
|
+
} = props;
|
|
22
|
+
return _react.default.createElement(type, propsWithoutChildren, ...children);
|
|
23
|
+
}
|
|
24
|
+
if (children.length === 0) {
|
|
25
|
+
return _react.default.createElement(type, props);
|
|
26
|
+
}
|
|
27
|
+
if (children.length === 1) {
|
|
28
|
+
return _react.default.createElement(type, props, children[0]);
|
|
29
|
+
}
|
|
30
|
+
return _react.default.createElement(type, props, ...children);
|
|
31
|
+
};
|
|
32
|
+
const renderTranslation = (translation, declarations = []) => {
|
|
33
|
+
if (!translation) {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
const tokens = (0, _tokenizer.tokenize)(translation);
|
|
37
|
+
const result = [];
|
|
38
|
+
const stack = [];
|
|
39
|
+
const literalTagNumbers = new Set();
|
|
40
|
+
const getCurrentDeclarations = () => {
|
|
41
|
+
if (stack.length === 0) {
|
|
42
|
+
return declarations;
|
|
43
|
+
}
|
|
44
|
+
const parentFrame = stack[stack.length - 1];
|
|
45
|
+
if (parentFrame.declaration.props?.children && Array.isArray(parentFrame.declaration.props.children)) {
|
|
46
|
+
return parentFrame.declaration.props.children;
|
|
47
|
+
}
|
|
48
|
+
return parentFrame.declarations;
|
|
49
|
+
};
|
|
50
|
+
tokens.forEach(token => {
|
|
51
|
+
switch (token.type) {
|
|
52
|
+
case 'Text':
|
|
53
|
+
{
|
|
54
|
+
const decoded = (0, _htmlEntityDecoder.decodeHtmlEntities)(token.value);
|
|
55
|
+
const targetArray = stack.length > 0 ? stack[stack.length - 1].children : result;
|
|
56
|
+
targetArray.push(decoded);
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
case 'TagOpen':
|
|
60
|
+
{
|
|
61
|
+
const {
|
|
62
|
+
tagNumber
|
|
63
|
+
} = token;
|
|
64
|
+
const currentDeclarations = getCurrentDeclarations();
|
|
65
|
+
const declaration = currentDeclarations[tagNumber];
|
|
66
|
+
if (!declaration) {
|
|
67
|
+
literalTagNumbers.add(tagNumber);
|
|
68
|
+
const literalText = `<${tagNumber}>`;
|
|
69
|
+
const targetArray = stack.length > 0 ? stack[stack.length - 1].children : result;
|
|
70
|
+
targetArray.push(literalText);
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
stack.push({
|
|
74
|
+
tagNumber,
|
|
75
|
+
children: [],
|
|
76
|
+
position: token.position,
|
|
77
|
+
declaration,
|
|
78
|
+
declarations: currentDeclarations
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
break;
|
|
82
|
+
case 'TagClose':
|
|
83
|
+
{
|
|
84
|
+
const {
|
|
85
|
+
tagNumber
|
|
86
|
+
} = token;
|
|
87
|
+
if (literalTagNumbers.has(tagNumber)) {
|
|
88
|
+
const literalText = `</${tagNumber}>`;
|
|
89
|
+
const literalTargetArray = stack.length > 0 ? stack[stack.length - 1].children : result;
|
|
90
|
+
literalTargetArray.push(literalText);
|
|
91
|
+
literalTagNumbers.delete(tagNumber);
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
if (stack.length === 0) {
|
|
95
|
+
throw new _TranslationParserError.TranslationParserError(`Unexpected closing tag </${tagNumber}> at position ${token.position}`, token.position, translation);
|
|
96
|
+
}
|
|
97
|
+
const frame = stack.pop();
|
|
98
|
+
if (frame.tagNumber !== tagNumber) {
|
|
99
|
+
throw new _TranslationParserError.TranslationParserError(`Mismatched tags: expected </${frame.tagNumber}> but got </${tagNumber}> at position ${token.position}`, token.position, translation);
|
|
100
|
+
}
|
|
101
|
+
const element = renderDeclarationNode(frame.declaration, frame.children, frame.declarations);
|
|
102
|
+
const elementTargetArray = stack.length > 0 ? stack[stack.length - 1].children : result;
|
|
103
|
+
elementTargetArray.push(element);
|
|
104
|
+
}
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
if (stack.length > 0) {
|
|
109
|
+
const unclosed = stack[stack.length - 1];
|
|
110
|
+
throw new _TranslationParserError.TranslationParserError(`Unclosed tag <${unclosed.tagNumber}> at position ${unclosed.position}`, unclosed.position, translation);
|
|
111
|
+
}
|
|
112
|
+
return result;
|
|
113
|
+
};
|
|
114
|
+
exports.renderTranslation = renderTranslation;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.tokenize = void 0;
|
|
7
|
+
const tokenize = translation => {
|
|
8
|
+
const tokens = [];
|
|
9
|
+
let position = 0;
|
|
10
|
+
let currentText = '';
|
|
11
|
+
const flushText = () => {
|
|
12
|
+
if (currentText) {
|
|
13
|
+
tokens.push({
|
|
14
|
+
type: 'Text',
|
|
15
|
+
value: currentText,
|
|
16
|
+
position: position - currentText.length
|
|
17
|
+
});
|
|
18
|
+
currentText = '';
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
while (position < translation.length) {
|
|
22
|
+
const char = translation[position];
|
|
23
|
+
if (char === '<') {
|
|
24
|
+
const tagMatch = translation.slice(position).match(/^<(\d+)>/);
|
|
25
|
+
if (tagMatch) {
|
|
26
|
+
flushText();
|
|
27
|
+
tokens.push({
|
|
28
|
+
type: 'TagOpen',
|
|
29
|
+
value: tagMatch[0],
|
|
30
|
+
position,
|
|
31
|
+
tagNumber: parseInt(tagMatch[1], 10)
|
|
32
|
+
});
|
|
33
|
+
position += tagMatch[0].length;
|
|
34
|
+
} else {
|
|
35
|
+
const closeTagMatch = translation.slice(position).match(/^<\/(\d+)>/);
|
|
36
|
+
if (closeTagMatch) {
|
|
37
|
+
flushText();
|
|
38
|
+
tokens.push({
|
|
39
|
+
type: 'TagClose',
|
|
40
|
+
value: closeTagMatch[0],
|
|
41
|
+
position,
|
|
42
|
+
tagNumber: parseInt(closeTagMatch[1], 10)
|
|
43
|
+
});
|
|
44
|
+
position += closeTagMatch[0].length;
|
|
45
|
+
} else {
|
|
46
|
+
currentText += char;
|
|
47
|
+
position += 1;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
} else {
|
|
51
|
+
currentText += char;
|
|
52
|
+
position += 1;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
flushText();
|
|
56
|
+
return tokens;
|
|
57
|
+
};
|
|
58
|
+
exports.tokenize = tokenize;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.IcuTransWithoutContext = IcuTransWithoutContext;
|
|
8
|
+
var _react = _interopRequireDefault(require("react"));
|
|
9
|
+
var _utils = require("./utils.js");
|
|
10
|
+
var _i18nInstance = require("./i18nInstance.js");
|
|
11
|
+
var _IcuTransUtils = require("./IcuTransUtils");
|
|
12
|
+
function IcuTransWithoutContext({
|
|
13
|
+
i18nKey,
|
|
14
|
+
defaultTranslation,
|
|
15
|
+
content,
|
|
16
|
+
ns,
|
|
17
|
+
values = {},
|
|
18
|
+
i18n: i18nFromProps,
|
|
19
|
+
t: tFromProps
|
|
20
|
+
}) {
|
|
21
|
+
const i18n = i18nFromProps || (0, _i18nInstance.getI18n)();
|
|
22
|
+
if (!i18n) {
|
|
23
|
+
(0, _utils.warnOnce)(i18n, 'NO_I18NEXT_INSTANCE', `IcuTrans: You need to pass in an i18next instance using i18nextReactModule`, {
|
|
24
|
+
i18nKey
|
|
25
|
+
});
|
|
26
|
+
return _react.default.createElement(_react.default.Fragment, {}, defaultTranslation);
|
|
27
|
+
}
|
|
28
|
+
const t = tFromProps || i18n.t?.bind(i18n) || (k => k);
|
|
29
|
+
let namespaces = ns || t.ns || i18n.options?.defaultNS;
|
|
30
|
+
namespaces = (0, _utils.isString)(namespaces) ? [namespaces] : namespaces || ['translation'];
|
|
31
|
+
let mergedValues = values;
|
|
32
|
+
if (i18n.options?.interpolation?.defaultVariables) {
|
|
33
|
+
mergedValues = values && Object.keys(values).length > 0 ? {
|
|
34
|
+
...values,
|
|
35
|
+
...i18n.options.interpolation.defaultVariables
|
|
36
|
+
} : {
|
|
37
|
+
...i18n.options.interpolation.defaultVariables
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
const translation = t(i18nKey, {
|
|
41
|
+
defaultValue: defaultTranslation,
|
|
42
|
+
...mergedValues,
|
|
43
|
+
ns: namespaces
|
|
44
|
+
});
|
|
45
|
+
try {
|
|
46
|
+
const rendered = (0, _IcuTransUtils.renderTranslation)(translation, content);
|
|
47
|
+
return _react.default.createElement(_react.default.Fragment, {}, ...rendered);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
(0, _utils.warn)(i18n, 'ICU_TRANS_RENDER_ERROR', `IcuTrans component error for key "${i18nKey}": ${error.message}`, {
|
|
50
|
+
i18nKey,
|
|
51
|
+
error
|
|
52
|
+
});
|
|
53
|
+
return _react.default.createElement(_react.default.Fragment, {}, translation);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
IcuTransWithoutContext.displayName = 'IcuTransWithoutContext';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { IcuTransWithoutContext } from './IcuTransWithoutContext.js';
|
|
3
|
+
import { getI18n, I18nContext } from './context.js';
|
|
4
|
+
export function IcuTrans({
|
|
5
|
+
i18nKey,
|
|
6
|
+
defaultTranslation,
|
|
7
|
+
content,
|
|
8
|
+
ns,
|
|
9
|
+
values = {},
|
|
10
|
+
i18n: i18nFromProps,
|
|
11
|
+
t: tFromProps
|
|
12
|
+
}) {
|
|
13
|
+
const {
|
|
14
|
+
i18n: i18nFromContext,
|
|
15
|
+
defaultNS: defaultNSFromContext
|
|
16
|
+
} = useContext(I18nContext) || {};
|
|
17
|
+
const i18n = i18nFromProps || i18nFromContext || getI18n();
|
|
18
|
+
const t = tFromProps || i18n?.t.bind(i18n);
|
|
19
|
+
return IcuTransWithoutContext({
|
|
20
|
+
i18nKey,
|
|
21
|
+
defaultTranslation,
|
|
22
|
+
content,
|
|
23
|
+
ns: ns || t?.ns || defaultNSFromContext || i18n?.options?.defaultNS,
|
|
24
|
+
values,
|
|
25
|
+
i18n,
|
|
26
|
+
t: tFromProps
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
IcuTrans.displayName = 'IcuTrans';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export class TranslationParserError extends Error {
|
|
2
|
+
constructor(message, position, translationString) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = 'TranslationParserError';
|
|
5
|
+
this.position = position;
|
|
6
|
+
this.translationString = translationString;
|
|
7
|
+
if (Error.captureStackTrace) {
|
|
8
|
+
Error.captureStackTrace(this, TranslationParserError);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|