-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript-i18n-library.js
310 lines (280 loc) · 10.7 KB
/
javascript-i18n-library.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
(function () {
var i18nServiceFactory = function (moment, momentTimezone, numbro, libPhoneNumber, config) {
var _config = {
referenceTimezone: 'Europe/Paris',
timezone: 'Europe/Paris',
locale: 'fr-FR',
currency: 'EUR',
dateFormat: 'DMY',
isMeridianTime: false
};
var _formats = {
date: {
MDY: {
short: 'MM/DD/YYYY',
medium: 'MM/DD/YYYY',
long: 'LL'
},
DMY: {
short: 'DD/MM/YYYY',
medium: 'DD/MM/YYYY',
long: 'LL'
},
YMD: {
short: 'YYYY-MM-DD',
medium: 'YYYY-MM-DD',
long: 'LL'
}
},
time: {
meridian: {
short: 'h:mm a',
medium: 'h:mm:ss a',
long: 'h:mm:ss a'
},
h24: {
short: 'HH:mm',
medium: 'HH:mm:ss',
long: 'HH:mm:ss'
}
}
};
var _mergeConfiguration = function (baseConfig, overrideConfig) {
var stampConfig = JSON.parse(JSON.stringify(baseConfig));
for (var key in overrideConfig) {
stampConfig[key] = overrideConfig[key];
}
return stampConfig;
};
var _parseTimestamp = function (timestamp) {
var parsedTimestamp = parseInt(timestamp, 10);
if (isNaN(parsedTimestamp)) {
throw 'i18n : bad timestamp format for input "' + timestamp + '", expect timestamp format.';
}
else if (parsedTimestamp.toString().length < 13) {
// 13 digits timestamp (9999999999999) == Sat Nov 20 2286,
throw 'i18n : bad timestamp format for input "' + timestamp + '", expect a millisecond timestamp.';
}
var parsedDateTime = momentTimezone.tz(
parsedTimestamp,
_config.referenceTimezone);
var timeZonedDateTime;
if (_config.offset !== undefined) {
timeZonedDateTime = parsedDateTime.utc().add(_config.offset, 'minutes');
} else {
timeZonedDateTime = momentTimezone.tz(parsedDateTime, _config.timezone);
}
return moment(timeZonedDateTime).locale(_config.locale);
};
var _parseDateTime = function (dateTimeString) {
var parsedDateTime = momentTimezone.tz(
dateTimeString,
moment.ISO_8601(),
_config.referenceTimezone);
if (!parsedDateTime.isValid()) {
throw 'i18n : bad date format for input "' + dateTimeString + '", expect ISO_8601 format.';
}
var timeZonedDateTime;
if (_config.offset !== undefined) {
timeZonedDateTime = parsedDateTime.utc().add(_config.offset, 'minutes');
} else {
timeZonedDateTime = momentTimezone.tz(parsedDateTime, _config.timezone);
}
return moment(timeZonedDateTime).locale(_config.locale);
};
var _getTimeAgo = function (dateTime) {
var currentDateTime = moment();
var timeAgoPayload = {
type: '',
offset: null
};
if ((timeAgoPayload.offset = currentDateTime.diff(dateTime, 'days')) > 0) {
timeAgoPayload.type = 'days';
}
else if ((timeAgoPayload.offset = currentDateTime.diff(dateTime, 'hours')) > 0) {
timeAgoPayload.type = 'hours';
}
else if ((timeAgoPayload.offset = currentDateTime.diff(dateTime, 'minutes')) > 0) {
timeAgoPayload.type = 'minutes';
}
else {
timeAgoPayload.offset = currentDateTime.diff(dateTime, 'seconds');
timeAgoPayload.type = 'seconds';
}
if (timeAgoPayload.offset < 0) {
throw 'i18n : future date not possible ;)';
}
return timeAgoPayload;
};
var _currencyISOToCurrencySymbol = function (currency, locale) {
var currencies = {
AUD: { default: "AU$", "en-AU": "$" },
BGN: { default: "лв" },
BRL: { default: "BR$" },
CAD: { default: "CA$", "en-CA": "$" },
CHF: { default: "CHF" },
CNY: { default: "CN¥", "zh-CN": "Ұ", "zh-HK": "元", "zh-TW": "¥" },
CZK: { default: "CZK" },
DKK: { default: "DKK", "da-DA": "kr" },
EUR: { default: "€" },
GBP: { default: "£UK", "en-GB": "£" },
HKD: { default: "HK$", "zh-HK": "$" },
HRK: { default: "HRK" },
HUF: { default: "HUF" },
IDR: { default: "IDR" },
ILS: { default: "₪" },
INR: { default: "₹" },
JPY: { default: "JP¥", "ja-JP": "¥" },
KRW: { default: "₩" },
MXN: { default: "MX$" },
MYR: { default: "MYR" },
NOK: { default: "NOK", "sv-SE": "kr" },
NZD: { default: "NZ$", "en-NZ": "$" },
PHP: { default: "PHP" },
PLN: { default: "PLN" },
RON: { default: "ROL", "ro-RO": "lei" },
RUB: { default: "RUB" },
SEK: { default: "SEK", "sv-SE": "kr" },
SGD: { default: "S$" },
THB: { default: "฿" },
TRY: { default: "TRY" },
USD: { default: "US$", "en-US": "$" },
ZAR: { default: "ZAR" }
};
if (currencies[currency]) {
return currencies[currency][locale] || currencies[currency]['default'];
}
return '¤';
};
if (!!config) {
_config = _mergeConfiguration(_config, config);
}
_config.timeFormat = _config.isMeridianTime ? 'meridian' : 'h24';
_config.currencySymbol = _currencyISOToCurrencySymbol(_config.currency, _config.locale);
return {
getTimeAgoFromTimestamp: function (timestamp) {
return _getTimeAgo(_parseTimestamp(timestamp));
},
getTimeAgoFromDateTime: function (dateTimeString) {
return _getTimeAgo(_parseDateTime(dateTimeString));
},
formatTimeAgoFromDateTime: function (dateTimeString) {
return _parseDateTime(dateTimeString).fromNow();
},
formatTimeAgoFromTimestamp: function (timestamp) {
return _parseTimestamp(timestamp).fromNow();
},
formatDateTime: function (dateTimeString, formatType) {
var timeZonedDateTime = _parseDateTime(dateTimeString);
var dateFormat = _formats.date[_config.dateFormat].short;
var timeFormat = _formats.time[_config.timeFormat].short;
if (formatType === this.formats.LONG) {
dateFormat = _formats.date[_config.dateFormat].long;
timeFormat = _formats.time[_config.timeFormat].long;
} else if (formatType === this.formats.MEDIUM) {
dateFormat = _formats.date[_config.dateFormat].medium;
timeFormat = _formats.time[_config.timeFormat].medium;
}
return timeZonedDateTime.format(dateFormat + ' ' + timeFormat);
},
formatDate: function (dateTimeString, formatType) {
var timeZonedDateTime = _parseDateTime(dateTimeString);
var dateFormat = _formats.date[_config.dateFormat].short;
if (formatType === this.formats.LONG) {
dateFormat = _formats.date[_config.dateFormat].long;
} else if (formatType === this.formats.MEDIUM) {
dateFormat = _formats.date[_config.dateFormat].medium;
}
return timeZonedDateTime.format(dateFormat);
},
formatTime: function (dateTimeString, formatType) {
var timeZonedDateTime = _parseDateTime(dateTimeString);
var timeFormat = _formats.time[_config.timeFormat].short;
if (formatType === this.formats.LONG) {
timeFormat = _formats.time[_config.timeFormat].long;
} else if (formatType === this.formats.MEDIUM) {
timeFormat = _formats.time[_config.timeFormat].medium;
}
return timeZonedDateTime.format(timeFormat);
},
formatNumber: function (value, decimalCount) {
var oldLanguage = numbro.language();
numbro.language(_config.locale);
if (decimalCount === undefined) {
var splittedValue = value.toString().split('.');
if (!!splittedValue[1]) {
decimalCount = splittedValue[1].length;
} else {
decimalCount = 0;
}
}
var decimalPattern = Array(decimalCount + 1).join('0');
var dotSymbol = decimalCount === 0 ? '[.]' : '.';
var valueFormated = numbro(value).format('0,0' + dotSymbol + decimalPattern);
numbro.language(oldLanguage);
return valueFormated;
},
formatCurrency: function (value, decimalCount, forcedCurrency) {
var oldLanguage = numbro.language();
numbro.language(_config.locale);
if (typeof decimalCount === 'string') {
forcedCurrency = decimalCount;
decimalCount = undefined;
}
if (decimalCount === undefined) {
var splittedValue = value.toString().split('.');
if (!!splittedValue[1]) {
decimalCount = splittedValue[1].length;
} else {
decimalCount = 0;
}
}
var decimalPattern = Array(decimalCount + 1).join('0');
var dotSymbol = decimalCount === 0 ? '[.]' : '.';
var valueFormated = numbro(value).formatCurrency('0,0' + dotSymbol + decimalPattern);
var currentCurrencySymbol = numbro.languages()[_config.locale].currency.symbol;
var currencySymbol = forcedCurrency ? _currencyISOToCurrencySymbol(forcedCurrency, _config.locale) : _config.currencySymbol;
valueFormated = valueFormated.replace(currentCurrencySymbol, currencySymbol);
numbro.language(oldLanguage);
return valueFormated;
},
unformat: function (formattedValue) {
var oldLanguage = numbro.language();
numbro.language(_config.locale);
var rawValue = numbro().unformat(formattedValue);
numbro.language(oldLanguage);
return rawValue;
},
formats: {
SHORT: "SHORT",
MEDIUM: "MEDIUM",
LONG: "LONG"
},
moment: moment,
momentTimezone: momentTimezone,
numbro: numbro,
libPhoneNumber: libPhoneNumber
};
};
// Node
if (typeof module !== 'undefined' && module.exports) {
var numbro = require('numbro');
var numbroLanguageLoader = require('./numbro-language-loader');
numbroLanguageLoader(numbro);
var momentTimezone = require('moment-timezone');
var moment = require('moment/min/moment-with-locales');
var libPhoneNumber = require('google-libphonenumber');
module.exports = function (config) {
// Pass moment with locales and moment-timezone because moment-timezone is not able to load all locales.
return i18nServiceFactory(moment, momentTimezone, numbro, libPhoneNumber, config);
};
}
// Browser
if (typeof window !== 'undefined') {
window.iadvize = window.iadvize || {};
window.iadvize.i18nServiceFactory = function (config) {
// Pass global moment twice because moment and moment-timezone are merged on desktop.
return i18nServiceFactory(window.moment, window.moment, window.numbro, window.libPhoneNumber, config);
};
}
})();