@zohodesk/i18n 1.0.0-beta.17 → 1.0.0-beta.18

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