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 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
+ '&nbsp;': '\u00A0',
9
+ '&amp;': '&',
10
+ '&lt;': '<',
11
+ '&gt;': '>',
12
+ '&quot;': '"',
13
+ '&apos;': "'",
14
+ '&copy;': '©',
15
+ '&reg;': '®',
16
+ '&trade;': '™',
17
+ '&hellip;': '…',
18
+ '&ndash;': '–',
19
+ '&mdash;': '—',
20
+ '&lsquo;': '\u2018',
21
+ '&rsquo;': '\u2019',
22
+ '&sbquo;': '\u201A',
23
+ '&ldquo;': '\u201C',
24
+ '&rdquo;': '\u201D',
25
+ '&bdquo;': '\u201E',
26
+ '&dagger;': '†',
27
+ '&Dagger;': '‡',
28
+ '&bull;': '•',
29
+ '&prime;': '′',
30
+ '&Prime;': '″',
31
+ '&lsaquo;': '‹',
32
+ '&rsaquo;': '›',
33
+ '&sect;': '§',
34
+ '&para;': '¶',
35
+ '&middot;': '·',
36
+ '&ensp;': '\u2002',
37
+ '&emsp;': '\u2003',
38
+ '&thinsp;': '\u2009',
39
+ '&euro;': '€',
40
+ '&pound;': '£',
41
+ '&yen;': '¥',
42
+ '&cent;': '¢',
43
+ '&curren;': '¤',
44
+ '&times;': '×',
45
+ '&divide;': '÷',
46
+ '&minus;': '−',
47
+ '&plusmn;': '±',
48
+ '&ne;': '≠',
49
+ '&le;': '≤',
50
+ '&ge;': '≥',
51
+ '&asymp;': '≈',
52
+ '&equiv;': '≡',
53
+ '&infin;': '∞',
54
+ '&int;': '∫',
55
+ '&sum;': '∑',
56
+ '&prod;': '∏',
57
+ '&radic;': '√',
58
+ '&part;': '∂',
59
+ '&permil;': '‰',
60
+ '&deg;': '°',
61
+ '&micro;': 'µ',
62
+ '&larr;': '←',
63
+ '&uarr;': '↑',
64
+ '&rarr;': '→',
65
+ '&darr;': '↓',
66
+ '&harr;': '↔',
67
+ '&crarr;': '↵',
68
+ '&lArr;': '⇐',
69
+ '&uArr;': '⇑',
70
+ '&rArr;': '⇒',
71
+ '&dArr;': '⇓',
72
+ '&hArr;': '⇔',
73
+ '&alpha;': 'α',
74
+ '&beta;': 'β',
75
+ '&gamma;': 'γ',
76
+ '&delta;': 'δ',
77
+ '&epsilon;': 'ε',
78
+ '&zeta;': 'ζ',
79
+ '&eta;': 'η',
80
+ '&theta;': 'θ',
81
+ '&iota;': 'ι',
82
+ '&kappa;': 'κ',
83
+ '&lambda;': 'λ',
84
+ '&mu;': 'μ',
85
+ '&nu;': 'ν',
86
+ '&xi;': 'ξ',
87
+ '&omicron;': 'ο',
88
+ '&pi;': 'π',
89
+ '&rho;': 'ρ',
90
+ '&sigma;': 'σ',
91
+ '&tau;': 'τ',
92
+ '&upsilon;': 'υ',
93
+ '&phi;': 'φ',
94
+ '&chi;': 'χ',
95
+ '&psi;': 'ψ',
96
+ '&omega;': 'ω',
97
+ '&Alpha;': 'Α',
98
+ '&Beta;': 'Β',
99
+ '&Gamma;': 'Γ',
100
+ '&Delta;': 'Δ',
101
+ '&Epsilon;': 'Ε',
102
+ '&Zeta;': 'Ζ',
103
+ '&Eta;': 'Η',
104
+ '&Theta;': 'Θ',
105
+ '&Iota;': 'Ι',
106
+ '&Kappa;': 'Κ',
107
+ '&Lambda;': 'Λ',
108
+ '&Mu;': 'Μ',
109
+ '&Nu;': 'Ν',
110
+ '&Xi;': 'Ξ',
111
+ '&Omicron;': 'Ο',
112
+ '&Pi;': 'Π',
113
+ '&Rho;': 'Ρ',
114
+ '&Sigma;': 'Σ',
115
+ '&Tau;': 'Τ',
116
+ '&Upsilon;': 'Υ',
117
+ '&Phi;': 'Φ',
118
+ '&Chi;': 'Χ',
119
+ '&Psi;': 'Ψ',
120
+ '&Omega;': 'Ω',
121
+ '&Agrave;': 'À',
122
+ '&Aacute;': 'Á',
123
+ '&Acirc;': 'Â',
124
+ '&Atilde;': 'Ã',
125
+ '&Auml;': 'Ä',
126
+ '&Aring;': 'Å',
127
+ '&AElig;': 'Æ',
128
+ '&Ccedil;': 'Ç',
129
+ '&Egrave;': 'È',
130
+ '&Eacute;': 'É',
131
+ '&Ecirc;': 'Ê',
132
+ '&Euml;': 'Ë',
133
+ '&Igrave;': 'Ì',
134
+ '&Iacute;': 'Í',
135
+ '&Icirc;': 'Î',
136
+ '&Iuml;': 'Ï',
137
+ '&ETH;': 'Ð',
138
+ '&Ntilde;': 'Ñ',
139
+ '&Ograve;': 'Ò',
140
+ '&Oacute;': 'Ó',
141
+ '&Ocirc;': 'Ô',
142
+ '&Otilde;': 'Õ',
143
+ '&Ouml;': 'Ö',
144
+ '&Oslash;': 'Ø',
145
+ '&Ugrave;': 'Ù',
146
+ '&Uacute;': 'Ú',
147
+ '&Ucirc;': 'Û',
148
+ '&Uuml;': 'Ü',
149
+ '&Yacute;': 'Ý',
150
+ '&THORN;': 'Þ',
151
+ '&szlig;': 'ß',
152
+ '&agrave;': 'à',
153
+ '&aacute;': 'á',
154
+ '&acirc;': 'â',
155
+ '&atilde;': 'ã',
156
+ '&auml;': 'ä',
157
+ '&aring;': 'å',
158
+ '&aelig;': 'æ',
159
+ '&ccedil;': 'ç',
160
+ '&egrave;': 'è',
161
+ '&eacute;': 'é',
162
+ '&ecirc;': 'ê',
163
+ '&euml;': 'ë',
164
+ '&igrave;': 'ì',
165
+ '&iacute;': 'í',
166
+ '&icirc;': 'î',
167
+ '&iuml;': 'ï',
168
+ '&eth;': 'ð',
169
+ '&ntilde;': 'ñ',
170
+ '&ograve;': 'ò',
171
+ '&oacute;': 'ó',
172
+ '&ocirc;': 'ô',
173
+ '&otilde;': 'õ',
174
+ '&ouml;': 'ö',
175
+ '&oslash;': 'ø',
176
+ '&ugrave;': 'ù',
177
+ '&uacute;': 'ú',
178
+ '&ucirc;': 'û',
179
+ '&uuml;': 'ü',
180
+ '&yacute;': 'ý',
181
+ '&thorn;': 'þ',
182
+ '&yuml;': 'ÿ',
183
+ '&iexcl;': '¡',
184
+ '&iquest;': '¿',
185
+ '&fnof;': 'ƒ',
186
+ '&circ;': 'ˆ',
187
+ '&tilde;': '˜',
188
+ '&OElig;': 'Œ',
189
+ '&oelig;': 'œ',
190
+ '&Scaron;': 'Š',
191
+ '&scaron;': 'š',
192
+ '&Yuml;': 'Ÿ',
193
+ '&ordf;': 'ª',
194
+ '&ordm;': 'º',
195
+ '&macr;': '¯',
196
+ '&acute;': '´',
197
+ '&cedil;': '¸',
198
+ '&sup1;': '¹',
199
+ '&sup2;': '²',
200
+ '&sup3;': '³',
201
+ '&frac14;': '¼',
202
+ '&frac12;': '½',
203
+ '&frac34;': '¾',
204
+ '&spades;': '♠',
205
+ '&clubs;': '♣',
206
+ '&hearts;': '♥',
207
+ '&diams;': '♦',
208
+ '&loz;': '◊',
209
+ '&oline;': '‾',
210
+ '&frasl;': '⁄',
211
+ '&weierp;': '℘',
212
+ '&image;': 'ℑ',
213
+ '&real;': 'ℜ',
214
+ '&alefsym;': 'ℵ'
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
+ }