globalize-rpk 1.7.0 → 1.7.1
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/globalize/currency.js +590 -0
- package/globalize/date.js +3139 -0
- package/globalize/globalize-runtime.js +326 -0
- package/globalize/globalize.js +507 -0
- package/globalize/message.js +2076 -0
- package/globalize/number.js +1727 -0
- package/globalize/plural.js +376 -0
- package/globalize/relative-time.js +203 -0
- package/globalize/unit.js +301 -0
- package/node-main.js +27 -0
- package/package.json +3 -26
- package/CONTRIBUTING.md +0 -5
- package/README.md +0 -818
- package/doc/api/core/constructor.md +0 -28
- package/doc/api/core/load.md +0 -96
- package/doc/api/core/locale.md +0 -43
- package/doc/api/currency/currency-formatter.md +0 -196
- package/doc/api/currency/currency-to-parts-formatter.md +0 -117
- package/doc/api/date/date-formatter.md +0 -203
- package/doc/api/date/date-parser.md +0 -60
- package/doc/api/date/date-to-parts-formatter.md +0 -176
- package/doc/api/date/load-iana-time-zone.md +0 -29
- package/doc/api/message/load-messages.md +0 -105
- package/doc/api/message/message-formatter.md +0 -208
- package/doc/api/number/number-formatter.md +0 -202
- package/doc/api/number/number-parser.md +0 -130
- package/doc/api/number/number-to-parts-formatter.md +0 -140
- package/doc/api/plural/plural-generator.md +0 -84
- package/doc/api/relative-time/relative-time-formatter.md +0 -60
- package/doc/api/unit/unit-formatter.md +0 -72
- package/doc/blog-post/2017-07-xx-1.3.0-announcement.md +0 -177
- package/doc/cldr.md +0 -114
- package/doc/error/e-default-locale-not-defined.md +0 -9
- package/doc/error/e-invalid-cldr.md +0 -14
- package/doc/error/e-invalid-par-type.md +0 -12
- package/doc/error/e-invalid-par-value.md +0 -11
- package/doc/error/e-missing-cldr.md +0 -11
- package/doc/error/e-missing-parameter.md +0 -10
- package/doc/error/e-missing-plural-module.md +0 -9
- package/doc/error/e-par-missing-key.md +0 -11
- package/doc/error/e-par-out-of-range.md +0 -13
- package/doc/error/e-unsupported.md +0 -10
- package/doc/migrating-from-0.x.md +0 -64
- package/examples/amd-bower/.bowerrc +0 -7
- package/examples/amd-bower/README.md +0 -65
- package/examples/amd-bower/bower.json +0 -13
- package/examples/amd-bower/index.html +0 -46
- package/examples/amd-bower/main.js +0 -141
- package/examples/amd-bower/messages/en.json +0 -12
- package/examples/amd-bower/package.json +0 -14
- package/examples/app-npm-webpack/README.md +0 -74
- package/examples/app-npm-webpack/app/index.js +0 -89
- package/examples/app-npm-webpack/index-template.html +0 -71
- package/examples/app-npm-webpack/messages/ar.json +0 -25
- package/examples/app-npm-webpack/messages/de.json +0 -21
- package/examples/app-npm-webpack/messages/en.json +0 -21
- package/examples/app-npm-webpack/messages/es.json +0 -21
- package/examples/app-npm-webpack/messages/pt.json +0 -21
- package/examples/app-npm-webpack/messages/ru.json +0 -23
- package/examples/app-npm-webpack/messages/zh.json +0 -20
- package/examples/app-npm-webpack/package.json +0 -17
- package/examples/app-npm-webpack/webpack-config.js +0 -63
- package/examples/globalize-compiler/README.md +0 -45
- package/examples/globalize-compiler/app.js +0 -58
- package/examples/globalize-compiler/development.html +0 -121
- package/examples/globalize-compiler/messages.json +0 -12
- package/examples/globalize-compiler/package.json +0 -15
- package/examples/globalize-compiler/production.html +0 -75
- package/examples/node-npm/README.md +0 -57
- package/examples/node-npm/main.js +0 -65
- package/examples/node-npm/messages/en.json +0 -12
- package/examples/node-npm/package.json +0 -10
- package/examples/plain-javascript/README.md +0 -81
- package/examples/plain-javascript/index.html +0 -445
@@ -0,0 +1,376 @@
|
|
1
|
+
/**
|
2
|
+
* Globalize v1.7.0
|
3
|
+
*
|
4
|
+
* https://github.com/globalizejs/globalize
|
5
|
+
*
|
6
|
+
* Copyright OpenJS Foundation and other contributors
|
7
|
+
* Released under the MIT license
|
8
|
+
* https://jquery.org/license
|
9
|
+
*
|
10
|
+
* Date: 2021-08-02T11:53Z
|
11
|
+
*/
|
12
|
+
/*!
|
13
|
+
* Globalize v1.7.0 2021-08-02T11:53Z Released under the MIT license
|
14
|
+
* http://git.io/TrdQbw
|
15
|
+
*/
|
16
|
+
(function( root, factory ) {
|
17
|
+
|
18
|
+
// UMD returnExports
|
19
|
+
if ( typeof define === "function" && define.amd ) {
|
20
|
+
|
21
|
+
// AMD
|
22
|
+
define([
|
23
|
+
"cldr",
|
24
|
+
"../globalize",
|
25
|
+
"cldr/event",
|
26
|
+
"cldr/supplemental"
|
27
|
+
], factory );
|
28
|
+
} else if ( typeof exports === "object" ) {
|
29
|
+
|
30
|
+
// Node, CommonJS
|
31
|
+
module.exports = factory( require( "cldrjs" ), require( "../globalize" ) );
|
32
|
+
} else {
|
33
|
+
|
34
|
+
// Global
|
35
|
+
factory( root.Cldr, root.Globalize );
|
36
|
+
}
|
37
|
+
}(this, function( Cldr, Globalize ) {
|
38
|
+
|
39
|
+
var runtimeBind = Globalize._runtimeBind,
|
40
|
+
validateCldr = Globalize._validateCldr,
|
41
|
+
validateDefaultLocale = Globalize._validateDefaultLocale,
|
42
|
+
validateParameterPresence = Globalize._validateParameterPresence,
|
43
|
+
validateParameterType = Globalize._validateParameterType,
|
44
|
+
validateParameterTypePlainObject = Globalize._validateParameterTypePlainObject;
|
45
|
+
var MakePlural;
|
46
|
+
/* eslint-disable */
|
47
|
+
MakePlural = (function() {
|
48
|
+
'use strict';
|
49
|
+
|
50
|
+
var _toArray = function (arr) { return Array.isArray(arr) ? arr : Array.from(arr); };
|
51
|
+
|
52
|
+
var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } };
|
53
|
+
|
54
|
+
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } };
|
55
|
+
|
56
|
+
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
|
57
|
+
|
58
|
+
|
59
|
+
/**
|
60
|
+
* make-plural.js -- https://github.com/eemeli/make-plural.js/
|
61
|
+
* Copyright (c) 2014-2015 by Eemeli Aro <eemeli@gmail.com>
|
62
|
+
*
|
63
|
+
* Permission to use, copy, modify, and/or distribute this software for any
|
64
|
+
* purpose with or without fee is hereby granted, provided that the above
|
65
|
+
* copyright notice and this permission notice appear in all copies.
|
66
|
+
*
|
67
|
+
* The software is provided "as is" and the author disclaims all warranties
|
68
|
+
* with regard to this software including all implied warranties of
|
69
|
+
* merchantability and fitness. In no event shall the author be liable for
|
70
|
+
* any special, direct, indirect, or consequential damages or any damages
|
71
|
+
* whatsoever resulting from loss of use, data or profits, whether in an
|
72
|
+
* action of contract, negligence or other tortious action, arising out of
|
73
|
+
* or in connection with the use or performance of this software.
|
74
|
+
*/
|
75
|
+
|
76
|
+
var Parser = (function () {
|
77
|
+
function Parser() {
|
78
|
+
_classCallCheck(this, Parser);
|
79
|
+
}
|
80
|
+
|
81
|
+
_createClass(Parser, [{
|
82
|
+
key: 'parse',
|
83
|
+
value: function parse(cond) {
|
84
|
+
var _this = this;
|
85
|
+
|
86
|
+
if (cond === 'i = 0 or n = 1') {
|
87
|
+
return 'n >= 0 && n <= 1';
|
88
|
+
}if (cond === 'i = 0,1') {
|
89
|
+
return 'n >= 0 && n < 2';
|
90
|
+
}if (cond === 'i = 1 and v = 0') {
|
91
|
+
this.v0 = 1;
|
92
|
+
return 'n == 1 && v0';
|
93
|
+
}
|
94
|
+
return cond.replace(/([tv]) (!?)= 0/g, function (m, sym, noteq) {
|
95
|
+
var sn = sym + '0';
|
96
|
+
_this[sn] = 1;
|
97
|
+
return noteq ? '!' + sn : sn;
|
98
|
+
}).replace(/\b[fintv]\b/g, function (m) {
|
99
|
+
_this[m] = 1;
|
100
|
+
return m;
|
101
|
+
}).replace(/([fin]) % (10+)/g, function (m, sym, num) {
|
102
|
+
var sn = sym + num;
|
103
|
+
_this[sn] = 1;
|
104
|
+
return sn;
|
105
|
+
}).replace(/n10+ = 0/g, 't0 && $&').replace(/(\w+ (!?)= )([0-9.]+,[0-9.,]+)/g, function (m, se, noteq, x) {
|
106
|
+
if (m === 'n = 0,1') return '(n == 0 || n == 1)';
|
107
|
+
if (noteq) return se + x.split(',').join(' && ' + se);
|
108
|
+
return '(' + se + x.split(',').join(' || ' + se) + ')';
|
109
|
+
}).replace(/(\w+) (!?)= ([0-9]+)\.\.([0-9]+)/g, function (m, sym, noteq, x0, x1) {
|
110
|
+
if (Number(x0) + 1 === Number(x1)) {
|
111
|
+
if (noteq) return '' + sym + ' != ' + x0 + ' && ' + sym + ' != ' + x1;
|
112
|
+
return '(' + sym + ' == ' + x0 + ' || ' + sym + ' == ' + x1 + ')';
|
113
|
+
}
|
114
|
+
if (noteq) return '(' + sym + ' < ' + x0 + ' || ' + sym + ' > ' + x1 + ')';
|
115
|
+
if (sym === 'n') {
|
116
|
+
_this.t0 = 1;return '(t0 && n >= ' + x0 + ' && n <= ' + x1 + ')';
|
117
|
+
}
|
118
|
+
return '(' + sym + ' >= ' + x0 + ' && ' + sym + ' <= ' + x1 + ')';
|
119
|
+
}).replace(/ and /g, ' && ').replace(/ or /g, ' || ').replace(/ = /g, ' == ');
|
120
|
+
}
|
121
|
+
}, {
|
122
|
+
key: 'vars',
|
123
|
+
value: (function (_vars) {
|
124
|
+
function vars() {
|
125
|
+
return _vars.apply(this, arguments);
|
126
|
+
}
|
127
|
+
|
128
|
+
vars.toString = function () {
|
129
|
+
return _vars.toString();
|
130
|
+
};
|
131
|
+
|
132
|
+
return vars;
|
133
|
+
})(function () {
|
134
|
+
var vars = [];
|
135
|
+
if (this.i) vars.push('i = s[0]');
|
136
|
+
if (this.f || this.v) vars.push('f = s[1] || \'\'');
|
137
|
+
if (this.t) vars.push('t = (s[1] || \'\').replace(/0+$/, \'\')');
|
138
|
+
if (this.v) vars.push('v = f.length');
|
139
|
+
if (this.v0) vars.push('v0 = !s[1]');
|
140
|
+
if (this.t0 || this.n10 || this.n100) vars.push('t0 = Number(s[0]) == n');
|
141
|
+
for (var k in this) {
|
142
|
+
if (/^.10+$/.test(k)) {
|
143
|
+
var k0 = k[0] === 'n' ? 't0 && s[0]' : k[0];
|
144
|
+
vars.push('' + k + ' = ' + k0 + '.slice(-' + k.substr(2).length + ')');
|
145
|
+
}
|
146
|
+
}if (!vars.length) return '';
|
147
|
+
return 'var ' + ['s = String(n).split(\'.\')'].concat(vars).join(', ');
|
148
|
+
})
|
149
|
+
}]);
|
150
|
+
|
151
|
+
return Parser;
|
152
|
+
})();
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
var MakePlural = (function () {
|
157
|
+
function MakePlural(lc) {
|
158
|
+
var _ref = arguments[1] === undefined ? MakePlural : arguments[1];
|
159
|
+
|
160
|
+
var cardinals = _ref.cardinals;
|
161
|
+
var ordinals = _ref.ordinals;
|
162
|
+
|
163
|
+
_classCallCheck(this, MakePlural);
|
164
|
+
|
165
|
+
if (!cardinals && !ordinals) throw new Error('At least one type of plural is required');
|
166
|
+
this.lc = lc;
|
167
|
+
this.categories = { cardinal: [], ordinal: [] };
|
168
|
+
this.parser = new Parser();
|
169
|
+
|
170
|
+
this.fn = this.buildFunction(cardinals, ordinals);
|
171
|
+
this.fn._obj = this;
|
172
|
+
this.fn.categories = this.categories;
|
173
|
+
|
174
|
+
this.fn.toString = this.fnToString.bind(this);
|
175
|
+
return this.fn;
|
176
|
+
}
|
177
|
+
|
178
|
+
_createClass(MakePlural, [{
|
179
|
+
key: 'compile',
|
180
|
+
value: function compile(type, req) {
|
181
|
+
var cases = [];
|
182
|
+
var rules = MakePlural.rules[type][this.lc];
|
183
|
+
if (!rules) {
|
184
|
+
if (req) throw new Error('Locale "' + this.lc + '" ' + type + ' rules not found');
|
185
|
+
this.categories[type] = ['other'];
|
186
|
+
return '\'other\'';
|
187
|
+
}
|
188
|
+
for (var r in rules) {
|
189
|
+
var _rules$r$trim$split = rules[r].trim().split(/\s*@\w*/);
|
190
|
+
|
191
|
+
var _rules$r$trim$split2 = _toArray(_rules$r$trim$split);
|
192
|
+
|
193
|
+
var cond = _rules$r$trim$split2[0];
|
194
|
+
var examples = _rules$r$trim$split2.slice(1);
|
195
|
+
var cat = r.replace('pluralRule-count-', '');
|
196
|
+
if (cond) cases.push([this.parser.parse(cond), cat]);
|
197
|
+
|
198
|
+
}
|
199
|
+
this.categories[type] = cases.map(function (c) {
|
200
|
+
return c[1];
|
201
|
+
}).concat('other');
|
202
|
+
if (cases.length === 1) {
|
203
|
+
return '(' + cases[0][0] + ') ? \'' + cases[0][1] + '\' : \'other\'';
|
204
|
+
} else {
|
205
|
+
return [].concat(_toConsumableArray(cases.map(function (c) {
|
206
|
+
return '(' + c[0] + ') ? \'' + c[1] + '\'';
|
207
|
+
})), ['\'other\'']).join('\n : ');
|
208
|
+
}
|
209
|
+
}
|
210
|
+
}, {
|
211
|
+
key: 'buildFunction',
|
212
|
+
value: function buildFunction(cardinals, ordinals) {
|
213
|
+
var _this3 = this;
|
214
|
+
|
215
|
+
var compile = function compile(c) {
|
216
|
+
return c ? (c[1] ? 'return ' : 'if (ord) return ') + _this3.compile.apply(_this3, _toConsumableArray(c)) : '';
|
217
|
+
},
|
218
|
+
fold = { vars: function vars(str) {
|
219
|
+
return (' ' + str + ';').replace(/(.{1,78})(,|$) ?/g, '$1$2\n ');
|
220
|
+
},
|
221
|
+
cond: function cond(str) {
|
222
|
+
return (' ' + str + ';').replace(/(.{1,78}) (\|\| |$) ?/gm, '$1\n $2');
|
223
|
+
} },
|
224
|
+
cond = [ordinals && ['ordinal', !cardinals], cardinals && ['cardinal', true]].map(compile).map(fold.cond),
|
225
|
+
body = [fold.vars(this.parser.vars())].concat(_toConsumableArray(cond)).join('\n').replace(/\s+$/gm, '').replace(/^[\s;]*[\r\n]+/gm, ''),
|
226
|
+
args = ordinals && cardinals ? 'n, ord' : 'n';
|
227
|
+
return new Function(args, body);
|
228
|
+
}
|
229
|
+
}, {
|
230
|
+
key: 'fnToString',
|
231
|
+
value: function fnToString(name) {
|
232
|
+
return Function.prototype.toString.call(this.fn).replace(/^function( \w+)?/, name ? 'function ' + name : 'function').replace('\n/**/', '');
|
233
|
+
}
|
234
|
+
}], [{
|
235
|
+
key: 'load',
|
236
|
+
value: function load() {
|
237
|
+
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
238
|
+
args[_key] = arguments[_key];
|
239
|
+
}
|
240
|
+
|
241
|
+
args.forEach(function (cldr) {
|
242
|
+
var data = cldr && cldr.supplemental || null;
|
243
|
+
if (!data) throw new Error('Data does not appear to be CLDR data');
|
244
|
+
MakePlural.rules = {
|
245
|
+
cardinal: data['plurals-type-cardinal'] || MakePlural.rules.cardinal,
|
246
|
+
ordinal: data['plurals-type-ordinal'] || MakePlural.rules.ordinal
|
247
|
+
};
|
248
|
+
});
|
249
|
+
return MakePlural;
|
250
|
+
}
|
251
|
+
}]);
|
252
|
+
|
253
|
+
return MakePlural;
|
254
|
+
})();
|
255
|
+
|
256
|
+
|
257
|
+
|
258
|
+
MakePlural.cardinals = true;
|
259
|
+
MakePlural.ordinals = false;
|
260
|
+
MakePlural.rules = { cardinal: {}, ordinal: {} };
|
261
|
+
|
262
|
+
|
263
|
+
return MakePlural;
|
264
|
+
}());
|
265
|
+
/* eslint-enable */
|
266
|
+
|
267
|
+
|
268
|
+
var validateParameterTypeNumber = function( value, name ) {
|
269
|
+
validateParameterType(
|
270
|
+
value,
|
271
|
+
name,
|
272
|
+
value === undefined || typeof value === "number",
|
273
|
+
"Number"
|
274
|
+
);
|
275
|
+
};
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
|
280
|
+
var validateParameterTypePluralType = function( value, name ) {
|
281
|
+
validateParameterType(
|
282
|
+
value,
|
283
|
+
name,
|
284
|
+
value === undefined || value === "cardinal" || value === "ordinal",
|
285
|
+
"String \"cardinal\" or \"ordinal\""
|
286
|
+
);
|
287
|
+
};
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
|
292
|
+
var pluralGeneratorFn = function( plural ) {
|
293
|
+
return function pluralGenerator( value ) {
|
294
|
+
validateParameterPresence( value, "value" );
|
295
|
+
validateParameterTypeNumber( value, "value" );
|
296
|
+
|
297
|
+
return plural( value );
|
298
|
+
};
|
299
|
+
};
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
|
304
|
+
/**
|
305
|
+
* .plural( value )
|
306
|
+
*
|
307
|
+
* @value [Number]
|
308
|
+
*
|
309
|
+
* Return the corresponding form (zero | one | two | few | many | other) of a
|
310
|
+
* value given locale.
|
311
|
+
*/
|
312
|
+
Globalize.plural =
|
313
|
+
Globalize.prototype.plural = function( value, options ) {
|
314
|
+
validateParameterPresence( value, "value" );
|
315
|
+
validateParameterTypeNumber( value, "value" );
|
316
|
+
return this.pluralGenerator( options )( value );
|
317
|
+
};
|
318
|
+
|
319
|
+
/**
|
320
|
+
* .pluralGenerator( [options] )
|
321
|
+
*
|
322
|
+
* Return a plural function (of the form below).
|
323
|
+
*
|
324
|
+
* fn( value )
|
325
|
+
*
|
326
|
+
* @value [Number]
|
327
|
+
*
|
328
|
+
* Return the corresponding form (zero | one | two | few | many | other) of a value given the
|
329
|
+
* default/instance locale.
|
330
|
+
*/
|
331
|
+
Globalize.pluralGenerator =
|
332
|
+
Globalize.prototype.pluralGenerator = function( options ) {
|
333
|
+
var args, cldr, isOrdinal, plural, returnFn, type;
|
334
|
+
|
335
|
+
validateParameterTypePlainObject( options, "options" );
|
336
|
+
|
337
|
+
options = options || {};
|
338
|
+
cldr = this.cldr;
|
339
|
+
|
340
|
+
args = [ options ];
|
341
|
+
type = options.type || "cardinal";
|
342
|
+
|
343
|
+
validateParameterTypePluralType( options.type, "options.type" );
|
344
|
+
|
345
|
+
validateDefaultLocale( cldr );
|
346
|
+
|
347
|
+
isOrdinal = type === "ordinal";
|
348
|
+
|
349
|
+
cldr.on( "get", validateCldr );
|
350
|
+
try {
|
351
|
+
cldr.supplemental([ "plurals-type-" + type, "{language}" ]);
|
352
|
+
} finally {
|
353
|
+
cldr.off( "get", validateCldr );
|
354
|
+
}
|
355
|
+
|
356
|
+
MakePlural.rules = {};
|
357
|
+
MakePlural.rules[ type ] = cldr.supplemental( "plurals-type-" + type );
|
358
|
+
|
359
|
+
plural = new MakePlural( cldr.attributes.language, {
|
360
|
+
"ordinals": isOrdinal,
|
361
|
+
"cardinals": !isOrdinal
|
362
|
+
});
|
363
|
+
|
364
|
+
returnFn = pluralGeneratorFn( plural );
|
365
|
+
|
366
|
+
runtimeBind( args, cldr, returnFn, [ plural ] );
|
367
|
+
|
368
|
+
return returnFn;
|
369
|
+
};
|
370
|
+
|
371
|
+
return Globalize;
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
|
376
|
+
}));
|
@@ -0,0 +1,203 @@
|
|
1
|
+
/**
|
2
|
+
* Globalize v1.7.0
|
3
|
+
*
|
4
|
+
* https://github.com/globalizejs/globalize
|
5
|
+
*
|
6
|
+
* Copyright OpenJS Foundation and other contributors
|
7
|
+
* Released under the MIT license
|
8
|
+
* https://jquery.org/license
|
9
|
+
*
|
10
|
+
* Date: 2021-08-02T11:53Z
|
11
|
+
*/
|
12
|
+
/*!
|
13
|
+
* Globalize v1.7.0 2021-08-02T11:53Z Released under the MIT license
|
14
|
+
* http://git.io/TrdQbw
|
15
|
+
*/
|
16
|
+
(function( root, factory ) {
|
17
|
+
|
18
|
+
// UMD returnExports
|
19
|
+
if ( typeof define === "function" && define.amd ) {
|
20
|
+
|
21
|
+
// AMD
|
22
|
+
define([
|
23
|
+
"cldr",
|
24
|
+
"../globalize",
|
25
|
+
"./number",
|
26
|
+
"./plural",
|
27
|
+
"cldr/event",
|
28
|
+
"cldr/supplemental"
|
29
|
+
], factory );
|
30
|
+
} else if ( typeof exports === "object" ) {
|
31
|
+
|
32
|
+
// Node, CommonJS
|
33
|
+
module.exports = factory( require( "cldrjs" ), require( "../globalize" ) );
|
34
|
+
} else {
|
35
|
+
|
36
|
+
// Extend global
|
37
|
+
factory( root.Cldr, root.Globalize );
|
38
|
+
}
|
39
|
+
}(this, function( Cldr, Globalize ) {
|
40
|
+
|
41
|
+
var formatMessage = Globalize._formatMessage,
|
42
|
+
runtimeBind = Globalize._runtimeBind,
|
43
|
+
validateCldr = Globalize._validateCldr,
|
44
|
+
validateDefaultLocale = Globalize._validateDefaultLocale,
|
45
|
+
validateParameterPresence = Globalize._validateParameterPresence,
|
46
|
+
validateParameterTypeString = Globalize._validateParameterTypeString,
|
47
|
+
validateParameterTypeNumber = Globalize._validateParameterTypeNumber;
|
48
|
+
|
49
|
+
|
50
|
+
/**
|
51
|
+
* format( value, numberFormatter, pluralGenerator, properties )
|
52
|
+
*
|
53
|
+
* @value [Number] The number to format
|
54
|
+
*
|
55
|
+
* @numberFormatter [String] A numberFormatter from Globalize.numberFormatter
|
56
|
+
*
|
57
|
+
* @pluralGenerator [String] A pluralGenerator from Globalize.pluralGenerator
|
58
|
+
*
|
59
|
+
* @properties [Object] containing relative time plural message.
|
60
|
+
*
|
61
|
+
* Format relative time.
|
62
|
+
*/
|
63
|
+
var relativeTimeFormat = function( value, numberFormatter, pluralGenerator, properties ) {
|
64
|
+
|
65
|
+
var relativeTime,
|
66
|
+
message = properties[ "relative-type-" + value ];
|
67
|
+
|
68
|
+
if ( message ) {
|
69
|
+
return message;
|
70
|
+
}
|
71
|
+
|
72
|
+
relativeTime = value <= 0 ? properties[ "relativeTime-type-past" ] :
|
73
|
+
properties[ "relativeTime-type-future" ];
|
74
|
+
|
75
|
+
value = Math.abs( value );
|
76
|
+
|
77
|
+
message = relativeTime[ "relativeTimePattern-count-" + pluralGenerator( value ) ];
|
78
|
+
return formatMessage( message, [ numberFormatter( value ) ] );
|
79
|
+
};
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
var relativeTimeFormatterFn = function( numberFormatter, pluralGenerator, properties ) {
|
85
|
+
return function relativeTimeFormatter( value ) {
|
86
|
+
validateParameterPresence( value, "value" );
|
87
|
+
validateParameterTypeNumber( value, "value" );
|
88
|
+
|
89
|
+
return relativeTimeFormat( value, numberFormatter, pluralGenerator, properties );
|
90
|
+
};
|
91
|
+
|
92
|
+
};
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
/**
|
98
|
+
* properties( unit, cldr, options )
|
99
|
+
*
|
100
|
+
* @unit [String] eg. "day", "week", "month", etc.
|
101
|
+
*
|
102
|
+
* @cldr [Cldr instance].
|
103
|
+
*
|
104
|
+
* @options [Object]
|
105
|
+
* - form: [String] eg. "short" or "narrow". Or falsy for default long form.
|
106
|
+
*
|
107
|
+
* Return relative time properties.
|
108
|
+
*/
|
109
|
+
var relativeTimeProperties = function( unit, cldr, options ) {
|
110
|
+
|
111
|
+
var form = options.form,
|
112
|
+
raw, properties, key, match;
|
113
|
+
|
114
|
+
if ( form ) {
|
115
|
+
unit = unit + "-" + form;
|
116
|
+
}
|
117
|
+
|
118
|
+
raw = cldr.main( [ "dates", "fields", unit ] );
|
119
|
+
properties = {
|
120
|
+
"relativeTime-type-future": raw[ "relativeTime-type-future" ],
|
121
|
+
"relativeTime-type-past": raw[ "relativeTime-type-past" ]
|
122
|
+
};
|
123
|
+
for ( key in raw ) {
|
124
|
+
if ( raw.hasOwnProperty( key ) ) {
|
125
|
+
match = /relative-type-(-?[0-9]+)/.exec( key );
|
126
|
+
if ( match ) {
|
127
|
+
properties[ key ] = raw[ key ];
|
128
|
+
}
|
129
|
+
}
|
130
|
+
}
|
131
|
+
|
132
|
+
return properties;
|
133
|
+
};
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
138
|
+
/**
|
139
|
+
* .formatRelativeTime( value, unit [, options] )
|
140
|
+
*
|
141
|
+
* @value [Number] The number of unit to format.
|
142
|
+
*
|
143
|
+
* @unit [String] see .relativeTimeFormatter() for details.
|
144
|
+
*
|
145
|
+
* @options [Object] see .relativeTimeFormatter() for details.
|
146
|
+
*
|
147
|
+
* Formats a relative time according to the given unit, options, and the default/instance locale.
|
148
|
+
*/
|
149
|
+
Globalize.formatRelativeTime =
|
150
|
+
Globalize.prototype.formatRelativeTime = function( value, unit, options ) {
|
151
|
+
validateParameterPresence( value, "value" );
|
152
|
+
validateParameterTypeNumber( value, "value" );
|
153
|
+
|
154
|
+
return this.relativeTimeFormatter( unit, options )( value );
|
155
|
+
};
|
156
|
+
|
157
|
+
/**
|
158
|
+
* .relativeTimeFormatter( unit [, options ])
|
159
|
+
*
|
160
|
+
* @unit [String] String value indicating the unit to be formatted. eg. "day", "week", "month", etc.
|
161
|
+
*
|
162
|
+
* @options [Object]
|
163
|
+
* - form: [String] eg. "short" or "narrow". Or falsy for default long form.
|
164
|
+
*
|
165
|
+
* Returns a function that formats a relative time according to the given unit, options, and the
|
166
|
+
* default/instance locale.
|
167
|
+
*/
|
168
|
+
Globalize.relativeTimeFormatter =
|
169
|
+
Globalize.prototype.relativeTimeFormatter = function( unit, options ) {
|
170
|
+
var args, cldr, numberFormatter, pluralGenerator, properties, returnFn;
|
171
|
+
|
172
|
+
validateParameterPresence( unit, "unit" );
|
173
|
+
validateParameterTypeString( unit, "unit" );
|
174
|
+
|
175
|
+
cldr = this.cldr;
|
176
|
+
options = options || {};
|
177
|
+
|
178
|
+
args = [ unit, options ];
|
179
|
+
|
180
|
+
validateDefaultLocale( cldr );
|
181
|
+
|
182
|
+
cldr.on( "get", validateCldr );
|
183
|
+
try {
|
184
|
+
properties = relativeTimeProperties( unit, cldr, options );
|
185
|
+
} finally {
|
186
|
+
cldr.off( "get", validateCldr );
|
187
|
+
}
|
188
|
+
numberFormatter = this.numberFormatter( options );
|
189
|
+
pluralGenerator = this.pluralGenerator();
|
190
|
+
|
191
|
+
returnFn = relativeTimeFormatterFn( numberFormatter, pluralGenerator, properties );
|
192
|
+
|
193
|
+
runtimeBind( args, cldr, returnFn, [ numberFormatter, pluralGenerator, properties ] );
|
194
|
+
|
195
|
+
return returnFn;
|
196
|
+
};
|
197
|
+
|
198
|
+
return Globalize;
|
199
|
+
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
}));
|