@zohodesk/i18n 1.0.0-beta.2 → 1.0.0-beta.20
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 +80 -5
- package/es/I18NContext.js +1 -2
- package/es/components/DateTimeDiffFormat.js +191 -184
- package/es/components/FormatText.js +4 -25
- package/es/components/HOCI18N.js +33 -45
- package/es/components/I18N.js +46 -63
- package/es/components/I18NProvider.js +54 -84
- package/es/components/PluralFormat.js +29 -48
- package/es/components/UserTimeDiffFormat.js +65 -70
- package/es/components/__tests__/DateTimeDiffFormat.spec.js +868 -657
- package/es/components/__tests__/FormatText.spec.js +20 -17
- package/es/components/__tests__/HOCI18N.spec.js +18 -22
- package/es/components/__tests__/I18N.spec.js +20 -19
- package/es/components/__tests__/I18NProvider.spec.js +36 -45
- package/es/components/__tests__/PluralFormat.spec.js +20 -17
- package/es/components/__tests__/UserTimeDiffFormat.spec.js +1343 -1095
- 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/index.js +2 -6
- package/es/utils.js +250 -385
- package/lib/I18NContext.js +6 -6
- package/lib/components/DateTimeDiffFormat.js +166 -123
- package/lib/components/FormatText.js +32 -22
- package/lib/components/HOCI18N.js +47 -23
- package/lib/components/I18N.js +60 -36
- package/lib/components/I18NProvider.js +76 -69
- package/lib/components/PluralFormat.js +42 -32
- package/lib/components/UserTimeDiffFormat.js +81 -54
- package/lib/components/__tests__/DateTimeDiffFormat.spec.js +815 -629
- package/lib/components/__tests__/FormatText.spec.js +23 -25
- package/lib/components/__tests__/HOCI18N.spec.js +26 -34
- package/lib/components/__tests__/I18N.spec.js +21 -26
- package/lib/components/__tests__/I18NProvider.spec.js +43 -51
- package/lib/components/__tests__/PluralFormat.spec.js +24 -28
- package/lib/components/__tests__/UserTimeDiffFormat.spec.js +1256 -1039
- 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/index.js +73 -119
- package/lib/utils.js +222 -329
- package/package.json +29 -29
- package/src/I18NContext.js +3 -0
- package/src/components/DateTimeDiffFormat.js +256 -0
- package/src/components/FormatText.js +14 -0
- package/src/components/HOCI18N.js +37 -0
- package/src/components/I18N.js +72 -0
- package/src/components/I18NProvider.js +110 -0
- package/src/components/PluralFormat.js +37 -0
- package/src/components/UserTimeDiffFormat.js +97 -0
- package/src/components/__tests__/DateTimeDiffFormat.spec.js +618 -0
- package/src/components/__tests__/FormatText.spec.js +27 -0
- package/src/components/__tests__/HOCI18N.spec.js +33 -0
- package/src/components/__tests__/I18N.spec.js +30 -0
- package/src/components/__tests__/I18NProvider.spec.js +65 -0
- package/src/components/__tests__/PluralFormat.spec.js +28 -0
- package/src/components/__tests__/UserTimeDiffFormat.spec.js +1076 -0
- package/src/components/__tests__/__snapshots__/DateTimeDiffFormat.spec.js.snap +258 -0
- package/src/components/__tests__/__snapshots__/FormatText.spec.js.snap +17 -0
- package/src/components/__tests__/__snapshots__/HOCI18N.spec.js.snap +15 -0
- package/src/components/__tests__/__snapshots__/I18N.spec.js.snap +17 -0
- package/src/components/__tests__/__snapshots__/I18NProvider.spec.js.snap +13 -0
- package/src/components/__tests__/__snapshots__/PluralFormat.spec.js.snap +17 -0
- package/src/components/__tests__/__snapshots__/UserTimeDiffFormat.spec.js.snap +366 -0
- package/src/index.js +33 -0
- package/src/utils.js +527 -0
- package/.DS_Store +0 -0
- package/es/components/NewDateFormat.js +0 -50
- package/es/offset.js +0 -629
- package/es/timezones.js +0 -112
- package/lib/components/NewDateFormat.js +0 -68
- package/lib/offset.js +0 -634
- package/lib/timezones.js +0 -120
package/package.json
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
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
|
-
"init": "npm run clean",
|
|
19
|
-
"lint": "react-cli lint"
|
|
20
|
-
},
|
|
21
|
-
"dependencies": {
|
|
22
|
-
"@zoho/SecurityJS": "5.0.
|
|
23
|
-
},
|
|
24
|
-
"react-cli": {
|
|
25
|
-
"docs": {
|
|
26
|
-
"componentFolder": "./src"
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@zohodesk/i18n",
|
|
3
|
+
"version": "1.0.0-beta.20",
|
|
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
|
+
"init": "npm run clean",
|
|
19
|
+
"lint": "react-cli lint"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@zoho/SecurityJS": "5.0.2"
|
|
23
|
+
},
|
|
24
|
+
"react-cli": {
|
|
25
|
+
"docs": {
|
|
26
|
+
"componentFolder": "./src"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +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}>
|
|
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
|
+
};
|
|
@@ -0,0 +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
|
+
};
|
|
@@ -0,0 +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
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import React, { Children } from 'react';
|
|
2
|
+
import PropTypes from 'prop-types';
|
|
3
|
+
import { replaceI18NValuesWithRegex, unescapeUnicode } from '../utils';
|
|
4
|
+
import { HTMLPurifier } from '@zoho/SecurityJS';
|
|
5
|
+
import { I18NContext } from '../I18NContext';
|
|
6
|
+
|
|
7
|
+
export default class I18N extends React.Component {
|
|
8
|
+
constructor(props) {
|
|
9
|
+
super(props);
|
|
10
|
+
this.getI18NValue = this.getI18NValue.bind(this);
|
|
11
|
+
this.createElement = this.createElement.bind(this);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
getI18NValue() {
|
|
15
|
+
const { i18NKey: key, values } = this.props;
|
|
16
|
+
const { i18n } = this.context || {};
|
|
17
|
+
if (typeof i18n === 'undefined') {
|
|
18
|
+
return key;
|
|
19
|
+
}
|
|
20
|
+
let i18nStr = i18n[key];
|
|
21
|
+
if (i18nStr === undefined) {
|
|
22
|
+
return key;
|
|
23
|
+
}
|
|
24
|
+
i18nStr = replaceI18NValuesWithRegex(i18nStr, values);
|
|
25
|
+
return unescapeUnicode(i18nStr);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
createElement() {
|
|
29
|
+
const props = Object.keys(this.props).reduce((result, nextKey) => {
|
|
30
|
+
if (
|
|
31
|
+
nextKey != 'i18NKey' &&
|
|
32
|
+
nextKey != 'tag' &&
|
|
33
|
+
nextKey != 'values' &&
|
|
34
|
+
nextKey != 'isHtml' &&
|
|
35
|
+
nextKey != 'dataId'
|
|
36
|
+
) {
|
|
37
|
+
result[nextKey] = this.props[nextKey];
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
}, {});
|
|
41
|
+
//const child=this.getI18NValue();
|
|
42
|
+
if (this.props.dataId) {
|
|
43
|
+
props['data-id'] = this.props.dataId;
|
|
44
|
+
}
|
|
45
|
+
if (this.props.isHtml) {
|
|
46
|
+
let dangerouslySetInnerHTML = {
|
|
47
|
+
__html: HTMLPurifier.sanitize(this.getI18NValue())
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
return React.createElement(
|
|
51
|
+
this.props.tag,
|
|
52
|
+
Object.assign(props, { dangerouslySetInnerHTML })
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
return React.createElement(this.props.tag, props, this.getI18NValue());
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
render() {
|
|
59
|
+
return this.createElement();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
I18N.propTypes = {
|
|
63
|
+
i18NKey: PropTypes.string.isRequired,
|
|
64
|
+
isHtml: PropTypes.bool,
|
|
65
|
+
tag: PropTypes.string,
|
|
66
|
+
values: PropTypes.oneOfType([PropTypes.string, PropTypes.array])
|
|
67
|
+
};
|
|
68
|
+
I18N.defaultProps = {
|
|
69
|
+
tag: 'span',
|
|
70
|
+
isHtml: false
|
|
71
|
+
};
|
|
72
|
+
I18N.contextType = I18NContext;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import PropTypes from 'prop-types';
|
|
3
|
+
import { getI18NValue, userDateFormat } from '../utils';
|
|
4
|
+
import { I18NContext } from '../I18NContext';
|
|
5
|
+
|
|
6
|
+
const emptyObj = {};
|
|
7
|
+
const dummy = (key, values) => key;
|
|
8
|
+
|
|
9
|
+
export const i18NProviderUtils = {
|
|
10
|
+
getI18NValue: dummy,
|
|
11
|
+
userDateFormat: dummy,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export default class I18NProvider extends React.Component {
|
|
15
|
+
constructor(props, context) {
|
|
16
|
+
super(props, context);
|
|
17
|
+
i18NProviderUtils.getI18NValue = getI18NValue(props.i18n);
|
|
18
|
+
if(props.tzData){
|
|
19
|
+
i18NProviderUtils.userDateFormat = userDateFormat(
|
|
20
|
+
i18NProviderUtils.getI18NValue,
|
|
21
|
+
props.tzData,
|
|
22
|
+
props.timeFormat,
|
|
23
|
+
props.datePattern,
|
|
24
|
+
props.isEnabledCurrentYear
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
componentDidUpdate(next) {
|
|
30
|
+
let {
|
|
31
|
+
i18n,
|
|
32
|
+
timeZone,
|
|
33
|
+
datePattern,
|
|
34
|
+
timeFormat,
|
|
35
|
+
direction,
|
|
36
|
+
onChange,
|
|
37
|
+
tzData,
|
|
38
|
+
} = this.props;
|
|
39
|
+
if (
|
|
40
|
+
i18n !== next.i18n ||
|
|
41
|
+
timeZone !== next.timeZone ||
|
|
42
|
+
datePattern !== next.datePattern ||
|
|
43
|
+
timeFormat !== next.timeFormat ||
|
|
44
|
+
direction !== next.direction ||
|
|
45
|
+
tzData !== next.tzData
|
|
46
|
+
) {
|
|
47
|
+
this.promise = new Promise((res, rej) => {
|
|
48
|
+
this.resolve = res;
|
|
49
|
+
this.reject = rej;
|
|
50
|
+
}).then(
|
|
51
|
+
() => {
|
|
52
|
+
i18NProviderUtils.getI18NValue = getI18NValue(nextProps.i18n);
|
|
53
|
+
if(props.tzData){
|
|
54
|
+
i18NProviderUtils.userDateFormat = userDateFormat(
|
|
55
|
+
i18NProviderUtils.getI18NValue,
|
|
56
|
+
props.tzData,
|
|
57
|
+
props.timeFormat,
|
|
58
|
+
props.datePattern,
|
|
59
|
+
this.props.isEnabledCurrentYear
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
this.promise = null;
|
|
63
|
+
},
|
|
64
|
+
() => {
|
|
65
|
+
this.promise = null;
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
onChange && onChange(this.resolve, this.reject);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
render() {
|
|
73
|
+
return (
|
|
74
|
+
<I18NContext.Provider
|
|
75
|
+
value={{
|
|
76
|
+
i18n: this.props.i18n,
|
|
77
|
+
direction: this.props.direction,
|
|
78
|
+
timeZone: this.props.timeZone,
|
|
79
|
+
datePattern: this.props.datePattern,
|
|
80
|
+
timeFormat: this.props.timeFormat,
|
|
81
|
+
isEnabledCurrentYear: this.props.isEnabledCurrentYear,
|
|
82
|
+
tzData: this.props.tzData,
|
|
83
|
+
}}
|
|
84
|
+
>
|
|
85
|
+
{this.props.children}
|
|
86
|
+
</I18NContext.Provider>
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
I18NProvider.defaultProps = {
|
|
92
|
+
i18n: emptyObj,
|
|
93
|
+
timeZone: '',
|
|
94
|
+
datePattern: '',
|
|
95
|
+
timeFormat: '',
|
|
96
|
+
isEnabledCurrentYear: '',
|
|
97
|
+
direction: 'ltr',
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
I18NProvider.propTypes = {
|
|
101
|
+
children: PropTypes.element.isRequired,
|
|
102
|
+
datePattern: PropTypes.string,
|
|
103
|
+
isEnabledCurrentYear: PropTypes.string,
|
|
104
|
+
direction: PropTypes.string,
|
|
105
|
+
i18n: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
|
|
106
|
+
onChange: PropTypes.func,
|
|
107
|
+
timeFormat: PropTypes.string,
|
|
108
|
+
timeZone: PropTypes.string,
|
|
109
|
+
tzData: PropTypes.object,
|
|
110
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import PropTypes from 'prop-types';
|
|
3
|
+
import FormatText from './FormatText';
|
|
4
|
+
|
|
5
|
+
export default class PluralFormat extends React.Component {
|
|
6
|
+
render() {
|
|
7
|
+
const { one, many, zero, value } = this.props;
|
|
8
|
+
let key = '',
|
|
9
|
+
values = '';
|
|
10
|
+
if (value > 1) {
|
|
11
|
+
key = many;
|
|
12
|
+
} else if (value == 1) {
|
|
13
|
+
key = one;
|
|
14
|
+
} else if (value == 0) {
|
|
15
|
+
key = zero;
|
|
16
|
+
}
|
|
17
|
+
values = `${value}`;
|
|
18
|
+
|
|
19
|
+
return (
|
|
20
|
+
<FormatText
|
|
21
|
+
{...this.props}
|
|
22
|
+
i18NKey={key}
|
|
23
|
+
values={values}
|
|
24
|
+
one={null}
|
|
25
|
+
many={null}
|
|
26
|
+
zero={null}
|
|
27
|
+
/>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
PluralFormat.propTypes = {
|
|
32
|
+
many: PropTypes.string,
|
|
33
|
+
one: PropTypes.string,
|
|
34
|
+
tag: PropTypes.string,
|
|
35
|
+
value: PropTypes.number,
|
|
36
|
+
zero: PropTypes.string
|
|
37
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import React, { Component } from 'react';
|
|
2
|
+
import PropTypes from 'prop-types';
|
|
3
|
+
import DateTimeDiffFormat from './DateTimeDiffFormat';
|
|
4
|
+
import { I18NContext } from '../I18NContext';
|
|
5
|
+
|
|
6
|
+
export default class UserTimeDiffFormat extends Component {
|
|
7
|
+
render() {
|
|
8
|
+
let currentTime = new Date();
|
|
9
|
+
let currentTimeUTCString = currentTime.toISOString();
|
|
10
|
+
let {
|
|
11
|
+
type,
|
|
12
|
+
page,
|
|
13
|
+
isNeedTime,
|
|
14
|
+
to,
|
|
15
|
+
today,
|
|
16
|
+
yesterday,
|
|
17
|
+
tomorrow,
|
|
18
|
+
others,
|
|
19
|
+
ago,
|
|
20
|
+
later,
|
|
21
|
+
format,
|
|
22
|
+
title,
|
|
23
|
+
className,
|
|
24
|
+
displayType,
|
|
25
|
+
dataId,
|
|
26
|
+
isOverdue,
|
|
27
|
+
isDateField
|
|
28
|
+
} = this.props;
|
|
29
|
+
let { tzData, timeFormat, datePattern, isEnabledCurrentYear } =
|
|
30
|
+
this.context || {};
|
|
31
|
+
|
|
32
|
+
if (!format && !others) {
|
|
33
|
+
format = ({ dateTimeFormat, dateFormat, timeFormat }) => {
|
|
34
|
+
displayType === 'dateTime'
|
|
35
|
+
? dateTimeFormat
|
|
36
|
+
: displayType === 'date'
|
|
37
|
+
? dateFormat
|
|
38
|
+
: timeFormat;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return (
|
|
43
|
+
<DateTimeDiffFormat
|
|
44
|
+
type={type}
|
|
45
|
+
page={page}
|
|
46
|
+
isNeedTime={isNeedTime}
|
|
47
|
+
from={currentTimeUTCString}
|
|
48
|
+
fromTzData={tzData}
|
|
49
|
+
to={to}
|
|
50
|
+
toTzData={tzData}
|
|
51
|
+
today={today}
|
|
52
|
+
yesterday={yesterday}
|
|
53
|
+
tomorrow={tomorrow}
|
|
54
|
+
others={others}
|
|
55
|
+
ago={ago}
|
|
56
|
+
later={later}
|
|
57
|
+
format={format}
|
|
58
|
+
title={title}
|
|
59
|
+
className={className}
|
|
60
|
+
dataId={dataId}
|
|
61
|
+
isOverdue={isOverdue}
|
|
62
|
+
timeFormat={timeFormat}
|
|
63
|
+
datePattern={datePattern}
|
|
64
|
+
isEnabledCurrentYear={isEnabledCurrentYear}
|
|
65
|
+
isDateField={isDateField}
|
|
66
|
+
/>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
UserTimeDiffFormat.propTypes = {
|
|
71
|
+
ago: PropTypes.string,
|
|
72
|
+
className: PropTypes.string,
|
|
73
|
+
dataId: PropTypes.string,
|
|
74
|
+
displayType: PropTypes.oneOf(['date', 'time', 'dateTime']),
|
|
75
|
+
format: PropTypes.func,
|
|
76
|
+
isDateField: PropTypes.bool,
|
|
77
|
+
later: PropTypes.string,
|
|
78
|
+
others: PropTypes.func,
|
|
79
|
+
title: PropTypes.string,
|
|
80
|
+
to: PropTypes.string,
|
|
81
|
+
today: PropTypes.oneOfType([
|
|
82
|
+
PropTypes.string,
|
|
83
|
+
PropTypes.object,
|
|
84
|
+
PropTypes.func,
|
|
85
|
+
]),
|
|
86
|
+
tomorrow: PropTypes.oneOfType([
|
|
87
|
+
PropTypes.string,
|
|
88
|
+
PropTypes.object,
|
|
89
|
+
PropTypes.func,
|
|
90
|
+
]),
|
|
91
|
+
yesterday: PropTypes.oneOfType([
|
|
92
|
+
PropTypes.string,
|
|
93
|
+
PropTypes.object,
|
|
94
|
+
PropTypes.func,
|
|
95
|
+
]),
|
|
96
|
+
};
|
|
97
|
+
UserTimeDiffFormat.contextType = I18NContext;
|