@zohodesk/i18n 1.0.0-beta.33 → 1.0.0-beta.35-murphy
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/README.md +4 -0
- package/docs/murphy/01-MURPHY_OVERVIEW.md +148 -0
- package/docs/murphy/02-MURPHY_ARCHITECTURE.md +283 -0
- package/docs/murphy/03-MURPHY_BACKEND_CONFIG.md +337 -0
- package/docs/murphy/04-MURPHY_FRONTEND_INIT.md +437 -0
- package/docs/murphy/05-MURPHY_DESK_CLIENT_USAGE.md +467 -0
- package/docs/murphy/06-MURPHY_I18N_INTEGRATION.md +402 -0
- package/docs/murphy/07-MURPHY_WHY_I18N_APPROACH.md +391 -0
- package/es/components/DateTimeDiffFormat.js +5 -19
- package/es/components/FormatText.js +2 -2
- package/es/components/HOCI18N.js +32 -43
- package/es/components/I18N.js +2 -13
- package/es/components/I18NProvider.js +0 -9
- package/es/components/PluralFormat.js +3 -5
- package/es/components/UserTimeDiffFormat.js +5 -9
- package/es/components/__tests__/DateTimeDiffFormat.spec.js +157 -221
- package/es/components/__tests__/FormatText.spec.js +2 -2
- package/es/components/__tests__/HOCI18N.spec.js +2 -4
- package/es/components/__tests__/I18N.spec.js +6 -4
- package/es/components/__tests__/I18NProvider.spec.js +4 -4
- package/es/components/__tests__/PluralFormat.spec.js +2 -2
- package/es/components/__tests__/UserTimeDiffFormat.spec.js +249 -348
- package/es/index.js +2 -0
- package/es/utils/__tests__/jsxTranslations.spec.js +3 -7
- package/es/utils/errorReporter.js +31 -0
- package/es/utils/index.js +42 -92
- package/es/utils/jsxTranslations.js +53 -61
- package/lib/I18NContext.js +2 -7
- package/lib/components/DateTimeDiffFormat.js +46 -87
- package/lib/components/FormatText.js +18 -41
- package/lib/components/HOCI18N.js +24 -59
- package/lib/components/I18N.js +27 -64
- package/lib/components/I18NProvider.js +27 -63
- package/lib/components/PluralFormat.js +24 -50
- package/lib/components/UserTimeDiffFormat.js +43 -72
- package/lib/components/__tests__/DateTimeDiffFormat.spec.js +95 -165
- package/lib/components/__tests__/FormatText.spec.js +3 -10
- package/lib/components/__tests__/HOCI18N.spec.js +3 -14
- package/lib/components/__tests__/I18N.spec.js +4 -12
- package/lib/components/__tests__/I18NProvider.spec.js +8 -23
- package/lib/components/__tests__/PluralFormat.spec.js +3 -11
- package/lib/components/__tests__/UserTimeDiffFormat.spec.js +157 -225
- package/lib/index.js +32 -22
- package/lib/utils/__tests__/jsxTranslations.spec.js +1 -12
- package/lib/utils/errorReporter.js +39 -0
- package/lib/utils/index.js +49 -125
- package/lib/utils/jsxTranslations.js +79 -105
- package/package.json +1 -1
- package/src/index.js +6 -0
- package/src/utils/errorReporter.js +36 -0
- package/src/utils/index.js +8 -1
- package/src/utils/jsxTranslations.js +31 -12
|
@@ -1,48 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
3
4
|
Object.defineProperty(exports, "__esModule", {
|
|
4
5
|
value: true
|
|
5
6
|
});
|
|
6
7
|
exports.createElement = createElement;
|
|
7
8
|
exports.generateChildren = generateChildren;
|
|
8
9
|
exports.getI18NComponent = getI18NComponent;
|
|
10
|
+
exports.placeComponentForTags = placeComponentForTags;
|
|
9
11
|
exports.prepareI18NFunc = prepareI18NFunc;
|
|
10
12
|
exports.replaceWithComponentPlaceHolder = replaceWithComponentPlaceHolder;
|
|
11
13
|
exports.splitMatchedExp = splitMatchedExp;
|
|
12
|
-
|
|
13
14
|
var _react = _interopRequireDefault(require("react"));
|
|
14
|
-
|
|
15
15
|
var _index = require("./index");
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
function _defineProperty(
|
|
20
|
-
|
|
21
|
-
function
|
|
22
|
-
|
|
16
|
+
var _I18NProvider = require("../components/I18NProvider");
|
|
17
|
+
var _errorReporter = require("./errorReporter");
|
|
18
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
19
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
20
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
21
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
22
|
+
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
23
23
|
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
24
|
-
|
|
25
|
-
function
|
|
26
|
-
|
|
27
|
-
function
|
|
28
|
-
|
|
29
|
-
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
30
|
-
|
|
31
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
32
|
-
|
|
33
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
34
|
-
|
|
24
|
+
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
25
|
+
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
|
26
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
|
|
27
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
28
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
35
29
|
function splitMatchedExp(_ref) {
|
|
36
30
|
var expression = _ref.expression,
|
|
37
|
-
|
|
38
|
-
|
|
31
|
+
charLenToOmit = _ref.charLenToOmit,
|
|
32
|
+
string = _ref.string;
|
|
39
33
|
var splitString = string.split(new RegExp(expression, 'g'));
|
|
40
34
|
var matchedExpAll = string.matchAll(new RegExp(expression, 'g'));
|
|
41
35
|
var matchedExpKeys = [];
|
|
42
|
-
|
|
43
36
|
var _iterator = _createForOfIteratorHelper(matchedExpAll),
|
|
44
|
-
|
|
45
|
-
|
|
37
|
+
_step;
|
|
46
38
|
try {
|
|
47
39
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
48
40
|
var match = _step.value;
|
|
@@ -54,42 +46,36 @@ function splitMatchedExp(_ref) {
|
|
|
54
46
|
} finally {
|
|
55
47
|
_iterator.f();
|
|
56
48
|
}
|
|
57
|
-
|
|
58
49
|
return {
|
|
59
50
|
splitString: splitString,
|
|
60
51
|
matchedExpKeys: matchedExpKeys
|
|
61
52
|
};
|
|
62
53
|
}
|
|
63
|
-
|
|
64
54
|
function generateChildren(_ref2) {
|
|
65
55
|
var children = _ref2.children,
|
|
66
|
-
|
|
56
|
+
child = _ref2.child;
|
|
67
57
|
var newChildren = [],
|
|
68
|
-
|
|
69
|
-
|
|
58
|
+
stringPlaceHolders = {};
|
|
70
59
|
var handleString = function handleString(string) {
|
|
71
60
|
if (string) {
|
|
72
61
|
var _splitMatchedExp = splitMatchedExp({
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
62
|
+
expression: '{[0-9]+?}',
|
|
63
|
+
charLenToOmit: 1,
|
|
64
|
+
string: string
|
|
65
|
+
}),
|
|
66
|
+
splitString = _splitMatchedExp.splitString,
|
|
67
|
+
matchedExpKeys = _splitMatchedExp.matchedExpKeys;
|
|
80
68
|
stringPlaceHolders[newChildren.length] = matchedExpKeys;
|
|
81
69
|
newChildren.push(splitString);
|
|
82
70
|
}
|
|
83
71
|
};
|
|
84
|
-
|
|
85
72
|
var _splitMatchedExp2 = splitMatchedExp({
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
73
|
+
expression: '<[A-Za-z0-9]+?>',
|
|
74
|
+
charLenToOmit: 1,
|
|
75
|
+
string: child
|
|
76
|
+
}),
|
|
77
|
+
splitChild = _splitMatchedExp2.splitString,
|
|
78
|
+
childrenKeys = _splitMatchedExp2.matchedExpKeys;
|
|
93
79
|
childrenKeys.forEach(function (childKey, childIndex) {
|
|
94
80
|
var matchedChild = splitChild[childIndex];
|
|
95
81
|
handleString(matchedChild);
|
|
@@ -101,27 +87,23 @@ function generateChildren(_ref2) {
|
|
|
101
87
|
stringPlaceHolders: stringPlaceHolders
|
|
102
88
|
};
|
|
103
89
|
}
|
|
104
|
-
|
|
105
90
|
function createElement() {
|
|
106
91
|
var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
92
|
+
componentId = _ref3.componentId,
|
|
93
|
+
_ref3$children = _ref3.children,
|
|
94
|
+
children = _ref3$children === void 0 ? [] : _ref3$children,
|
|
95
|
+
_ref3$stringPlaceHold = _ref3.stringPlaceHolders,
|
|
96
|
+
stringPlaceHolders = _ref3$stringPlaceHold === void 0 ? {} : _ref3$stringPlaceHold;
|
|
113
97
|
return function () {
|
|
114
98
|
var _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
99
|
+
_ref4$components = _ref4.components,
|
|
100
|
+
components = _ref4$components === void 0 ? {} : _ref4$components,
|
|
101
|
+
_ref4$values = _ref4.values,
|
|
102
|
+
values = _ref4$values === void 0 ? [] : _ref4$values;
|
|
120
103
|
var _ref5 = components[componentId] || {},
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
104
|
+
_ref5$type = _ref5.type,
|
|
105
|
+
type = _ref5$type === void 0 ? _react["default"].Fragment : _ref5$type,
|
|
106
|
+
props = _ref5.props;
|
|
125
107
|
var childElements = children.map(function (child, index) {
|
|
126
108
|
if (typeof child === 'function') {
|
|
127
109
|
return child({
|
|
@@ -129,60 +111,49 @@ function createElement() {
|
|
|
129
111
|
values: values
|
|
130
112
|
});
|
|
131
113
|
}
|
|
132
|
-
|
|
133
114
|
var string = '',
|
|
134
|
-
|
|
115
|
+
stringIndex = 0;
|
|
135
116
|
(stringPlaceHolders[index] || []).map(function (stringId, index) {
|
|
136
117
|
if (child[index]) {
|
|
137
118
|
string += child[index];
|
|
138
119
|
}
|
|
139
|
-
|
|
140
120
|
string += values[stringId];
|
|
141
121
|
stringIndex++;
|
|
142
122
|
});
|
|
143
|
-
|
|
144
123
|
if (child[stringIndex]) {
|
|
145
124
|
string += child[stringIndex];
|
|
146
125
|
}
|
|
147
|
-
|
|
148
126
|
return string;
|
|
149
127
|
});
|
|
150
128
|
return /*#__PURE__*/_react["default"].createElement.apply(_react["default"], [type, props].concat(_toConsumableArray(childElements)));
|
|
151
129
|
};
|
|
152
130
|
}
|
|
153
|
-
|
|
154
131
|
;
|
|
155
|
-
|
|
156
132
|
function replaceWithComponentPlaceHolder(_ref6) {
|
|
157
133
|
var componentId = _ref6.componentId,
|
|
158
|
-
|
|
159
|
-
|
|
134
|
+
i18nKey = _ref6.i18nKey,
|
|
135
|
+
childrenLength = _ref6.childrenLength;
|
|
160
136
|
var closingTagIndex = i18nKey.indexOf("</".concat(componentId, ">"));
|
|
161
137
|
var childWithOpenTag = i18nKey.substring(0, closingTagIndex);
|
|
162
138
|
var openTagIndex = childWithOpenTag.lastIndexOf("<".concat(componentId, ">"));
|
|
163
139
|
return i18nKey.substring(0, openTagIndex) + "<0".concat(childrenLength).concat(componentId, ">") + i18nKey.substring(closingTagIndex + "".concat(componentId).length + 3);
|
|
164
140
|
}
|
|
165
|
-
|
|
166
141
|
function prepareI18NFunc() {
|
|
167
142
|
var _ref7 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
143
|
+
i18nKey = _ref7.i18nKey,
|
|
144
|
+
_ref7$children = _ref7.children,
|
|
145
|
+
children = _ref7$children === void 0 ? {} : _ref7$children;
|
|
172
146
|
var componentIdMatch = i18nKey.match(/<\/[A-Za-z0-9]+?>/);
|
|
173
|
-
|
|
174
147
|
if (componentIdMatch) {
|
|
175
148
|
var componentId = componentIdMatch[0].substring(2, componentIdMatch[0].length - 1);
|
|
176
149
|
var childWithOpenTag = (i18nKey.match(new RegExp("(.*?)(?=</".concat(componentId, ">)"))) || [])[0] || '';
|
|
177
150
|
var child = childWithOpenTag.split("<".concat(componentId, ">")).pop();
|
|
178
|
-
|
|
179
151
|
var _generateChildren = generateChildren({
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
152
|
+
children: children,
|
|
153
|
+
child: child
|
|
154
|
+
}),
|
|
155
|
+
_newChildren = _generateChildren.newChildren,
|
|
156
|
+
_stringPlaceHolders = _generateChildren.stringPlaceHolders;
|
|
186
157
|
var childrenLength = Object.keys(children).length;
|
|
187
158
|
children["0".concat(childrenLength).concat(componentId)] = createElement({
|
|
188
159
|
componentId: componentId,
|
|
@@ -199,44 +170,47 @@ function prepareI18NFunc() {
|
|
|
199
170
|
children: children
|
|
200
171
|
});
|
|
201
172
|
}
|
|
202
|
-
|
|
203
173
|
var _generateChildren2 = generateChildren({
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
174
|
+
child: i18nKey,
|
|
175
|
+
children: children
|
|
176
|
+
}),
|
|
177
|
+
newChildren = _generateChildren2.newChildren,
|
|
178
|
+
stringPlaceHolders = _generateChildren2.stringPlaceHolders;
|
|
210
179
|
return createElement({
|
|
211
180
|
componentId: -1,
|
|
212
181
|
children: newChildren,
|
|
213
182
|
stringPlaceHolders: stringPlaceHolders
|
|
214
183
|
});
|
|
215
184
|
}
|
|
216
|
-
|
|
185
|
+
function i18nMechanismForComponents(i18nValue, key) {
|
|
186
|
+
if (typeof i18nValue === 'string') {
|
|
187
|
+
i18nValue = (0, _index.unescapeUnicode)(i18nValue);
|
|
188
|
+
var value = prepareI18NFunc({
|
|
189
|
+
i18nKey: i18nValue
|
|
190
|
+
});
|
|
191
|
+
window.loadI18nChunk && window.loadI18nChunk(_defineProperty({}, key, value));
|
|
192
|
+
i18nValue = value;
|
|
193
|
+
}
|
|
194
|
+
return i18nValue;
|
|
195
|
+
}
|
|
196
|
+
function placeComponentForTags(i18nValue) {
|
|
197
|
+
var value = _I18NProvider.i18NProviderUtils.getI18NComponent(i18nValue);
|
|
198
|
+
return i18nMechanismForComponents(value, value);
|
|
199
|
+
}
|
|
217
200
|
function getI18NComponent(i18n) {
|
|
218
201
|
if (typeof i18n === 'undefined') {
|
|
202
|
+
(0, _errorReporter.reportI18NError)(_errorReporter.I18N_ERROR_TYPES.UNDEFINED_OBJECT, 'i18n_object_jsx');
|
|
219
203
|
return function (key) {
|
|
220
204
|
return key;
|
|
221
205
|
};
|
|
222
206
|
}
|
|
223
|
-
|
|
224
207
|
return function (key) {
|
|
225
208
|
var i18nStr = i18n[key];
|
|
226
|
-
|
|
227
209
|
if (i18nStr === undefined) {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
if (typeof i18nStr === 'string') {
|
|
232
|
-
i18nStr = (0, _index.unescapeUnicode)(i18nStr);
|
|
233
|
-
var value = prepareI18NFunc({
|
|
234
|
-
i18nKey: i18nStr
|
|
235
|
-
});
|
|
236
|
-
window.loadI18nChunk && window.loadI18nChunk(_defineProperty({}, key, value));
|
|
237
|
-
i18nStr = value;
|
|
210
|
+
var isNumeric = /^\d+$/.test(key);
|
|
211
|
+
(0, _errorReporter.reportI18NError)(isNumeric ? _errorReporter.I18N_ERROR_TYPES.NUMERIC_FALLBACK : _errorReporter.I18N_ERROR_TYPES.MISSING_KEY, key);
|
|
212
|
+
return getFallbackText(key);
|
|
238
213
|
}
|
|
239
|
-
|
|
240
|
-
return i18nStr;
|
|
214
|
+
return i18nMechanismForComponents(i18nStr, key);
|
|
241
215
|
};
|
|
242
216
|
}
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -21,6 +21,11 @@ export {
|
|
|
21
21
|
monthi18n,
|
|
22
22
|
timei18n
|
|
23
23
|
} from './utils';
|
|
24
|
+
export {
|
|
25
|
+
reportI18NError,
|
|
26
|
+
clearReportedKeys,
|
|
27
|
+
I18N_ERROR_TYPES
|
|
28
|
+
} from './utils/errorReporter';
|
|
24
29
|
import { getI18NValue as getI18NValue1 } from './utils';
|
|
25
30
|
export { I18NContext } from './I18NContext';
|
|
26
31
|
export {
|
|
@@ -33,5 +38,6 @@ export { default as FormatText } from './components/FormatText';
|
|
|
33
38
|
export { default as PluralFormat } from './components/PluralFormat';
|
|
34
39
|
export { default as DateTimeDiffFormat } from './components/DateTimeDiffFormat';
|
|
35
40
|
export { default as UserTimeDiffFormat } from './components/UserTimeDiffFormat';
|
|
41
|
+
export { placeComponentForTags } from "./utils/jsxTranslations";
|
|
36
42
|
export const getI18NValue = (i18n, key, values) =>
|
|
37
43
|
getI18NValue1(i18n)(key, values);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export const I18N_ERROR_TYPES = {
|
|
2
|
+
UNDEFINED_OBJECT: 'I18N_UNDEFINED_OBJECT',
|
|
3
|
+
MISSING_KEY: 'I18N_MISSING_KEY',
|
|
4
|
+
NUMERIC_FALLBACK: 'I18N_NUMERIC_FALLBACK'
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
const reportedKeys = new Set();
|
|
8
|
+
|
|
9
|
+
function isMurphyAvailable() {
|
|
10
|
+
return typeof murphy !== 'undefined' && typeof murphy.error === 'function';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function reportI18NError(type, key) {
|
|
14
|
+
const dedupeKey = `${type}:${key}`;
|
|
15
|
+
if (reportedKeys.has(dedupeKey)) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
reportedKeys.add(dedupeKey);
|
|
19
|
+
|
|
20
|
+
if (isMurphyAvailable()) {
|
|
21
|
+
const error = new Error(`i18n ${type}: ${key}`);
|
|
22
|
+
error.name = type;
|
|
23
|
+
|
|
24
|
+
murphy.error(error, undefined, {
|
|
25
|
+
customTags: {
|
|
26
|
+
errorType: type,
|
|
27
|
+
i18nKey: key,
|
|
28
|
+
category: 'i18n'
|
|
29
|
+
},
|
|
30
|
+
preventClientGrouping: true
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export function clearReportedKeys() {
|
|
35
|
+
reportedKeys.clear();
|
|
36
|
+
}
|
package/src/utils/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import datetime from '@zohodesk/datetimejs';
|
|
|
2
2
|
import {
|
|
3
3
|
i18NProviderUtils
|
|
4
4
|
} from '../components/I18NProvider';
|
|
5
|
+
import { reportI18NError, I18N_ERROR_TYPES } from './errorReporter';
|
|
5
6
|
|
|
6
7
|
export const dayi18n = {
|
|
7
8
|
'Sun': 'deskreact.calendar.daynameshort.sunday',
|
|
@@ -290,6 +291,7 @@ export function getLocalizedValue({
|
|
|
290
291
|
}
|
|
291
292
|
export function getI18NValue(i18n) {
|
|
292
293
|
if (typeof i18n === 'undefined') {
|
|
294
|
+
reportI18NError(I18N_ERROR_TYPES.UNDEFINED_OBJECT, 'i18n_object');
|
|
293
295
|
return (key) => key;
|
|
294
296
|
}
|
|
295
297
|
return (key, values, localizedProps) => {
|
|
@@ -297,7 +299,12 @@ export function getI18NValue(i18n) {
|
|
|
297
299
|
const finalKey = getMappedKey(key);
|
|
298
300
|
let i18nStr = i18n[finalKey];
|
|
299
301
|
if (i18nStr === undefined) {
|
|
300
|
-
|
|
302
|
+
const isNumeric = /^\d+$/.test(finalKey);
|
|
303
|
+
reportI18NError(
|
|
304
|
+
isNumeric ? I18N_ERROR_TYPES.NUMERIC_FALLBACK : I18N_ERROR_TYPES.MISSING_KEY,
|
|
305
|
+
finalKey
|
|
306
|
+
);
|
|
307
|
+
return localizedValue || getFallbackText(finalKey);
|
|
301
308
|
}
|
|
302
309
|
i18nStr = replaceI18NValuesWithRegex(i18nStr, values);
|
|
303
310
|
return localizedValue || unescapeUnicode(i18nStr);
|
|
@@ -2,6 +2,10 @@ import React from 'react';
|
|
|
2
2
|
import {
|
|
3
3
|
unescapeUnicode
|
|
4
4
|
} from './index';
|
|
5
|
+
import {
|
|
6
|
+
i18NProviderUtils
|
|
7
|
+
} from "../components/I18NProvider";
|
|
8
|
+
import { reportI18NError, I18N_ERROR_TYPES } from './errorReporter';
|
|
5
9
|
|
|
6
10
|
export function splitMatchedExp({
|
|
7
11
|
expression,
|
|
@@ -156,25 +160,40 @@ export function prepareI18NFunc({
|
|
|
156
160
|
});
|
|
157
161
|
}
|
|
158
162
|
|
|
163
|
+
function i18nMechanismForComponents(i18nValue, key) {
|
|
164
|
+
if(typeof i18nValue === 'string') {
|
|
165
|
+
i18nValue = unescapeUnicode(i18nValue);
|
|
166
|
+
const value = prepareI18NFunc({
|
|
167
|
+
i18nKey: i18nValue
|
|
168
|
+
});
|
|
169
|
+
window.loadI18nChunk && window.loadI18nChunk({
|
|
170
|
+
[key] : value
|
|
171
|
+
});
|
|
172
|
+
i18nValue = value;
|
|
173
|
+
}
|
|
174
|
+
return i18nValue;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function placeComponentForTags(i18nValue) {
|
|
178
|
+
const value = i18NProviderUtils.getI18NComponent(i18nValue);
|
|
179
|
+
return i18nMechanismForComponents(value, value);
|
|
180
|
+
}
|
|
181
|
+
|
|
159
182
|
export function getI18NComponent(i18n) {
|
|
160
183
|
if(typeof i18n === 'undefined') {
|
|
184
|
+
reportI18NError(I18N_ERROR_TYPES.UNDEFINED_OBJECT, 'i18n_object_jsx');
|
|
161
185
|
return (key) => key;
|
|
162
186
|
}
|
|
163
187
|
return (key) => {
|
|
164
188
|
let i18nStr = i18n[key];
|
|
165
189
|
if (i18nStr === undefined) {
|
|
166
|
-
|
|
190
|
+
const isNumeric = /^\d+$/.test(key);
|
|
191
|
+
reportI18NError(
|
|
192
|
+
isNumeric ? I18N_ERROR_TYPES.NUMERIC_FALLBACK : I18N_ERROR_TYPES.MISSING_KEY,
|
|
193
|
+
key
|
|
194
|
+
);
|
|
195
|
+
return getFallbackText(key);
|
|
167
196
|
}
|
|
168
|
-
|
|
169
|
-
i18nStr = unescapeUnicode(i18nStr);
|
|
170
|
-
const value = prepareI18NFunc({
|
|
171
|
-
i18nKey: i18nStr
|
|
172
|
-
});
|
|
173
|
-
window.loadI18nChunk && window.loadI18nChunk({
|
|
174
|
-
[key] : value
|
|
175
|
-
});
|
|
176
|
-
i18nStr = value;
|
|
177
|
-
}
|
|
178
|
-
return i18nStr;
|
|
197
|
+
return i18nMechanismForComponents(i18nStr, key);
|
|
179
198
|
}
|
|
180
199
|
}
|