@zohodesk/i18n 1.0.0-exp.4 → 1.0.0-exp.5

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 (39) hide show
  1. package/es/components/DateTimeDiffFormat.js +19 -5
  2. package/es/components/FormatText.js +2 -2
  3. package/es/components/HOCI18N.js +10 -2
  4. package/es/components/I18N.js +13 -2
  5. package/es/components/I18NProvider.js +9 -0
  6. package/es/components/PluralFormat.js +5 -3
  7. package/es/components/UserTimeDiffFormat.js +3 -0
  8. package/es/components/__tests__/DateTimeDiffFormat.spec.js +89 -46
  9. package/es/components/__tests__/FormatText.spec.js +2 -2
  10. package/es/components/__tests__/HOCI18N.spec.js +4 -2
  11. package/es/components/__tests__/I18N.spec.js +4 -6
  12. package/es/components/__tests__/I18NProvider.spec.js +4 -4
  13. package/es/components/__tests__/PluralFormat.spec.js +2 -2
  14. package/es/components/__tests__/UserTimeDiffFormat.spec.js +117 -90
  15. package/es/utils/__tests__/jsxTranslations.spec.js +3 -0
  16. package/es/utils/errorReporter.js +3 -0
  17. package/es/utils/index.js +62 -17
  18. package/es/utils/jsxTranslations.js +17 -2
  19. package/lib/I18NContext.js +5 -1
  20. package/lib/components/DateTimeDiffFormat.js +79 -41
  21. package/lib/components/FormatText.js +34 -14
  22. package/lib/components/HOCI18N.js +50 -17
  23. package/lib/components/I18N.js +55 -20
  24. package/lib/components/I18NProvider.js +54 -22
  25. package/lib/components/PluralFormat.js +43 -20
  26. package/lib/components/UserTimeDiffFormat.js +65 -38
  27. package/lib/components/__tests__/DateTimeDiffFormat.spec.js +189 -119
  28. package/lib/components/__tests__/FormatText.spec.js +10 -3
  29. package/lib/components/__tests__/HOCI18N.spec.js +14 -3
  30. package/lib/components/__tests__/I18N.spec.js +12 -4
  31. package/lib/components/__tests__/I18NProvider.spec.js +23 -7
  32. package/lib/components/__tests__/PluralFormat.spec.js +11 -3
  33. package/lib/components/__tests__/UserTimeDiffFormat.spec.js +271 -202
  34. package/lib/index.js +20 -3
  35. package/lib/utils/__tests__/jsxTranslations.spec.js +12 -1
  36. package/lib/utils/errorReporter.js +5 -0
  37. package/lib/utils/index.js +117 -40
  38. package/lib/utils/jsxTranslations.js +99 -56
  39. package/package.json +1 -1
@@ -8,8 +8,10 @@ export default class DateTimeDiffFormat extends React.Component {
8
8
  super(props);
9
9
  this.getSuffix = this.getSuffix.bind(this);
10
10
  }
11
+
11
12
  getSuffix(min) {
12
13
  let suffix;
14
+
13
15
  if (this.props.ago && min < 0) {
14
16
  suffix = this.props.ago || '';
15
17
  } else if (this.props.later || min > 0) {
@@ -17,8 +19,10 @@ export default class DateTimeDiffFormat extends React.Component {
17
19
  } else {
18
20
  suffix = '';
19
21
  }
22
+
20
23
  return suffix;
21
24
  }
25
+
22
26
  render() {
23
27
  const {
24
28
  type,
@@ -91,10 +95,9 @@ export default class DateTimeDiffFormat extends React.Component {
91
95
  timeFormat: timeFormat,
92
96
  datePattern: datePattern,
93
97
  dateTimePattern: `${datePattern} ${timeFormat}`
94
- };
95
-
96
- //In if condition we'll remove year and set date format if the current year is not required
98
+ }; //In if condition we'll remove year and set date format if the current year is not required
97
99
  //In else part we'll set the date format as it is
100
+
98
101
  if (isEnabledCurrentYear === true && diffObj1.years === 0 && diffObj1.tYear === diffObj1.crntYear) {
99
102
  let dateFormat = getDatePatternWithoutYear(datePattern);
100
103
  diffObj1.dateFormat = dateFormat;
@@ -103,10 +106,12 @@ export default class DateTimeDiffFormat extends React.Component {
103
106
  diffObj1.dateFormat = datePattern;
104
107
  diffObj1.dateTimeFormat = `${datePattern} ${timeFormat}`;
105
108
  }
109
+
106
110
  let key = '';
107
111
  let values = [];
108
112
  let text = null;
109
113
  let isSuffixEnable = false;
114
+
110
115
  if (format) {
111
116
  let years, months, days, hours, minutes, seconds;
112
117
  years = diffObj1.years > 1 ? '2' : diffObj1.years;
@@ -123,12 +128,15 @@ export default class DateTimeDiffFormat extends React.Component {
123
128
  } else {
124
129
  res = res + next;
125
130
  }
131
+
126
132
  return res;
127
133
  }, '');
128
134
  let value = format(diffObj1, pattern);
135
+
129
136
  if (value && typeof value === 'object') {
130
137
  key = value.key;
131
138
  values = getValues(value.params, diffObj);
139
+
132
140
  if (pattern.indexOf('00000') === 0) {
133
141
  //suffix ignore for second hook
134
142
  isSuffixEnable = false;
@@ -141,28 +149,32 @@ export default class DateTimeDiffFormat extends React.Component {
141
149
  } else {
142
150
  let dateObj = new Date(toDateObj);
143
151
  let curDateObj = new Date(fromDateObj);
144
- let diffDayType = diffObj1.yDays;
152
+ let diffDayType = diffObj1.yDays; //In this condition, to calculate different days we have copied it from live --> diffDayType
145
153
 
146
- //In this condition, to calculate different days we have copied it from live --> diffDayType
147
154
  if (isOverdue && dateObj.getDate() < curDateObj.getDate() && diffObj1.yDays == 0) {
148
155
  diffDayType = -1;
149
156
  }
157
+
150
158
  if (!isOverdue) {
151
159
  let diffHr = dateObj.getHours() - curDateObj.getHours();
160
+
152
161
  if (diffHr < 0) {
153
162
  diffDayType += 1;
154
163
  } else if (diffHr == 0) {
155
164
  let diffMins = dateObj.getMinutes() - curDateObj.getMinutes();
165
+
156
166
  if (diffMins < 0) {
157
167
  diffDayType += 1;
158
168
  } else if (diffMins == 0) {
159
169
  let diffSec = dateObj.getSeconds() - curDateObj.getSeconds();
170
+
160
171
  if (diffSec < 0) {
161
172
  diffDayType += 1;
162
173
  }
163
174
  }
164
175
  }
165
176
  }
177
+
166
178
  if (diff.y === 0 && (diffDayType === 0 || diffDayType === 1)) {
167
179
  if (dateObj.getDate() === curDateObj.getDate()) {
168
180
  var value = today && today(diffObj1) || others(diffObj1);
@@ -179,6 +191,7 @@ export default class DateTimeDiffFormat extends React.Component {
179
191
  text = formatDate(toDateObj, value);
180
192
  }
181
193
  }
194
+
182
195
  return text ? /*#__PURE__*/React.createElement("span", {
183
196
  className: className,
184
197
  "data-title": title,
@@ -191,6 +204,7 @@ export default class DateTimeDiffFormat extends React.Component {
191
204
  "data-title": title
192
205
  });
193
206
  }
207
+
194
208
  }
195
209
  DateTimeDiffFormat.propTypes = {
196
210
  ago: PropTypes.string,
@@ -3,10 +3,10 @@ import PropTypes from 'prop-types';
3
3
  import I18N from "./I18N";
4
4
  export default class FormatText extends React.Component {
5
5
  render() {
6
- return /*#__PURE__*/React.createElement(I18N, {
7
- ...this.props
6
+ return /*#__PURE__*/React.createElement(I18N, { ...this.props
8
7
  });
9
8
  }
9
+
10
10
  }
11
11
  FormatText.propTypes = {
12
12
  i18NKey: PropTypes.string.isRequired,
@@ -10,32 +10,40 @@ export default (function () {
10
10
  super(props);
11
11
  this.getI18NValue = this.getI18NValue.bind(this);
12
12
  }
13
+
13
14
  getI18NValue(key) {
14
15
  const {
15
16
  i18n
16
17
  } = this.context || {};
18
+
17
19
  if (typeof i18n === 'undefined') {
18
20
  return key;
19
21
  }
22
+
20
23
  let i18nStr = i18n[key];
24
+
21
25
  if (i18nStr === undefined) {
22
26
  return key;
23
27
  }
28
+
24
29
  return unescapeUnicode(i18nStr);
25
30
  }
31
+
26
32
  render() {
27
33
  let i18nProps = i18NKeys.reduce((result, key) => {
28
34
  if (this.props[key]) {
29
35
  result[key] = this.getI18NValue(this.props[key]);
30
36
  }
37
+
31
38
  return result;
32
39
  }, {});
33
- return /*#__PURE__*/React.createElement(Component, {
34
- ...this.props,
40
+ return /*#__PURE__*/React.createElement(Component, { ...this.props,
35
41
  ...i18nProps
36
42
  });
37
43
  }
44
+
38
45
  }
46
+
39
47
  HOCI18N.contextType = I18NContext;
40
48
  return HOCI18N;
41
49
  };
@@ -9,6 +9,7 @@ export default class I18N extends React.Component {
9
9
  this.getI18NValue = this.getI18NValue.bind(this);
10
10
  this.createElement = this.createElement.bind(this);
11
11
  }
12
+
12
13
  getI18NValue() {
13
14
  const {
14
15
  i18NKey: key,
@@ -17,28 +18,35 @@ export default class I18N extends React.Component {
17
18
  const {
18
19
  i18n
19
20
  } = this.context || {};
21
+
20
22
  if (typeof i18n === 'undefined') {
21
23
  return key;
22
24
  }
25
+
23
26
  let i18nStr = i18n[key];
27
+
24
28
  if (i18nStr === undefined) {
25
29
  return key;
26
30
  }
31
+
27
32
  i18nStr = replaceI18NValuesWithRegex(i18nStr, values);
28
33
  return unescapeUnicode(i18nStr);
29
34
  }
35
+
30
36
  createElement() {
31
37
  const props = Object.keys(this.props).reduce((result, nextKey) => {
32
38
  if (nextKey != 'i18NKey' && nextKey != 'tag' && nextKey != 'values' && nextKey != 'isHtml' && nextKey != 'dataId') {
33
39
  result[nextKey] = this.props[nextKey];
34
40
  }
41
+
35
42
  return result;
36
- }, {});
37
- //const child=this.getI18NValue();
43
+ }, {}); //const child=this.getI18NValue();
44
+
38
45
  if (this.props.dataId) {
39
46
  props['data-id'] = this.props.dataId;
40
47
  props['data-test-id'] = this.props.dataId;
41
48
  }
49
+
42
50
  if (this.props.isHtml) {
43
51
  let dangerouslySetInnerHTML = {
44
52
  __html: HTMLPurifier.sanitize(this.getI18NValue())
@@ -47,11 +55,14 @@ export default class I18N extends React.Component {
47
55
  dangerouslySetInnerHTML
48
56
  }));
49
57
  }
58
+
50
59
  return /*#__PURE__*/React.createElement(this.props.tag, props, this.getI18NValue());
51
60
  }
61
+
52
62
  render() {
53
63
  return this.createElement();
54
64
  }
65
+
55
66
  }
56
67
  I18N.propTypes = {
57
68
  i18NKey: PropTypes.string.isRequired,
@@ -4,7 +4,9 @@ import { getI18NValue, userDateFormat } from "../utils";
4
4
  import { getI18NComponent } from "../utils/jsxTranslations";
5
5
  import { I18NContext } from "../I18NContext";
6
6
  const emptyObj = {};
7
+
7
8
  const dummy = (key, values) => key;
9
+
8
10
  export const i18NProviderUtils = {
9
11
  getI18NValue: dummy,
10
12
  userDateFormat: dummy,
@@ -15,10 +17,12 @@ export default class I18NProvider extends React.Component {
15
17
  super(props, context);
16
18
  i18NProviderUtils.getI18NValue = getI18NValue(props.i18n);
17
19
  i18NProviderUtils.getI18NComponent = getI18NComponent(props.i18n);
20
+
18
21
  if (props.tzData) {
19
22
  i18NProviderUtils.userDateFormat = userDateFormat(i18NProviderUtils.getI18NValue, props.tzData, props.timeFormat, props.datePattern, props.isEnabledCurrentYear);
20
23
  }
21
24
  }
25
+
22
26
  componentDidUpdate(prevProps) {
23
27
  let {
24
28
  i18n,
@@ -30,6 +34,7 @@ export default class I18NProvider extends React.Component {
30
34
  tzData,
31
35
  isEnabledCurrentYear
32
36
  } = this.props;
37
+
33
38
  if (i18n !== prevProps.i18n || timeZone !== prevProps.timeZone || datePattern !== prevProps.datePattern || timeFormat !== prevProps.timeFormat || direction !== prevProps.direction || tzData !== prevProps.tzData || isEnabledCurrentYear !== prevProps.isEnabledCurrentYear) {
34
39
  this.promise = new Promise((res, rej) => {
35
40
  this.resolve = res;
@@ -37,9 +42,11 @@ export default class I18NProvider extends React.Component {
37
42
  }).then(() => {
38
43
  i18NProviderUtils.getI18NValue = getI18NValue(this.props.i18n);
39
44
  i18NProviderUtils.getI18NComponent = getI18NComponent(this.props.i18n);
45
+
40
46
  if (this.props.tzData) {
41
47
  i18NProviderUtils.userDateFormat = userDateFormat(i18NProviderUtils.getI18NValue, this.props.tzData, this.props.timeFormat, this.props.datePattern, this.props.isEnabledCurrentYear);
42
48
  }
49
+
43
50
  this.promise = null;
44
51
  }, () => {
45
52
  this.promise = null;
@@ -47,6 +54,7 @@ export default class I18NProvider extends React.Component {
47
54
  onChange && onChange(this.resolve, this.reject);
48
55
  }
49
56
  }
57
+
50
58
  render() {
51
59
  return /*#__PURE__*/React.createElement(I18NContext.Provider, {
52
60
  value: {
@@ -60,6 +68,7 @@ export default class I18NProvider extends React.Component {
60
68
  }
61
69
  }, this.props.children);
62
70
  }
71
+
63
72
  }
64
73
  I18NProvider.defaultProps = {
65
74
  i18n: emptyObj,
@@ -10,7 +10,8 @@ export default class PluralFormat extends React.Component {
10
10
  value
11
11
  } = this.props;
12
12
  let key = '',
13
- values = '';
13
+ values = '';
14
+
14
15
  if (value > 1) {
15
16
  key = many;
16
17
  } else if (value == 1) {
@@ -18,9 +19,9 @@ export default class PluralFormat extends React.Component {
18
19
  } else if (value == 0) {
19
20
  key = zero;
20
21
  }
22
+
21
23
  values = `${value}`;
22
- return /*#__PURE__*/React.createElement(FormatText, {
23
- ...this.props,
24
+ return /*#__PURE__*/React.createElement(FormatText, { ...this.props,
24
25
  i18NKey: key,
25
26
  values: values,
26
27
  one: null,
@@ -28,6 +29,7 @@ export default class PluralFormat extends React.Component {
28
29
  zero: null
29
30
  });
30
31
  }
32
+
31
33
  }
32
34
  PluralFormat.propTypes = {
33
35
  many: PropTypes.string,
@@ -31,6 +31,7 @@ export default class UserTimeDiffFormat extends Component {
31
31
  datePattern,
32
32
  isEnabledCurrentYear
33
33
  } = this.context || {};
34
+
34
35
  if (!format && !others) {
35
36
  format = _ref => {
36
37
  let {
@@ -41,6 +42,7 @@ export default class UserTimeDiffFormat extends Component {
41
42
  displayType === 'dateTime' ? dateTimeFormat : displayType === 'date' ? dateFormat : timeFormat;
42
43
  };
43
44
  }
45
+
44
46
  return /*#__PURE__*/React.createElement(DateTimeDiffFormat, {
45
47
  type: type,
46
48
  page: page,
@@ -66,6 +68,7 @@ export default class UserTimeDiffFormat extends Component {
66
68
  isDateField: isDateField
67
69
  });
68
70
  }
71
+
69
72
  }
70
73
  UserTimeDiffFormat.propTypes = {
71
74
  ago: PropTypes.string,