@zohodesk/i18n 1.0.0-beta.26 → 1.0.0-beta.28
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 +113 -105
- package/es/components/I18NProvider.js +5 -1
- package/es/components/__tests__/__snapshots__/DateTimeDiffFormat.spec.js.snap +258 -258
- package/es/components/__tests__/__snapshots__/FormatText.spec.js.snap +17 -17
- package/es/components/__tests__/__snapshots__/HOCI18N.spec.js.snap +15 -15
- package/es/components/__tests__/__snapshots__/I18N.spec.js.snap +17 -17
- package/es/components/__tests__/__snapshots__/I18NProvider.spec.js.snap +13 -13
- package/es/components/__tests__/__snapshots__/PluralFormat.spec.js.snap +17 -17
- package/es/components/__tests__/__snapshots__/UserTimeDiffFormat.spec.js.snap +366 -366
- package/es/utils/__tests__/jsxTranslations.spec.js +174 -0
- package/es/{utils.js → utils/index.js} +25 -2
- package/es/utils/jsxTranslations.js +193 -0
- package/lib/components/I18NProvider.js +6 -1
- package/lib/components/__tests__/__snapshots__/DateTimeDiffFormat.spec.js.snap +258 -258
- package/lib/components/__tests__/__snapshots__/FormatText.spec.js.snap +17 -17
- package/lib/components/__tests__/__snapshots__/HOCI18N.spec.js.snap +15 -15
- package/lib/components/__tests__/__snapshots__/I18N.spec.js.snap +17 -17
- package/lib/components/__tests__/__snapshots__/I18NProvider.spec.js.snap +13 -13
- package/lib/components/__tests__/__snapshots__/PluralFormat.spec.js.snap +17 -17
- package/lib/components/__tests__/__snapshots__/UserTimeDiffFormat.spec.js.snap +366 -366
- package/lib/utils/__tests__/jsxTranslations.spec.js +183 -0
- package/lib/{utils.js → utils/index.js} +28 -5
- package/lib/utils/jsxTranslations.js +242 -0
- package/package.json +30 -29
- package/src/I18NContext.js +2 -2
- package/src/components/DateTimeDiffFormat.js +256 -256
- package/src/components/FormatText.js +14 -14
- package/src/components/HOCI18N.js +37 -37
- package/src/components/I18N.js +74 -74
- package/src/components/I18NProvider.js +116 -110
- package/src/components/PluralFormat.js +37 -37
- package/src/components/UserTimeDiffFormat.js +97 -97
- package/src/components/__tests__/DateTimeDiffFormat.spec.js +618 -618
- package/src/components/__tests__/FormatText.spec.js +26 -26
- package/src/components/__tests__/HOCI18N.spec.js +33 -33
- package/src/components/__tests__/I18N.spec.js +29 -29
- package/src/components/__tests__/I18NProvider.spec.js +65 -65
- package/src/components/__tests__/PluralFormat.spec.js +27 -27
- package/src/components/__tests__/UserTimeDiffFormat.spec.js +1076 -1076
- package/src/components/__tests__/__snapshots__/DateTimeDiffFormat.spec.js.snap +258 -258
- package/src/components/__tests__/__snapshots__/FormatText.spec.js.snap +17 -17
- package/src/components/__tests__/__snapshots__/HOCI18N.spec.js.snap +15 -15
- package/src/components/__tests__/__snapshots__/I18N.spec.js.snap +17 -17
- package/src/components/__tests__/__snapshots__/I18NProvider.spec.js.snap +13 -13
- package/src/components/__tests__/__snapshots__/PluralFormat.spec.js.snap +17 -17
- package/src/components/__tests__/__snapshots__/UserTimeDiffFormat.spec.js.snap +366 -366
- package/src/index.js +37 -37
- package/src/utils/__tests__/jsxTranslations.spec.js +213 -0
- package/src/{utils.js → utils/index.js} +632 -612
- package/src/utils/jsxTranslations.js +180 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _react = _interopRequireDefault(require("react"));
|
|
4
|
+
|
|
5
|
+
var _jsxTranslations = require("../jsxTranslations");
|
|
6
|
+
|
|
7
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
8
|
+
|
|
9
|
+
function Bold(_ref) {
|
|
10
|
+
var children = _ref.children;
|
|
11
|
+
return /*#__PURE__*/_react["default"].createElement("b", null, children);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function Simple(props) {
|
|
15
|
+
return /*#__PURE__*/_react["default"].createElement("span", null, props.children);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe('Verifying splitMatchedExp method', function () {
|
|
19
|
+
it('A string with no match test', function () {
|
|
20
|
+
var response = (0, _jsxTranslations.splitMatchedExp)({
|
|
21
|
+
expression: '{[0-9]+?}',
|
|
22
|
+
charLenToOmit: 1,
|
|
23
|
+
string: "Hello"
|
|
24
|
+
});
|
|
25
|
+
var expected = {
|
|
26
|
+
splitString: ['Hello'],
|
|
27
|
+
matchedExpKeys: []
|
|
28
|
+
};
|
|
29
|
+
expect(response).toEqual(expected);
|
|
30
|
+
});
|
|
31
|
+
it('A string with JSX Tag placeholder test', function () {
|
|
32
|
+
var response = (0, _jsxTranslations.splitMatchedExp)({
|
|
33
|
+
expression: '<[A-Za-z0-9]+?>',
|
|
34
|
+
charLenToOmit: 1,
|
|
35
|
+
string: "Hi <00Good> occurs <01Strong>"
|
|
36
|
+
});
|
|
37
|
+
var expected = {
|
|
38
|
+
splitString: ["Hi ", " occurs ", ""],
|
|
39
|
+
matchedExpKeys: ['00Good', '01Strong']
|
|
40
|
+
};
|
|
41
|
+
expect(response).toEqual(expected);
|
|
42
|
+
});
|
|
43
|
+
it('A string with String placeholder test', function () {
|
|
44
|
+
var response = (0, _jsxTranslations.splitMatchedExp)({
|
|
45
|
+
expression: '{[0-9]+?}',
|
|
46
|
+
charLenToOmit: 1,
|
|
47
|
+
string: "Hi {0} occurs {1}"
|
|
48
|
+
});
|
|
49
|
+
var expected = {
|
|
50
|
+
splitString: ["Hi ", " occurs ", ""],
|
|
51
|
+
matchedExpKeys: ['0', '1']
|
|
52
|
+
};
|
|
53
|
+
expect(response).toEqual(expected);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
describe('Verifying generateChildren Method', function () {
|
|
57
|
+
it('A string with Tag Placeholder and String placeholders', function () {
|
|
58
|
+
var children = {
|
|
59
|
+
'00Good': function Good() {},
|
|
60
|
+
'01Strong': function Strong() {}
|
|
61
|
+
};
|
|
62
|
+
var response = (0, _jsxTranslations.generateChildren)({
|
|
63
|
+
children: children,
|
|
64
|
+
child: 'Hi <00Good>{0}<01Strong>'
|
|
65
|
+
});
|
|
66
|
+
var expected = {
|
|
67
|
+
newChildren: [['Hi '], children['00Good'], ['', ''], children['01Strong']],
|
|
68
|
+
stringPlaceHolders: {
|
|
69
|
+
0: [],
|
|
70
|
+
2: ['0']
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
expect(response).toEqual(expected);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
describe('Verifying the replaceWithComponentPlaceHolder method', function () {
|
|
77
|
+
it('A JSX Tag with string', function () {
|
|
78
|
+
var response = (0, _jsxTranslations.replaceWithComponentPlaceHolder)({
|
|
79
|
+
componentId: 'Simple',
|
|
80
|
+
i18nKey: '<Simple>Hi everyone</Simple>',
|
|
81
|
+
childrenLength: 1
|
|
82
|
+
});
|
|
83
|
+
var expected = '<01Simple>';
|
|
84
|
+
expect(response).toBe(expected);
|
|
85
|
+
});
|
|
86
|
+
it('A string with multiple JSX Tags', function () {
|
|
87
|
+
var response = (0, _jsxTranslations.replaceWithComponentPlaceHolder)({
|
|
88
|
+
componentId: 'Simple',
|
|
89
|
+
i18nKey: 'Greetings <Simple>Hi everyone</Simple> for the <Simple>Day</Simple>',
|
|
90
|
+
childrenLength: 1
|
|
91
|
+
});
|
|
92
|
+
var expected = 'Greetings <01Simple> for the <Simple>Day</Simple>';
|
|
93
|
+
expect(response).toBe(expected);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
describe('Verifying the createElement method', function () {
|
|
97
|
+
it('A component Id with JSX children and Dynamic String placeholders', function () {
|
|
98
|
+
var jsxChild = (0, _jsxTranslations.createElement)({
|
|
99
|
+
componentId: 'Simple',
|
|
100
|
+
children: [['Greetings']],
|
|
101
|
+
stringPlaceHolders: {
|
|
102
|
+
0: []
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
var response = (0, _jsxTranslations.createElement)({
|
|
106
|
+
componentId: -1,
|
|
107
|
+
children: [['Hi ', ','], jsxChild, [' for the day']],
|
|
108
|
+
stringPlaceHolders: {
|
|
109
|
+
0: ['0'],
|
|
110
|
+
2: []
|
|
111
|
+
}
|
|
112
|
+
})({
|
|
113
|
+
components: {
|
|
114
|
+
Simple: {
|
|
115
|
+
type: Simple
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
values: ['Zoho Desk']
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
var expected = /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, "Hi Zoho Desk,", /*#__PURE__*/_react["default"].createElement(Simple, null, "Greetings"), " for the day");
|
|
122
|
+
|
|
123
|
+
expect(JSON.stringify(response)).toEqual(JSON.stringify(expected));
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
describe('Verifying the prepareI18NFunc method', function () {
|
|
127
|
+
//Hi <Bo01ld>Gigiee</Bo01ld>,<Simple> How are you</Simple><Simple>Make it simple</Simple>
|
|
128
|
+
it('A text with sibling JSX tags and alphanumeric JSX Tags', function () {
|
|
129
|
+
var response = (0, _jsxTranslations.prepareI18NFunc)({
|
|
130
|
+
i18nKey: 'Hi <Bo01ld>Gigiee</Bo01ld>,<Simple>How are you</Simple><Simple>Make it simple</Simple>'
|
|
131
|
+
})({
|
|
132
|
+
components: {
|
|
133
|
+
'Bo01ld': {
|
|
134
|
+
type: Bold
|
|
135
|
+
},
|
|
136
|
+
Simple: {
|
|
137
|
+
type: Simple
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
var expected = /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, "Hi ", /*#__PURE__*/_react["default"].createElement(Bold, null, "Gigiee"), ",", /*#__PURE__*/_react["default"].createElement(Simple, null, "How are you"), /*#__PURE__*/_react["default"].createElement(Simple, null, "Make it simple"));
|
|
143
|
+
|
|
144
|
+
expect(JSON.stringify(response)).toEqual(JSON.stringify(expected));
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
describe('Verifying getI18NComponent method', function () {
|
|
148
|
+
it('An i18n key with JSX tag in its value', function () {
|
|
149
|
+
var i18n = {
|
|
150
|
+
'support.demo': 'Hi <Bo01ld>Gigiee</Bo01ld>,<Simple>How are you</Simple><Simple>Make it simple</Simple>'
|
|
151
|
+
};
|
|
152
|
+
var I18N = (0, _jsxTranslations.getI18NComponent)(i18n);
|
|
153
|
+
var response = I18N('support.demo')({
|
|
154
|
+
components: {
|
|
155
|
+
'Bo01ld': {
|
|
156
|
+
type: Bold
|
|
157
|
+
},
|
|
158
|
+
Simple: {
|
|
159
|
+
type: Simple
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
var expected = /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, "Hi ", /*#__PURE__*/_react["default"].createElement(Bold, null, "Gigiee"), ",", /*#__PURE__*/_react["default"].createElement(Simple, null, "How are you"), /*#__PURE__*/_react["default"].createElement(Simple, null, "Make it simple"));
|
|
165
|
+
|
|
166
|
+
expect(JSON.stringify(response)).toEqual(JSON.stringify(expected));
|
|
167
|
+
});
|
|
168
|
+
it('Testing absence of i18n object', function () {
|
|
169
|
+
var I18N = (0, _jsxTranslations.getI18NComponent)();
|
|
170
|
+
var response = I18N('support.demo');
|
|
171
|
+
var expected = 'support.demo';
|
|
172
|
+
expect(expected).toBe(response);
|
|
173
|
+
});
|
|
174
|
+
it('Testing of missing i18n key', function () {
|
|
175
|
+
var i18n = {
|
|
176
|
+
'support.demo': 'Demo'
|
|
177
|
+
};
|
|
178
|
+
var I18N = (0, _jsxTranslations.getI18NComponent)(i18n);
|
|
179
|
+
var response = I18N('support.need');
|
|
180
|
+
var expected = 'support.need';
|
|
181
|
+
expect(expected).toBe(response);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
@@ -30,7 +30,7 @@ exports.userDateFormat = userDateFormat;
|
|
|
30
30
|
|
|
31
31
|
var _datetimejs = _interopRequireDefault(require("@zohodesk/datetimejs"));
|
|
32
32
|
|
|
33
|
-
var _I18NProvider = require("
|
|
33
|
+
var _I18NProvider = require("../components/I18NProvider");
|
|
34
34
|
|
|
35
35
|
var _monthi18n;
|
|
36
36
|
|
|
@@ -241,9 +241,9 @@ function setLocalizedStringData(data) {
|
|
|
241
241
|
var keys = Object.keys(data);
|
|
242
242
|
|
|
243
243
|
for (var i = 0; i < keys.length; i++) {
|
|
244
|
-
var
|
|
245
|
-
var newKeyString = keyString ? "".concat(keyString, ".").concat(
|
|
246
|
-
setLocalizedStringData(data[
|
|
244
|
+
var _key = keys[i];
|
|
245
|
+
var newKeyString = keyString ? "".concat(keyString, ".").concat(_key) : _key;
|
|
246
|
+
setLocalizedStringData(data[_key], newKeyString);
|
|
247
247
|
}
|
|
248
248
|
}
|
|
249
249
|
|
|
@@ -261,8 +261,13 @@ function getLocalizedValue() {
|
|
|
261
261
|
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
262
262
|
type = _ref.type,
|
|
263
263
|
moduleName = _ref.moduleName,
|
|
264
|
+
deptName = _ref.deptName,
|
|
265
|
+
folderName = _ref.folderName,
|
|
266
|
+
reportId = _ref.reportId,
|
|
267
|
+
reportName = _ref.reportName,
|
|
264
268
|
apiName = _ref.apiName,
|
|
265
|
-
fieldValue = _ref.fieldValue
|
|
269
|
+
fieldValue = _ref.fieldValue,
|
|
270
|
+
layoutName = _ref.layoutName;
|
|
266
271
|
|
|
267
272
|
var localizedValue = localizedData;
|
|
268
273
|
|
|
@@ -279,6 +284,24 @@ function getLocalizedValue() {
|
|
|
279
284
|
});
|
|
280
285
|
break;
|
|
281
286
|
|
|
287
|
+
case 'layout':
|
|
288
|
+
if (deptName != null) {
|
|
289
|
+
['Layout', moduleName, deptName, layoutName].map(function () {
|
|
290
|
+
localizedValue = localizedValue[key] || '';
|
|
291
|
+
});
|
|
292
|
+
} else {
|
|
293
|
+
['Layout', moduleName, layoutName].map(function () {
|
|
294
|
+
localizedValue = localizedValue[key] || '';
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
break;
|
|
299
|
+
|
|
300
|
+
case 'report':
|
|
301
|
+
['Report', deptName, folderName, reportName, reportId].map(function (key) {
|
|
302
|
+
localizedValue = localizedValue[key] || '';
|
|
303
|
+
});
|
|
304
|
+
|
|
282
305
|
default:
|
|
283
306
|
return null;
|
|
284
307
|
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createElement = createElement;
|
|
7
|
+
exports.generateChildren = generateChildren;
|
|
8
|
+
exports.getI18NComponent = getI18NComponent;
|
|
9
|
+
exports.prepareI18NFunc = prepareI18NFunc;
|
|
10
|
+
exports.replaceWithComponentPlaceHolder = replaceWithComponentPlaceHolder;
|
|
11
|
+
exports.splitMatchedExp = splitMatchedExp;
|
|
12
|
+
|
|
13
|
+
var _react = _interopRequireDefault(require("react"));
|
|
14
|
+
|
|
15
|
+
var _index = require("./index");
|
|
16
|
+
|
|
17
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
18
|
+
|
|
19
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
20
|
+
|
|
21
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
22
|
+
|
|
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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
26
|
+
|
|
27
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
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
|
+
|
|
35
|
+
function splitMatchedExp(_ref) {
|
|
36
|
+
var expression = _ref.expression,
|
|
37
|
+
charLenToOmit = _ref.charLenToOmit,
|
|
38
|
+
string = _ref.string;
|
|
39
|
+
var splitString = string.split(new RegExp(expression, 'g'));
|
|
40
|
+
var matchedExpAll = string.matchAll(new RegExp(expression, 'g'));
|
|
41
|
+
var matchedExpKeys = [];
|
|
42
|
+
|
|
43
|
+
var _iterator = _createForOfIteratorHelper(matchedExpAll),
|
|
44
|
+
_step;
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
48
|
+
var match = _step.value;
|
|
49
|
+
var value = match[0];
|
|
50
|
+
matchedExpKeys.push(value.substring(charLenToOmit, value.length - charLenToOmit));
|
|
51
|
+
}
|
|
52
|
+
} catch (err) {
|
|
53
|
+
_iterator.e(err);
|
|
54
|
+
} finally {
|
|
55
|
+
_iterator.f();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
splitString: splitString,
|
|
60
|
+
matchedExpKeys: matchedExpKeys
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function generateChildren(_ref2) {
|
|
65
|
+
var children = _ref2.children,
|
|
66
|
+
child = _ref2.child;
|
|
67
|
+
var newChildren = [],
|
|
68
|
+
stringPlaceHolders = {};
|
|
69
|
+
|
|
70
|
+
var handleString = function handleString(string) {
|
|
71
|
+
if (string) {
|
|
72
|
+
var _splitMatchedExp = splitMatchedExp({
|
|
73
|
+
expression: '{[0-9]+?}',
|
|
74
|
+
charLenToOmit: 1,
|
|
75
|
+
string: string
|
|
76
|
+
}),
|
|
77
|
+
splitString = _splitMatchedExp.splitString,
|
|
78
|
+
matchedExpKeys = _splitMatchedExp.matchedExpKeys;
|
|
79
|
+
|
|
80
|
+
stringPlaceHolders[newChildren.length] = matchedExpKeys;
|
|
81
|
+
newChildren.push(splitString);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
var _splitMatchedExp2 = splitMatchedExp({
|
|
86
|
+
expression: '<[A-Za-z0-9]+?>',
|
|
87
|
+
charLenToOmit: 1,
|
|
88
|
+
string: child
|
|
89
|
+
}),
|
|
90
|
+
splitChild = _splitMatchedExp2.splitString,
|
|
91
|
+
childrenKeys = _splitMatchedExp2.matchedExpKeys;
|
|
92
|
+
|
|
93
|
+
childrenKeys.forEach(function (childKey, childIndex) {
|
|
94
|
+
var matchedChild = splitChild[childIndex];
|
|
95
|
+
handleString(matchedChild);
|
|
96
|
+
newChildren.push(children[childKey]);
|
|
97
|
+
});
|
|
98
|
+
handleString(splitChild[childrenKeys.length]);
|
|
99
|
+
return {
|
|
100
|
+
newChildren: newChildren,
|
|
101
|
+
stringPlaceHolders: stringPlaceHolders
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function createElement() {
|
|
106
|
+
var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
107
|
+
componentId = _ref3.componentId,
|
|
108
|
+
_ref3$children = _ref3.children,
|
|
109
|
+
children = _ref3$children === void 0 ? [] : _ref3$children,
|
|
110
|
+
_ref3$stringPlaceHold = _ref3.stringPlaceHolders,
|
|
111
|
+
stringPlaceHolders = _ref3$stringPlaceHold === void 0 ? {} : _ref3$stringPlaceHold;
|
|
112
|
+
|
|
113
|
+
return function () {
|
|
114
|
+
var _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
115
|
+
_ref4$components = _ref4.components,
|
|
116
|
+
components = _ref4$components === void 0 ? {} : _ref4$components,
|
|
117
|
+
_ref4$values = _ref4.values,
|
|
118
|
+
values = _ref4$values === void 0 ? [] : _ref4$values;
|
|
119
|
+
|
|
120
|
+
var _ref5 = components[componentId] || {},
|
|
121
|
+
_ref5$type = _ref5.type,
|
|
122
|
+
type = _ref5$type === void 0 ? _react["default"].Fragment : _ref5$type,
|
|
123
|
+
props = _ref5.props;
|
|
124
|
+
|
|
125
|
+
var childElements = children.map(function (child, index) {
|
|
126
|
+
if (typeof child === 'function') {
|
|
127
|
+
return child({
|
|
128
|
+
components: components,
|
|
129
|
+
values: values
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
var string = '',
|
|
134
|
+
stringIndex = 0;
|
|
135
|
+
(stringPlaceHolders[index] || []).map(function (stringId, index) {
|
|
136
|
+
if (child[index]) {
|
|
137
|
+
string += child[index];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
string += values[stringId];
|
|
141
|
+
stringIndex++;
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
if (child[stringIndex]) {
|
|
145
|
+
string += child[stringIndex];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return string;
|
|
149
|
+
});
|
|
150
|
+
return /*#__PURE__*/_react["default"].createElement.apply(_react["default"], [type, props].concat(_toConsumableArray(childElements)));
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
;
|
|
155
|
+
|
|
156
|
+
function replaceWithComponentPlaceHolder(_ref6) {
|
|
157
|
+
var componentId = _ref6.componentId,
|
|
158
|
+
i18nKey = _ref6.i18nKey,
|
|
159
|
+
childrenLength = _ref6.childrenLength;
|
|
160
|
+
var closingTagIndex = i18nKey.indexOf("</".concat(componentId, ">"));
|
|
161
|
+
var childWithOpenTag = i18nKey.substring(0, closingTagIndex);
|
|
162
|
+
var openTagIndex = childWithOpenTag.lastIndexOf("<".concat(componentId, ">"));
|
|
163
|
+
return i18nKey.substring(0, openTagIndex) + "<0".concat(childrenLength).concat(componentId, ">") + i18nKey.substring(closingTagIndex + "".concat(componentId).length + 3);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function prepareI18NFunc() {
|
|
167
|
+
var _ref7 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
168
|
+
i18nKey = _ref7.i18nKey,
|
|
169
|
+
_ref7$children = _ref7.children,
|
|
170
|
+
children = _ref7$children === void 0 ? {} : _ref7$children;
|
|
171
|
+
|
|
172
|
+
var componentIdMatch = i18nKey.match(/<\/[A-Za-z0-9]+?>/);
|
|
173
|
+
|
|
174
|
+
if (componentIdMatch) {
|
|
175
|
+
var componentId = componentIdMatch[0].substring(2, componentIdMatch[0].length - 1);
|
|
176
|
+
var childWithOpenTag = (i18nKey.match(new RegExp("(.*?)(?=</".concat(componentId, ">)"))) || [])[0] || '';
|
|
177
|
+
var child = childWithOpenTag.split("<".concat(componentId, ">")).pop();
|
|
178
|
+
|
|
179
|
+
var _generateChildren = generateChildren({
|
|
180
|
+
children: children,
|
|
181
|
+
child: child
|
|
182
|
+
}),
|
|
183
|
+
_newChildren = _generateChildren.newChildren,
|
|
184
|
+
_stringPlaceHolders = _generateChildren.stringPlaceHolders;
|
|
185
|
+
|
|
186
|
+
var childrenLength = Object.keys(children).length;
|
|
187
|
+
children["0".concat(childrenLength).concat(componentId)] = createElement({
|
|
188
|
+
componentId: componentId,
|
|
189
|
+
children: _newChildren,
|
|
190
|
+
stringPlaceHolders: _stringPlaceHolders
|
|
191
|
+
});
|
|
192
|
+
i18nKey = replaceWithComponentPlaceHolder({
|
|
193
|
+
componentId: componentId,
|
|
194
|
+
i18nKey: i18nKey,
|
|
195
|
+
childrenLength: childrenLength
|
|
196
|
+
});
|
|
197
|
+
return prepareI18NFunc({
|
|
198
|
+
i18nKey: i18nKey,
|
|
199
|
+
children: children
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
var _generateChildren2 = generateChildren({
|
|
204
|
+
child: i18nKey,
|
|
205
|
+
children: children
|
|
206
|
+
}),
|
|
207
|
+
newChildren = _generateChildren2.newChildren,
|
|
208
|
+
stringPlaceHolders = _generateChildren2.stringPlaceHolders;
|
|
209
|
+
|
|
210
|
+
return createElement({
|
|
211
|
+
componentId: -1,
|
|
212
|
+
children: newChildren,
|
|
213
|
+
stringPlaceHolders: stringPlaceHolders
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function getI18NComponent(i18n) {
|
|
218
|
+
if (typeof i18n === 'undefined') {
|
|
219
|
+
return function (key) {
|
|
220
|
+
return key;
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return function (key) {
|
|
225
|
+
var i18nStr = i18n[key];
|
|
226
|
+
|
|
227
|
+
if (i18nStr === undefined) {
|
|
228
|
+
return key;
|
|
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;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return i18nStr;
|
|
241
|
+
};
|
|
242
|
+
}
|
package/package.json
CHANGED
|
@@ -1,29 +1,30 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@zohodesk/i18n",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
4
|
-
"main": "lib/index",
|
|
5
|
-
"module": "es/index.js",
|
|
6
|
-
"jsnext:main": "es/index.js",
|
|
7
|
-
"private": false,
|
|
8
|
-
"umdVar": "i18n",
|
|
9
|
-
"author": "",
|
|
10
|
-
"license": "ISC",
|
|
11
|
-
"scripts": {
|
|
12
|
-
"clean": "react-cli clean lib es coverage",
|
|
13
|
-
"build": "react-cli build:component:cmjs",
|
|
14
|
-
"build:es": "react-cli build:library:es",
|
|
15
|
-
"build:dev": "npm run clean && npm run build --module:mode=dev -- -w",
|
|
16
|
-
"prepublish": "npm run init && npm run build && npm run build:es ",
|
|
17
|
-
"prepare": "npm run init && npm run build && npm run build:es",
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@zohodesk/i18n",
|
|
3
|
+
"version": "1.0.0-beta.28",
|
|
4
|
+
"main": "lib/index",
|
|
5
|
+
"module": "es/index.js",
|
|
6
|
+
"jsnext:main": "es/index.js",
|
|
7
|
+
"private": false,
|
|
8
|
+
"umdVar": "i18n",
|
|
9
|
+
"author": "",
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"clean": "react-cli clean lib es coverage",
|
|
13
|
+
"build": "react-cli build:component:cmjs",
|
|
14
|
+
"build:es": "react-cli build:library:es",
|
|
15
|
+
"build:dev": "npm run clean && npm run build --module:mode=dev -- -w",
|
|
16
|
+
"prepublish": "npm run init && npm run build && npm run build:es ",
|
|
17
|
+
"prepare": "npm run init && npm run build && npm run build:es",
|
|
18
|
+
"test": "react-cli test",
|
|
19
|
+
"init": "npm run clean",
|
|
20
|
+
"lint": "react-cli lint"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@zoho/SecurityJS": "7.0.2"
|
|
24
|
+
},
|
|
25
|
+
"react-cli": {
|
|
26
|
+
"docs": {
|
|
27
|
+
"componentFolder": "./src"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/I18NContext.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
3
|
export const I18NContext = React.createContext();
|