@zohodesk/i18n 1.0.0-beta.23 → 1.0.0-beta.25

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.
Files changed (44) hide show
  1. package/README.md +101 -92
  2. package/es/components/__tests__/__snapshots__/DateTimeDiffFormat.spec.js.snap +258 -258
  3. package/es/components/__tests__/__snapshots__/FormatText.spec.js.snap +17 -17
  4. package/es/components/__tests__/__snapshots__/HOCI18N.spec.js.snap +15 -15
  5. package/es/components/__tests__/__snapshots__/I18N.spec.js.snap +17 -17
  6. package/es/components/__tests__/__snapshots__/I18NProvider.spec.js.snap +13 -13
  7. package/es/components/__tests__/__snapshots__/PluralFormat.spec.js.snap +17 -17
  8. package/es/components/__tests__/__snapshots__/UserTimeDiffFormat.spec.js.snap +366 -366
  9. package/es/index.js +1 -1
  10. package/es/utils.js +62 -9
  11. package/lib/components/__tests__/__snapshots__/DateTimeDiffFormat.spec.js.snap +258 -258
  12. package/lib/components/__tests__/__snapshots__/FormatText.spec.js.snap +17 -17
  13. package/lib/components/__tests__/__snapshots__/HOCI18N.spec.js.snap +15 -15
  14. package/lib/components/__tests__/__snapshots__/I18N.spec.js.snap +17 -17
  15. package/lib/components/__tests__/__snapshots__/I18NProvider.spec.js.snap +13 -13
  16. package/lib/components/__tests__/__snapshots__/PluralFormat.spec.js.snap +17 -17
  17. package/lib/components/__tests__/__snapshots__/UserTimeDiffFormat.spec.js.snap +366 -366
  18. package/lib/index.js +18 -0
  19. package/lib/utils.js +65 -9
  20. package/package.json +29 -29
  21. package/src/I18NContext.js +2 -2
  22. package/src/components/DateTimeDiffFormat.js +256 -256
  23. package/src/components/FormatText.js +14 -14
  24. package/src/components/HOCI18N.js +37 -37
  25. package/src/components/I18N.js +74 -74
  26. package/src/components/I18NProvider.js +110 -110
  27. package/src/components/PluralFormat.js +37 -37
  28. package/src/components/UserTimeDiffFormat.js +97 -97
  29. package/src/components/__tests__/DateTimeDiffFormat.spec.js +618 -618
  30. package/src/components/__tests__/FormatText.spec.js +26 -26
  31. package/src/components/__tests__/HOCI18N.spec.js +33 -33
  32. package/src/components/__tests__/I18N.spec.js +29 -29
  33. package/src/components/__tests__/I18NProvider.spec.js +65 -65
  34. package/src/components/__tests__/PluralFormat.spec.js +27 -27
  35. package/src/components/__tests__/UserTimeDiffFormat.spec.js +1076 -1076
  36. package/src/components/__tests__/__snapshots__/DateTimeDiffFormat.spec.js.snap +258 -258
  37. package/src/components/__tests__/__snapshots__/FormatText.spec.js.snap +17 -17
  38. package/src/components/__tests__/__snapshots__/HOCI18N.spec.js.snap +15 -15
  39. package/src/components/__tests__/__snapshots__/I18N.spec.js.snap +17 -17
  40. package/src/components/__tests__/__snapshots__/I18NProvider.spec.js.snap +13 -13
  41. package/src/components/__tests__/__snapshots__/PluralFormat.spec.js.snap +17 -17
  42. package/src/components/__tests__/__snapshots__/UserTimeDiffFormat.spec.js.snap +366 -366
  43. package/src/index.js +36 -33
  44. package/src/utils.js +585 -527
@@ -1,256 +1,256 @@
1
- import React from 'react';
2
- import PropTypes from 'prop-types';
3
- import {
4
- pad,
5
- getValues,
6
- getDiffObj,
7
- formatDate,
8
- getLyears,
9
- getDatePatternWithoutYear,
10
- } from '../utils';
11
- import FormatText from './FormatText';
12
- import datetime from '@zohodesk/datetimejs';
13
- export default class DateTimeDiffFormat extends React.Component {
14
- constructor(props) {
15
- super(props);
16
- this.getSuffix = this.getSuffix.bind(this);
17
- }
18
-
19
- getSuffix(min) {
20
- let suffix;
21
- if (this.props.ago && min < 0) {
22
- suffix = this.props.ago || '';
23
- } else if (this.props.later || min > 0) {
24
- suffix = this.props.later || '';
25
- } else {
26
- suffix = '';
27
- }
28
- return suffix;
29
- }
30
-
31
- render() {
32
- const {
33
- type,
34
- page,
35
- isNeedTime,
36
- from,
37
- fromTzData,
38
- to,
39
- toTzData,
40
- today,
41
- yesterday,
42
- tomorrow,
43
- others,
44
- format,
45
- dataId,
46
- className = null,
47
- title = null,
48
- isOverdue,
49
- timeFormat,
50
- datePattern,
51
- isEnabledCurrentYear,
52
- isDateField
53
- } = this.props;
54
-
55
- let fromDateObj = datetime
56
- .toDate(datetime.tz.utcToTz(from, fromTzData))
57
- .getTime();
58
- let toDateObj = datetime
59
- .toDate(isDateField ? to : datetime.tz.utcToTz(to, toTzData))
60
- .getTime();
61
-
62
- let diffMin = new Date(to).getTime() - new Date(from).getTime();
63
- let suffix = this.getSuffix(diffMin);
64
- let diff = getDiffObj(diffMin);
65
- let withInAWeak = diff.y === 0 && diff.yd <= 7;
66
-
67
- let diffObj = {
68
- h: diff.h,
69
- m: diff.m,
70
- s: diff.s,
71
- y: diff.y,
72
- hh: pad(diff.h, 2),
73
- mm: pad(diff.m, 2),
74
- ss: pad(diff.s, 2),
75
- yy: pad(diff.y, 2),
76
- days: diff.yd,
77
- yDays: pad(diff.yd, 2),
78
- isWithInAWeek: withInAWeak,
79
- suffix: suffix,
80
- };
81
- let diffObj1 = {
82
- to: to,
83
- type: type,
84
- page: page,
85
- isNeedTime: isNeedTime,
86
- hours: diff.h,
87
- minutes: diff.m,
88
- seconds: diff.s,
89
- years: diff.y,
90
- yDays: diff.yd,
91
- isWithInAWeek: withInAWeak,
92
- suffix: suffix,
93
- crntDate: new Date(from).getDate(),
94
- crntMonth: new Date(from).getMonth(),
95
- crntYear: new Date(from).getFullYear(),
96
- crntHours: new Date(from).getHours(),
97
- crntMinutes: new Date(from).getMinutes(),
98
- crntSeconds: new Date(from).getSeconds(),
99
- tDate: new Date(to).getDate(),
100
- tMonth: new Date(to).getMonth(),
101
- tYear: new Date(to).getFullYear(),
102
- tHours: new Date(to).getHours(),
103
- tMinutes: new Date(to).getMinutes(),
104
- tSeconds: new Date(to).getSeconds(),
105
- betweenleepYears: getLyears(from, to),
106
- isOverdue: isOverdue,
107
- timeFormat: timeFormat,
108
- datePattern: datePattern,
109
- dateTimePattern: `${datePattern} ${timeFormat}`,
110
- };
111
-
112
- //In if condition we'll remove year and set date format if the current year is not required
113
- //In else part we'll set the date format as it is
114
- if (
115
- isEnabledCurrentYear === true &&
116
- diffObj1.years === 0 &&
117
- diffObj1.tYear === diffObj1.crntYear
118
- ) {
119
- let dateFormat = getDatePatternWithoutYear(datePattern);
120
- diffObj1.dateFormat = dateFormat;
121
- diffObj1.dateTimeFormat = `${dateFormat} ${timeFormat}`;
122
- } else {
123
- diffObj1.dateFormat = datePattern;
124
- diffObj1.dateTimeFormat = `${datePattern} ${timeFormat}`;
125
- }
126
-
127
- let key = '';
128
- let values = [];
129
- let text = null;
130
- let isSuffixEnable = false;
131
- if (format) {
132
- let years, months, days, hours, minutes, seconds;
133
- years = diffObj1.years > 1 ? '2' : diffObj1.years;
134
-
135
- days = diff.yd > 1 ? '2' : diff.yd;
136
- hours = diffObj1.hours > 1 ? '2' : diffObj1.hours;
137
- minutes = diffObj1.minutes > 1 ? '2' : diffObj1.minutes;
138
-
139
- let count = 0;
140
- let pattern = [years, days, hours, minutes].reduce((res, next) => {
141
- if (count === 2) {
142
- res = `${res}0`;
143
- } else if (next !== 0) {
144
- count++;
145
- res = res + next;
146
- } else {
147
- res = res + next;
148
- }
149
- return res;
150
- }, '');
151
-
152
- let value = format(diffObj1, pattern);
153
- if (value && typeof value === 'object') {
154
- key = value.key;
155
- values = getValues(value.params, diffObj);
156
- if (pattern.indexOf('00000') === 0) {
157
- //suffix ignore for second hook
158
- isSuffixEnable = false;
159
- } else {
160
- isSuffixEnable = true;
161
- }
162
- } else if (typeof value === 'string') {
163
- text = formatDate(toDateObj, value, diffObj1);
164
- }
165
- } else {
166
- let dateObj = new Date(toDateObj);
167
- let curDateObj = new Date(fromDateObj);
168
- let diffDayType = diffObj1.yDays;
169
-
170
- //In this condition, to calculate different days we have copied it from live --> diffDayType
171
- if (
172
- isOverdue &&
173
- dateObj.getDate() < curDateObj.getDate() &&
174
- diffObj1.yDays == 0
175
- ) {
176
- diffDayType = -1;
177
- }
178
- if (!isOverdue) {
179
- let diffHr = dateObj.getHours() - curDateObj.getHours();
180
- if (diffHr < 0) {
181
- diffDayType += 1;
182
- } else if (diffHr == 0) {
183
- let diffMins = dateObj.getMinutes() - curDateObj.getMinutes();
184
- if (diffMins < 0) {
185
- diffDayType += 1;
186
- } else if (diffMins == 0) {
187
- let diffSec = dateObj.getSeconds() - curDateObj.getSeconds();
188
- if (diffSec < 0) {
189
- diffDayType += 1;
190
- }
191
- }
192
- }
193
- }
194
- if (diff.y === 0 && (diffDayType === 0 || diffDayType === 1)) {
195
- if (dateObj.getDate() === curDateObj.getDate()) {
196
- var value = (today && today(diffObj1)) || others(diffObj1);
197
- text = formatDate(toDateObj, value);
198
- } else if (
199
- (dateObj.getMonth() === curDateObj.getMonth() &&
200
- dateObj.getDate() < curDateObj.getDate()) ||
201
- dateObj.getMonth() < curDateObj.getMonth()
202
- ) {
203
- var value = (yesterday && yesterday(diffObj1)) || others(diffObj1);
204
- text = formatDate(toDateObj, value);
205
- } else if (!isOverdue && diff.y === 0 && diffDayType === 1) {
206
- var value = (tomorrow && tomorrow(diffObj1)) || others(diffObj1);
207
- text = formatDate(toDateObj, value);
208
- }
209
- } else {
210
- var value = others(diffObj1);
211
- text = formatDate(toDateObj, value);
212
- }
213
- }
214
- return text ? (
215
- <span className={className} data-title={title} data-id={dataId} data-test-id={dataId}>
216
- {text}
217
- </span>
218
- ) : (
219
- <FormatText
220
- i18NKey={isSuffixEnable && suffix !== '' ? `${key}.${suffix}` : key}
221
- values={values}
222
- className={className}
223
- data-title={title}
224
- />
225
- );
226
- }
227
- }
228
- DateTimeDiffFormat.propTypes = {
229
- ago: PropTypes.string,
230
- className: PropTypes.string,
231
- dataId: PropTypes.string,
232
- format: PropTypes.func,
233
- from: PropTypes.string,
234
- fromTzData: PropTypes.object,
235
- isDateField: PropTypes.bool,
236
- later: PropTypes.string,
237
- others: PropTypes.func,
238
- title: PropTypes.string,
239
- to: PropTypes.string,
240
- toTzData: PropTypes.object,
241
- today: PropTypes.oneOfType([
242
- PropTypes.string,
243
- PropTypes.object,
244
- PropTypes.func,
245
- ]),
246
- tomorrow: PropTypes.oneOfType([
247
- PropTypes.string,
248
- PropTypes.object,
249
- PropTypes.func,
250
- ]),
251
- yesterday: PropTypes.oneOfType([
252
- PropTypes.string,
253
- PropTypes.object,
254
- PropTypes.func,
255
- ]),
256
- };
1
+ import React from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import {
4
+ pad,
5
+ getValues,
6
+ getDiffObj,
7
+ formatDate,
8
+ getLyears,
9
+ getDatePatternWithoutYear,
10
+ } from '../utils';
11
+ import FormatText from './FormatText';
12
+ import datetime from '@zohodesk/datetimejs';
13
+ export default class DateTimeDiffFormat extends React.Component {
14
+ constructor(props) {
15
+ super(props);
16
+ this.getSuffix = this.getSuffix.bind(this);
17
+ }
18
+
19
+ getSuffix(min) {
20
+ let suffix;
21
+ if (this.props.ago && min < 0) {
22
+ suffix = this.props.ago || '';
23
+ } else if (this.props.later || min > 0) {
24
+ suffix = this.props.later || '';
25
+ } else {
26
+ suffix = '';
27
+ }
28
+ return suffix;
29
+ }
30
+
31
+ render() {
32
+ const {
33
+ type,
34
+ page,
35
+ isNeedTime,
36
+ from,
37
+ fromTzData,
38
+ to,
39
+ toTzData,
40
+ today,
41
+ yesterday,
42
+ tomorrow,
43
+ others,
44
+ format,
45
+ dataId,
46
+ className = null,
47
+ title = null,
48
+ isOverdue,
49
+ timeFormat,
50
+ datePattern,
51
+ isEnabledCurrentYear,
52
+ isDateField
53
+ } = this.props;
54
+
55
+ let fromDateObj = datetime
56
+ .toDate(datetime.tz.utcToTz(from, fromTzData))
57
+ .getTime();
58
+ let toDateObj = datetime
59
+ .toDate(isDateField ? to : datetime.tz.utcToTz(to, toTzData))
60
+ .getTime();
61
+
62
+ let diffMin = new Date(to).getTime() - new Date(from).getTime();
63
+ let suffix = this.getSuffix(diffMin);
64
+ let diff = getDiffObj(diffMin);
65
+ let withInAWeak = diff.y === 0 && diff.yd <= 7;
66
+
67
+ let diffObj = {
68
+ h: diff.h,
69
+ m: diff.m,
70
+ s: diff.s,
71
+ y: diff.y,
72
+ hh: pad(diff.h, 2),
73
+ mm: pad(diff.m, 2),
74
+ ss: pad(diff.s, 2),
75
+ yy: pad(diff.y, 2),
76
+ days: diff.yd,
77
+ yDays: pad(diff.yd, 2),
78
+ isWithInAWeek: withInAWeak,
79
+ suffix: suffix,
80
+ };
81
+ let diffObj1 = {
82
+ to: to,
83
+ type: type,
84
+ page: page,
85
+ isNeedTime: isNeedTime,
86
+ hours: diff.h,
87
+ minutes: diff.m,
88
+ seconds: diff.s,
89
+ years: diff.y,
90
+ yDays: diff.yd,
91
+ isWithInAWeek: withInAWeak,
92
+ suffix: suffix,
93
+ crntDate: new Date(from).getDate(),
94
+ crntMonth: new Date(from).getMonth(),
95
+ crntYear: new Date(from).getFullYear(),
96
+ crntHours: new Date(from).getHours(),
97
+ crntMinutes: new Date(from).getMinutes(),
98
+ crntSeconds: new Date(from).getSeconds(),
99
+ tDate: new Date(to).getDate(),
100
+ tMonth: new Date(to).getMonth(),
101
+ tYear: new Date(to).getFullYear(),
102
+ tHours: new Date(to).getHours(),
103
+ tMinutes: new Date(to).getMinutes(),
104
+ tSeconds: new Date(to).getSeconds(),
105
+ betweenleepYears: getLyears(from, to),
106
+ isOverdue: isOverdue,
107
+ timeFormat: timeFormat,
108
+ datePattern: datePattern,
109
+ dateTimePattern: `${datePattern} ${timeFormat}`,
110
+ };
111
+
112
+ //In if condition we'll remove year and set date format if the current year is not required
113
+ //In else part we'll set the date format as it is
114
+ if (
115
+ isEnabledCurrentYear === true &&
116
+ diffObj1.years === 0 &&
117
+ diffObj1.tYear === diffObj1.crntYear
118
+ ) {
119
+ let dateFormat = getDatePatternWithoutYear(datePattern);
120
+ diffObj1.dateFormat = dateFormat;
121
+ diffObj1.dateTimeFormat = `${dateFormat} ${timeFormat}`;
122
+ } else {
123
+ diffObj1.dateFormat = datePattern;
124
+ diffObj1.dateTimeFormat = `${datePattern} ${timeFormat}`;
125
+ }
126
+
127
+ let key = '';
128
+ let values = [];
129
+ let text = null;
130
+ let isSuffixEnable = false;
131
+ if (format) {
132
+ let years, months, days, hours, minutes, seconds;
133
+ years = diffObj1.years > 1 ? '2' : diffObj1.years;
134
+
135
+ days = diff.yd > 1 ? '2' : diff.yd;
136
+ hours = diffObj1.hours > 1 ? '2' : diffObj1.hours;
137
+ minutes = diffObj1.minutes > 1 ? '2' : diffObj1.minutes;
138
+
139
+ let count = 0;
140
+ let pattern = [years, days, hours, minutes].reduce((res, next) => {
141
+ if (count === 2) {
142
+ res = `${res}0`;
143
+ } else if (next !== 0) {
144
+ count++;
145
+ res = res + next;
146
+ } else {
147
+ res = res + next;
148
+ }
149
+ return res;
150
+ }, '');
151
+
152
+ let value = format(diffObj1, pattern);
153
+ if (value && typeof value === 'object') {
154
+ key = value.key;
155
+ values = getValues(value.params, diffObj);
156
+ if (pattern.indexOf('00000') === 0) {
157
+ //suffix ignore for second hook
158
+ isSuffixEnable = false;
159
+ } else {
160
+ isSuffixEnable = true;
161
+ }
162
+ } else if (typeof value === 'string') {
163
+ text = formatDate(toDateObj, value, diffObj1);
164
+ }
165
+ } else {
166
+ let dateObj = new Date(toDateObj);
167
+ let curDateObj = new Date(fromDateObj);
168
+ let diffDayType = diffObj1.yDays;
169
+
170
+ //In this condition, to calculate different days we have copied it from live --> diffDayType
171
+ if (
172
+ isOverdue &&
173
+ dateObj.getDate() < curDateObj.getDate() &&
174
+ diffObj1.yDays == 0
175
+ ) {
176
+ diffDayType = -1;
177
+ }
178
+ if (!isOverdue) {
179
+ let diffHr = dateObj.getHours() - curDateObj.getHours();
180
+ if (diffHr < 0) {
181
+ diffDayType += 1;
182
+ } else if (diffHr == 0) {
183
+ let diffMins = dateObj.getMinutes() - curDateObj.getMinutes();
184
+ if (diffMins < 0) {
185
+ diffDayType += 1;
186
+ } else if (diffMins == 0) {
187
+ let diffSec = dateObj.getSeconds() - curDateObj.getSeconds();
188
+ if (diffSec < 0) {
189
+ diffDayType += 1;
190
+ }
191
+ }
192
+ }
193
+ }
194
+ if (diff.y === 0 && (diffDayType === 0 || diffDayType === 1)) {
195
+ if (dateObj.getDate() === curDateObj.getDate()) {
196
+ var value = (today && today(diffObj1)) || others(diffObj1);
197
+ text = formatDate(toDateObj, value);
198
+ } else if (
199
+ (dateObj.getMonth() === curDateObj.getMonth() &&
200
+ dateObj.getDate() < curDateObj.getDate()) ||
201
+ dateObj.getMonth() < curDateObj.getMonth()
202
+ ) {
203
+ var value = (yesterday && yesterday(diffObj1)) || others(diffObj1);
204
+ text = formatDate(toDateObj, value);
205
+ } else if (!isOverdue && diff.y === 0 && diffDayType === 1) {
206
+ var value = (tomorrow && tomorrow(diffObj1)) || others(diffObj1);
207
+ text = formatDate(toDateObj, value);
208
+ }
209
+ } else {
210
+ var value = others(diffObj1);
211
+ text = formatDate(toDateObj, value);
212
+ }
213
+ }
214
+ return text ? (
215
+ <span className={className} data-title={title} data-id={dataId} data-test-id={dataId}>
216
+ {text}
217
+ </span>
218
+ ) : (
219
+ <FormatText
220
+ i18NKey={isSuffixEnable && suffix !== '' ? `${key}.${suffix}` : key}
221
+ values={values}
222
+ className={className}
223
+ data-title={title}
224
+ />
225
+ );
226
+ }
227
+ }
228
+ DateTimeDiffFormat.propTypes = {
229
+ ago: PropTypes.string,
230
+ className: PropTypes.string,
231
+ dataId: PropTypes.string,
232
+ format: PropTypes.func,
233
+ from: PropTypes.string,
234
+ fromTzData: PropTypes.object,
235
+ isDateField: PropTypes.bool,
236
+ later: PropTypes.string,
237
+ others: PropTypes.func,
238
+ title: PropTypes.string,
239
+ to: PropTypes.string,
240
+ toTzData: PropTypes.object,
241
+ today: PropTypes.oneOfType([
242
+ PropTypes.string,
243
+ PropTypes.object,
244
+ PropTypes.func,
245
+ ]),
246
+ tomorrow: PropTypes.oneOfType([
247
+ PropTypes.string,
248
+ PropTypes.object,
249
+ PropTypes.func,
250
+ ]),
251
+ yesterday: PropTypes.oneOfType([
252
+ PropTypes.string,
253
+ PropTypes.object,
254
+ PropTypes.func,
255
+ ]),
256
+ };
@@ -1,14 +1,14 @@
1
- import React from 'react';
2
- import PropTypes from 'prop-types';
3
- import I18N from './I18N';
4
-
5
- export default class FormatText extends React.Component {
6
- render() {
7
- return <I18N {...this.props} />;
8
- }
9
- }
10
- FormatText.propTypes = {
11
- i18NKey: PropTypes.string.isRequired,
12
- isHtml: PropTypes.bool,
13
- values: PropTypes.oneOfType([PropTypes.string, PropTypes.array])
14
- };
1
+ import React from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import I18N from './I18N';
4
+
5
+ export default class FormatText extends React.Component {
6
+ render() {
7
+ return <I18N {...this.props} />;
8
+ }
9
+ }
10
+ FormatText.propTypes = {
11
+ i18NKey: PropTypes.string.isRequired,
12
+ isHtml: PropTypes.bool,
13
+ values: PropTypes.oneOfType([PropTypes.string, PropTypes.array])
14
+ };
@@ -1,37 +1,37 @@
1
- import React, { Children } from 'react';
2
- import PropTypes from 'prop-types';
3
- import { replaceI18NValuesWithRegex, unescapeUnicode } from '../utils';
4
- import { I18NContext } from '../I18NContext';
5
- export default (i18NKeys = []) => Component => {
6
- class HOCI18N extends React.Component {
7
- constructor(props) {
8
- super(props);
9
- this.getI18NValue = this.getI18NValue.bind(this);
10
- }
11
-
12
- getI18NValue(key) {
13
- const { i18n } = this.context || {};
14
- if (typeof i18n === 'undefined') {
15
- return key;
16
- }
17
- let i18nStr = i18n[key];
18
- if (i18nStr === undefined) {
19
- return key;
20
- }
21
- return unescapeUnicode(i18nStr);
22
- }
23
-
24
- render() {
25
- let i18nProps = i18NKeys.reduce((result, key) => {
26
- if (this.props[key]) {
27
- result[key] = this.getI18NValue(this.props[key]);
28
- }
29
- return result;
30
- }, {});
31
- return <Component {...this.props} {...i18nProps} />;
32
- }
33
- }
34
-
35
- HOCI18N.contextType = I18NContext;
36
- return HOCI18N;
37
- };
1
+ import React, { Children } from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import { replaceI18NValuesWithRegex, unescapeUnicode } from '../utils';
4
+ import { I18NContext } from '../I18NContext';
5
+ export default (i18NKeys = []) => Component => {
6
+ class HOCI18N extends React.Component {
7
+ constructor(props) {
8
+ super(props);
9
+ this.getI18NValue = this.getI18NValue.bind(this);
10
+ }
11
+
12
+ getI18NValue(key) {
13
+ const { i18n } = this.context || {};
14
+ if (typeof i18n === 'undefined') {
15
+ return key;
16
+ }
17
+ let i18nStr = i18n[key];
18
+ if (i18nStr === undefined) {
19
+ return key;
20
+ }
21
+ return unescapeUnicode(i18nStr);
22
+ }
23
+
24
+ render() {
25
+ let i18nProps = i18NKeys.reduce((result, key) => {
26
+ if (this.props[key]) {
27
+ result[key] = this.getI18NValue(this.props[key]);
28
+ }
29
+ return result;
30
+ }, {});
31
+ return <Component {...this.props} {...i18nProps} />;
32
+ }
33
+ }
34
+
35
+ HOCI18N.contextType = I18NContext;
36
+ return HOCI18N;
37
+ };