globalize-rpk 1.7.0
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/CONTRIBUTING.md +5 -0
- package/LICENSE +20 -0
- package/README.md +818 -0
- package/doc/api/core/constructor.md +28 -0
- package/doc/api/core/load.md +96 -0
- package/doc/api/core/locale.md +43 -0
- package/doc/api/currency/currency-formatter.md +196 -0
- package/doc/api/currency/currency-to-parts-formatter.md +117 -0
- package/doc/api/date/date-formatter.md +203 -0
- package/doc/api/date/date-parser.md +60 -0
- package/doc/api/date/date-to-parts-formatter.md +176 -0
- package/doc/api/date/load-iana-time-zone.md +29 -0
- package/doc/api/message/load-messages.md +105 -0
- package/doc/api/message/message-formatter.md +208 -0
- package/doc/api/number/number-formatter.md +202 -0
- package/doc/api/number/number-parser.md +130 -0
- package/doc/api/number/number-to-parts-formatter.md +140 -0
- package/doc/api/plural/plural-generator.md +84 -0
- package/doc/api/relative-time/relative-time-formatter.md +60 -0
- package/doc/api/unit/unit-formatter.md +72 -0
- package/doc/blog-post/2017-07-xx-1.3.0-announcement.md +177 -0
- package/doc/cldr.md +114 -0
- package/doc/error/e-default-locale-not-defined.md +9 -0
- package/doc/error/e-invalid-cldr.md +14 -0
- package/doc/error/e-invalid-par-type.md +12 -0
- package/doc/error/e-invalid-par-value.md +11 -0
- package/doc/error/e-missing-cldr.md +11 -0
- package/doc/error/e-missing-parameter.md +10 -0
- package/doc/error/e-missing-plural-module.md +9 -0
- package/doc/error/e-par-missing-key.md +11 -0
- package/doc/error/e-par-out-of-range.md +13 -0
- package/doc/error/e-unsupported.md +10 -0
- package/doc/migrating-from-0.x.md +64 -0
- package/examples/amd-bower/.bowerrc +7 -0
- package/examples/amd-bower/README.md +65 -0
- package/examples/amd-bower/bower.json +13 -0
- package/examples/amd-bower/index.html +46 -0
- package/examples/amd-bower/main.js +141 -0
- package/examples/amd-bower/messages/en.json +12 -0
- package/examples/amd-bower/package.json +14 -0
- package/examples/app-npm-webpack/README.md +74 -0
- package/examples/app-npm-webpack/app/index.js +89 -0
- package/examples/app-npm-webpack/index-template.html +71 -0
- package/examples/app-npm-webpack/messages/ar.json +25 -0
- package/examples/app-npm-webpack/messages/de.json +21 -0
- package/examples/app-npm-webpack/messages/en.json +21 -0
- package/examples/app-npm-webpack/messages/es.json +21 -0
- package/examples/app-npm-webpack/messages/pt.json +21 -0
- package/examples/app-npm-webpack/messages/ru.json +23 -0
- package/examples/app-npm-webpack/messages/zh.json +20 -0
- package/examples/app-npm-webpack/package.json +17 -0
- package/examples/app-npm-webpack/webpack-config.js +63 -0
- package/examples/globalize-compiler/README.md +45 -0
- package/examples/globalize-compiler/app.js +58 -0
- package/examples/globalize-compiler/development.html +121 -0
- package/examples/globalize-compiler/messages.json +12 -0
- package/examples/globalize-compiler/package.json +15 -0
- package/examples/globalize-compiler/production.html +75 -0
- package/examples/node-npm/README.md +57 -0
- package/examples/node-npm/main.js +65 -0
- package/examples/node-npm/messages/en.json +12 -0
- package/examples/node-npm/package.json +10 -0
- package/examples/plain-javascript/README.md +81 -0
- package/examples/plain-javascript/index.html +445 -0
- package/package.json +109 -0
@@ -0,0 +1,202 @@
|
|
1
|
+
## .numberFormatter( [options] ) ➜ function( value )
|
2
|
+
|
3
|
+
Return a function that formats a number according to the given options.
|
4
|
+
|
5
|
+
The returned function is invoked with one argument: the Number `value` to be formatted.
|
6
|
+
|
7
|
+
### Parameters
|
8
|
+
|
9
|
+
#### options.style
|
10
|
+
|
11
|
+
Optional. String `decimal` (default), or `percent`.
|
12
|
+
|
13
|
+
#### options.minimumIntegerDigits
|
14
|
+
|
15
|
+
Optional. Non-negative integer Number value indicating the minimum integer digits to be used. Numbers will be padded with leading zeroes if necessary.
|
16
|
+
|
17
|
+
#### options.minimumFractionDigits, options.maximumFractionDigits
|
18
|
+
|
19
|
+
Optional. Non-negative integer Number values indicating the minimum and maximum fraction digits to be used. Numbers will be rounded or padded with trailing zeroes if necessary. Either one or both of these properties must be present. If they are, they will override minimum and maximum fraction digits derived from the CLDR patterns.
|
20
|
+
|
21
|
+
#### options.minimumSignificantDigits, options.maximumSignificantDigits
|
22
|
+
|
23
|
+
Optional. Positive integer Number values indicating the minimum and maximum fraction digits to be shown. Either none or both of these properties are present. If they are, they override minimum and maximum integer and fraction digits. The formatter uses however many integer and fraction digits are required to display the specified number of significant digits.
|
24
|
+
|
25
|
+
#### options.round
|
26
|
+
|
27
|
+
Optional. String with rounding method `ceil`, `floor`, `round` (default), or `truncate`.
|
28
|
+
|
29
|
+
#### options.useGrouping
|
30
|
+
|
31
|
+
Optional. Boolean (default is true) value indicating whether a grouping separator should be used.
|
32
|
+
|
33
|
+
#### options.compact
|
34
|
+
|
35
|
+
Optional. String `short` or `long` indicating which compact number format should be used to represent the number.
|
36
|
+
|
37
|
+
### Examples
|
38
|
+
|
39
|
+
#### Static Formatter
|
40
|
+
|
41
|
+
Prior to using any number methods, you must load `cldr/main/{locale}/numbers.json` and `cldr/supplemental/numberingSystems.json`. Read [CLDR content][] if you need more information.
|
42
|
+
|
43
|
+
[CLDR content]: ../../../README.md#2-cldr-content
|
44
|
+
|
45
|
+
You can use the static method `Globalize.numberFormatter()`, which uses the default locale.
|
46
|
+
|
47
|
+
```javascript
|
48
|
+
var formatter;
|
49
|
+
|
50
|
+
Globalize.locale( "en" );
|
51
|
+
formatter = Globalize.numberFormatter();
|
52
|
+
|
53
|
+
formatter( 3.141592 );
|
54
|
+
// > "3.142"
|
55
|
+
```
|
56
|
+
|
57
|
+
#### Instance Formatter
|
58
|
+
|
59
|
+
You can use the instance method `.numberFormatter()`, which uses the instance
|
60
|
+
locale.
|
61
|
+
|
62
|
+
```javascript
|
63
|
+
var arFormatter = Globalize( "ar" ).numberFormatter(),
|
64
|
+
esFormatter = Globalize( "es" ).numberFormatter(),
|
65
|
+
zhFormatter = Globalize( "zh-u-nu-native" ).numberFormatter();
|
66
|
+
|
67
|
+
arFormatter( 3.141592 );
|
68
|
+
// > "٣٫١٤٢"
|
69
|
+
|
70
|
+
esFormatter( 3.141592 );
|
71
|
+
// > "3,142"
|
72
|
+
|
73
|
+
zhFormatter( 3.141592 );
|
74
|
+
// > "三.一四二"
|
75
|
+
```
|
76
|
+
|
77
|
+
#### Configuring decimal places
|
78
|
+
|
79
|
+
The number of decimal places can be decreased or increased using `minimumFractionDigits` and `maximumFractionDigits`.
|
80
|
+
|
81
|
+
```javascript
|
82
|
+
Globalize.numberFormatter({ maximumFractionDigits: 2 })( 3.141592 );
|
83
|
+
// > "3.14"
|
84
|
+
|
85
|
+
Globalize.numberFormatter({ minimumFractionDigits: 2 })( 1.5 );
|
86
|
+
// > "1.50"
|
87
|
+
```
|
88
|
+
|
89
|
+
#### Configuring significant digits
|
90
|
+
|
91
|
+
The number of significant (non-zero) digits can be decreased or increased using `minimumSignificantDigits` and `maximumSignificantDigits`.
|
92
|
+
|
93
|
+
```javascript
|
94
|
+
var formatter = Globalize.numberFormatter({
|
95
|
+
minimumSignificantDigits: 1,
|
96
|
+
maximumSignificantDigits: 3
|
97
|
+
});
|
98
|
+
|
99
|
+
formatter( 3.141592 );
|
100
|
+
// > "3.14"
|
101
|
+
|
102
|
+
formatter = Globalize.numberFormatter({
|
103
|
+
minimumSignificantDigits: 1,
|
104
|
+
maximumSignificantDigits: 3
|
105
|
+
});
|
106
|
+
|
107
|
+
formatter( 12345 );
|
108
|
+
// > "12,300"
|
109
|
+
|
110
|
+
formatter = Globalize.numberFormatter({
|
111
|
+
minimumSignificantDigits: 1,
|
112
|
+
maximumSignificantDigits: 3
|
113
|
+
});
|
114
|
+
|
115
|
+
formatter( 0.00012345 );
|
116
|
+
// > "0.000123"
|
117
|
+
```
|
118
|
+
|
119
|
+
#### Formatting Percentages
|
120
|
+
|
121
|
+
Numbers can be formatted as percentages.
|
122
|
+
|
123
|
+
```javascript
|
124
|
+
var enFormatter = Globalize( "en" ).numberFormatter({
|
125
|
+
style: "percent",
|
126
|
+
minimumFractionDigits: 1,
|
127
|
+
maximumFractionDigits: 1
|
128
|
+
});
|
129
|
+
|
130
|
+
var frFormatter = Globalize( "fr" ).numberFormatter({
|
131
|
+
style: "percent",
|
132
|
+
minimumFractionDigits: 2,
|
133
|
+
maximumFractionDigits: 2
|
134
|
+
});
|
135
|
+
|
136
|
+
enFormatter( 0.0016 );
|
137
|
+
// > "0.2%"
|
138
|
+
|
139
|
+
enFormatter( 0.0014 );
|
140
|
+
// > "0.1%"
|
141
|
+
|
142
|
+
frFormatter( 0.0005 );
|
143
|
+
// > "0,05 %"
|
144
|
+
```
|
145
|
+
|
146
|
+
#### Formatting Compact Numbers
|
147
|
+
|
148
|
+
Long numbers can be represented in a compact format, with `short` using abbreviated units and `long` using the full unit name.
|
149
|
+
|
150
|
+
```javascript
|
151
|
+
var shortFormatter = Globalize( "en" ).numberFormatter({
|
152
|
+
compact: "short"
|
153
|
+
});
|
154
|
+
|
155
|
+
var longFormatter = Globalize( "en" ).numberFormatter({
|
156
|
+
compact: "long"
|
157
|
+
});
|
158
|
+
|
159
|
+
shortFormatter( 27588910 );
|
160
|
+
// > "28M"
|
161
|
+
|
162
|
+
longFormatter( 27588910 );
|
163
|
+
// > "28 million"
|
164
|
+
```
|
165
|
+
|
166
|
+
The minimumSignificantDigits and maximumSignificantDigits options are specially useful to control the number of digits to display.
|
167
|
+
|
168
|
+
```js
|
169
|
+
Globalize( "en" ).formatNumber( 27588910, {
|
170
|
+
compact: "short",
|
171
|
+
minimumSignificantDigits: 3,
|
172
|
+
maximumSignificantDigits: 3
|
173
|
+
});
|
174
|
+
// > "27.6M"
|
175
|
+
```
|
176
|
+
|
177
|
+
#### Configuring Rounding
|
178
|
+
|
179
|
+
Numbers with a decreased amount of decimal places can be rounded up, rounded down, rounded arithmetically, or truncated by setting the `round` option to `ceil`, `floor`, `round` (default), or `truncate`.
|
180
|
+
|
181
|
+
```javascript
|
182
|
+
var formatter = Globalize.numberFormatter({
|
183
|
+
maximumFractionDigits: 2,
|
184
|
+
round: "ceil"
|
185
|
+
});
|
186
|
+
|
187
|
+
formatter( 3.141592 );
|
188
|
+
// > "3.15"
|
189
|
+
```
|
190
|
+
|
191
|
+
#### Performance Suggestions
|
192
|
+
|
193
|
+
For improved performance on iterations, the formatter should be created before the loop. Then, it can be reused in each iteration.
|
194
|
+
|
195
|
+
```javascript
|
196
|
+
var numbers = [ 1, 1, 2, 3, ... ];
|
197
|
+
var formatter = Globalize( "en" ).numberFormatter();
|
198
|
+
|
199
|
+
formattedNumbers = numbers.map(function( number ) {
|
200
|
+
return formatter( number );
|
201
|
+
});
|
202
|
+
```
|
@@ -0,0 +1,130 @@
|
|
1
|
+
## .numberParser( [options] ) ➜ function( value )
|
2
|
+
|
3
|
+
Return a function that parses a String representing a number according to the given options. If value is invalid, `NaN` is returned.
|
4
|
+
|
5
|
+
The returned function is invoked with one argument: the String representing a number `value` to be parsed.
|
6
|
+
|
7
|
+
### Parameters
|
8
|
+
|
9
|
+
#### options
|
10
|
+
|
11
|
+
See [.numberFormatter() options](./number-formatter.md#parameters).
|
12
|
+
|
13
|
+
#### value
|
14
|
+
|
15
|
+
String with number to be parsed, eg. `"3.14"`.
|
16
|
+
|
17
|
+
### Example
|
18
|
+
|
19
|
+
Prior to using any number methods, you must load `cldr/main/{locale}/numbers.json` and `cldr/supplemental/numberingSystems.json`. Read [CLDR content][] if you need more information.
|
20
|
+
|
21
|
+
[CLDR content]: ../../../README.md#2-cldr-content
|
22
|
+
|
23
|
+
You can use the static method `Globalize.numberParser()`, which uses the default locale.
|
24
|
+
|
25
|
+
```javascript
|
26
|
+
var parser;
|
27
|
+
|
28
|
+
Globalize.locale( "en" );
|
29
|
+
parser = Globalize.numberParser();
|
30
|
+
|
31
|
+
parser( "3.14" );
|
32
|
+
// > 3.14
|
33
|
+
```
|
34
|
+
|
35
|
+
You can use the instance method `.numberParser()`, which uses the instance locale.
|
36
|
+
|
37
|
+
```javascript
|
38
|
+
var enParser = Globalize( "en" ).numberParser(),
|
39
|
+
esParser = Globalize( "es" ).numberParser();
|
40
|
+
|
41
|
+
enParser( "3.14" );
|
42
|
+
// > 3.14
|
43
|
+
|
44
|
+
esParser( "3,14" );
|
45
|
+
// > 3.14
|
46
|
+
```
|
47
|
+
|
48
|
+
Some more examples.
|
49
|
+
|
50
|
+
```javascript
|
51
|
+
var enParser = Globalize( "en" ).numberParser();
|
52
|
+
|
53
|
+
enParser( "12,735" );
|
54
|
+
// > 12735
|
55
|
+
|
56
|
+
enParser( "12,735.00" );
|
57
|
+
// > 12735
|
58
|
+
|
59
|
+
Globalize( "en" ).numberParser({ style: "percent" })( "100%" );
|
60
|
+
// > 1
|
61
|
+
|
62
|
+
enParser( "∞" );
|
63
|
+
// > Infinity
|
64
|
+
|
65
|
+
enParser( "-3" );
|
66
|
+
// > -3
|
67
|
+
|
68
|
+
enParser( "-∞" );
|
69
|
+
// > -Infinity
|
70
|
+
|
71
|
+
enParser( "invalid-stuff" );
|
72
|
+
// > NaN
|
73
|
+
|
74
|
+
enParser( "invalid-stuff-that-includes-number-123" );
|
75
|
+
// > NaN
|
76
|
+
|
77
|
+
enParser( "invalid-stuff-123-that-includes-number" );
|
78
|
+
// > NaN
|
79
|
+
|
80
|
+
enParser( "123-invalid-stuff-that-includes-number" );
|
81
|
+
// > NaN
|
82
|
+
|
83
|
+
// Invalid decimal separator. (note `.` is used as decimal separator for English)
|
84
|
+
enParser( "3,14" );
|
85
|
+
// > NaN
|
86
|
+
|
87
|
+
// Invalid grouping separator position.
|
88
|
+
enParser( "127,35.00" );
|
89
|
+
// > NaN
|
90
|
+
```
|
91
|
+
|
92
|
+
Loose matching examples.
|
93
|
+
|
94
|
+
```js
|
95
|
+
var svParser = Globalize( "sv" ).numberParser();
|
96
|
+
|
97
|
+
// Swedish uses NO-BREAK-SPACE U+00A0 as grouping separator.
|
98
|
+
svParser( "1\xA0000,50" );
|
99
|
+
// > 1000.5
|
100
|
+
|
101
|
+
// The parser is lenient and accepts various space characters like regular space
|
102
|
+
// SPACE U+0020. Technically, it accepts any character of the Unicode general
|
103
|
+
// category [:Zs:].
|
104
|
+
svParser( "1 000,50" );
|
105
|
+
// > 1000.5
|
106
|
+
|
107
|
+
var fiParser = Globalize( "fi" ).numberParser();
|
108
|
+
|
109
|
+
// Finish uses MINUS SIGN U+2212 for the minus sign.
|
110
|
+
fiParser( "\u22123" );
|
111
|
+
// > -3
|
112
|
+
|
113
|
+
// The parser is lenient and accepts various hyphen characters like regular
|
114
|
+
// HYPHEN-MINUS U+002D. Technically, it accepts any character of the Unicode
|
115
|
+
// general category [:Dash:].
|
116
|
+
fiParser( "-3" );
|
117
|
+
// > -3
|
118
|
+
```
|
119
|
+
|
120
|
+
For improved performance on iterations, first create the parser. Then, reuse it
|
121
|
+
on each loop.
|
122
|
+
|
123
|
+
```javascript
|
124
|
+
var formattedNumbers = [ "1", "1", "2", "3", ... ];
|
125
|
+
var parser = Globalize( "en" ).numberParser();
|
126
|
+
|
127
|
+
numbers = formattedNumbers.map(function( formattedNumber ) {
|
128
|
+
return parser( formattedNumber );
|
129
|
+
});
|
130
|
+
```
|
@@ -0,0 +1,140 @@
|
|
1
|
+
## .numberToPartsFormatter( [options] ) ➜ function( value )
|
2
|
+
|
3
|
+
Return a function that formats a number into parts tokens according to the given options.
|
4
|
+
|
5
|
+
The returned function is invoked with one argument: the Number `value` to be formatted.
|
6
|
+
|
7
|
+
### Parameters
|
8
|
+
|
9
|
+
#### options
|
10
|
+
|
11
|
+
Please, see [.numberFormatter() options](./number-formatter.md#parameters).
|
12
|
+
|
13
|
+
### Returns
|
14
|
+
|
15
|
+
An Array of objects containing the formatted number in parts. The returned structure looks like this:
|
16
|
+
|
17
|
+
- `decimal`
|
18
|
+
|
19
|
+
The decimal separator string, e.g., `"."`.
|
20
|
+
|
21
|
+
- `fraction`
|
22
|
+
|
23
|
+
The fraction number.
|
24
|
+
|
25
|
+
- `group`
|
26
|
+
|
27
|
+
The group separator string, e.g., `","`.
|
28
|
+
|
29
|
+
- `infinity`
|
30
|
+
|
31
|
+
The Infinity string, e.g., `"∞"`.
|
32
|
+
|
33
|
+
- `integer`
|
34
|
+
|
35
|
+
The integer number.
|
36
|
+
|
37
|
+
- `literal`
|
38
|
+
|
39
|
+
Any literal strings or whitespace in the formatted number.
|
40
|
+
|
41
|
+
- `minusSign`
|
42
|
+
|
43
|
+
The minus sign string, e.g., `"-"`.
|
44
|
+
|
45
|
+
- `nan`
|
46
|
+
|
47
|
+
The NaN string, e.g., `"NaN"`.
|
48
|
+
|
49
|
+
- `plusSign`
|
50
|
+
|
51
|
+
The plus sign string, e.g., `"+"`.
|
52
|
+
|
53
|
+
- `percentSign`
|
54
|
+
|
55
|
+
The percent sign string, e.g., `"%"`.
|
56
|
+
|
57
|
+
- `compact`
|
58
|
+
|
59
|
+
The compact string, e.g., `"thousand"`.
|
60
|
+
|
61
|
+
### Examples
|
62
|
+
|
63
|
+
Prior to using any number methods, you must load `cldr/main/{locale}/numbers.json` and `cldr/supplemental/numberingSystems.json`. Read [CLDR content][] if you need more information.
|
64
|
+
|
65
|
+
[CLDR content]: ../../../README.md#2-cldr-content
|
66
|
+
|
67
|
+
#### Static Formatter
|
68
|
+
|
69
|
+
You can use the static method `Globalize.numberToPartsFormatter()`, which uses the default locale.
|
70
|
+
|
71
|
+
```javascript
|
72
|
+
var formatter;
|
73
|
+
|
74
|
+
Globalize.locale( "en" );
|
75
|
+
formatter = Globalize.numberToPartsFormatter();
|
76
|
+
|
77
|
+
formatter( 3.141592 );
|
78
|
+
// > [
|
79
|
+
// { "type": "integer", "value": "3" },
|
80
|
+
// { "type": "decimal", "value": "." },
|
81
|
+
// { "type": "fraction", "value": "142" }
|
82
|
+
// ]
|
83
|
+
```
|
84
|
+
|
85
|
+
#### Instance Formatter
|
86
|
+
|
87
|
+
You can use the instance method `.numberFormatter()`, which uses the instance
|
88
|
+
locale.
|
89
|
+
|
90
|
+
```javascript
|
91
|
+
var arFormatter = Globalize( "ar" ).numberToPartsFormatter(),
|
92
|
+
esFormatter = Globalize( "es" ).numberToPartsFormatter(),
|
93
|
+
zhFormatter = Globalize( "zh-u-nu-native" ).numberToPartsFormatter();
|
94
|
+
|
95
|
+
arFormatter( 3.141592 );
|
96
|
+
// > [
|
97
|
+
// { "type": "integer", "value": "٣" },
|
98
|
+
// { "type": "decimal", "value": "٫" },
|
99
|
+
// { "type": "fraction", "value": "١٤٢" }
|
100
|
+
// ]
|
101
|
+
|
102
|
+
esFormatter( 3.141592 );
|
103
|
+
// > [
|
104
|
+
// { "type": "integer", "value": "3" },
|
105
|
+
// { "type": "decimal", "value": "," },
|
106
|
+
// { "type": "fraction", "value": "142" }
|
107
|
+
// ]
|
108
|
+
|
109
|
+
zhFormatter( 3.141592 );
|
110
|
+
// > [
|
111
|
+
// { "type": "integer", "value": "三" },
|
112
|
+
// { "type": "decimal", "value": "." },
|
113
|
+
// { "type": "fraction", "value": "一四二" }
|
114
|
+
// ]
|
115
|
+
```
|
116
|
+
|
117
|
+
The information is available separately and it can be formatted and concatenated again in a customized way. For example by using [`Array.prototype.map()`][], [arrow functions][], a [switch statement][], [template literals][], and [`Array.prototype.reduce()`][].
|
118
|
+
|
119
|
+
[`Array.prototype.map()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
|
120
|
+
[arrow functions]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
|
121
|
+
[switch statement]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
|
122
|
+
[template literals]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
|
123
|
+
[`Array.prototype.reduce()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
|
124
|
+
|
125
|
+
#### More Examples
|
126
|
+
|
127
|
+
Please, see [.numberFormatter() example](./number-formatter.md#example) for additional examples such as configuring decimal places, significant digits, percentages, and compact numbers.
|
128
|
+
|
129
|
+
#### Performance Suggestions
|
130
|
+
|
131
|
+
For improved performance on iterations, the formatter should be created before the loop. Then, it can be reused in each iteration.
|
132
|
+
|
133
|
+
```javascript
|
134
|
+
var numbers = [ 1, 1, 2, 3, ... ];
|
135
|
+
var formatter = Globalize( "en" ).numberFormatter();
|
136
|
+
|
137
|
+
formattedNumbers = numbers.map(function( number ) {
|
138
|
+
return formatter( number );
|
139
|
+
});
|
140
|
+
```
|
@@ -0,0 +1,84 @@
|
|
1
|
+
## .pluralGenerator( [options] ) ➜ function( value )
|
2
|
+
|
3
|
+
It supports the creation of internationalized messages with plural inflection by returning a function that returns the value's plural group: `zero`, `one`, `two`, `few`, `many`, or `other`.
|
4
|
+
|
5
|
+
The returned function is invoked with one argument: the Number `value` for which to return the plural group.
|
6
|
+
|
7
|
+
### Parameters
|
8
|
+
|
9
|
+
#### options.type
|
10
|
+
|
11
|
+
Optional. String `cardinal` (default), or `ordinal`.
|
12
|
+
|
13
|
+
#### value
|
14
|
+
|
15
|
+
A Number for which to return the plural group.
|
16
|
+
|
17
|
+
### Example
|
18
|
+
|
19
|
+
Prior to using any plural method, you must load either `supplemental/plurals.json` for cardinals or `supplemental/ordinals.json` for ordinals.
|
20
|
+
|
21
|
+
Read [CLDR content][] if you need more information.
|
22
|
+
|
23
|
+
[CLDR content]: ../../../README.md#2-cldr-content
|
24
|
+
|
25
|
+
You can use the static method `Globalize.pluralGenerator()`, which uses the default locale.
|
26
|
+
|
27
|
+
```javascript
|
28
|
+
var plural;
|
29
|
+
|
30
|
+
Globalize.locale( "en" );
|
31
|
+
|
32
|
+
// Cardinals
|
33
|
+
plural = Globalize.pluralGenerator();
|
34
|
+
|
35
|
+
plural( 0 );
|
36
|
+
// > "other"
|
37
|
+
|
38
|
+
plural( 1 );
|
39
|
+
// > "one"
|
40
|
+
|
41
|
+
plural( 2 );
|
42
|
+
// > "other"
|
43
|
+
|
44
|
+
// Ordinals
|
45
|
+
plural = Globalize.pluralGenerator({ type: "ordinal" });
|
46
|
+
|
47
|
+
plural( 0 );
|
48
|
+
// > "other"
|
49
|
+
|
50
|
+
plural( 1 );
|
51
|
+
// > "one"
|
52
|
+
|
53
|
+
plural( 2 );
|
54
|
+
// > "two"
|
55
|
+
```
|
56
|
+
|
57
|
+
You can use the instance method `.pluralGenerator()`, which uses the instance locale.
|
58
|
+
|
59
|
+
```javascript
|
60
|
+
var plural = Globalize( "zh" ).pluralGenerator();
|
61
|
+
|
62
|
+
plural( 1 );
|
63
|
+
// > "other"
|
64
|
+
```
|
65
|
+
|
66
|
+
For comparison (cardinals):
|
67
|
+
|
68
|
+
| | en (English) | ru (Russian) | ar (Arabic) |
|
69
|
+
| ------------- | ------------ | ------------ | ----------- |
|
70
|
+
| `plural( 0 )` | `other` | `many` | `zero` |
|
71
|
+
| `plural( 1 )` | `one` | `one` | `one` |
|
72
|
+
| `plural( 2 )` | `other` | `few` | `two` |
|
73
|
+
| `plural( 3 )` | `other` | `few` | `few` |
|
74
|
+
| `plural( 5 )` | `other` | `many` | `few` |
|
75
|
+
|
76
|
+
For comparison (ordinals):
|
77
|
+
|
78
|
+
| | en (English) | ru (Russian) | ar (Arabic) |
|
79
|
+
| ---------------------------------- | ------------ | ------------ | ----------- |
|
80
|
+
| `plural( 0, { type: "ordinal" } )` | `other` | `other` | `other` |
|
81
|
+
| `plural( 1, { type: "ordinal" } )` | `one` | `other` | `other` |
|
82
|
+
| `plural( 2, { type: "ordinal" } )` | `two` | `other` | `other` |
|
83
|
+
| `plural( 3, { type: "ordinal" } )` | `few` | `other` | `other` |
|
84
|
+
| `plural( 5, { type: "ordinal" } )` | `other` | `other` | `other` |
|
@@ -0,0 +1,60 @@
|
|
1
|
+
## .relativeTimeFormatter( unit [, options] ) ➜ function( value )
|
2
|
+
|
3
|
+
Returns a function that formats a relative time according to the given unit, options, and the default/instance locale.
|
4
|
+
|
5
|
+
The returned function is invoked with one argument: the number `value` to be formatted.
|
6
|
+
|
7
|
+
### Parameters
|
8
|
+
|
9
|
+
#### unit
|
10
|
+
|
11
|
+
String value indicating the unit to be formatted. eg. "day", "week", "month", etc.
|
12
|
+
|
13
|
+
#### options.form
|
14
|
+
|
15
|
+
String, e.g., `"short"` or `"narrow"`, or falsy for default long form.
|
16
|
+
|
17
|
+
#### value
|
18
|
+
|
19
|
+
The number to be formatted.
|
20
|
+
|
21
|
+
|
22
|
+
### Example
|
23
|
+
|
24
|
+
Prior to using any relative time methods, you must load `cldr/main/{locale}/dateFields.json` and the CLDR content required by the number and plural modules. Read [CLDR content][] if you need more information.
|
25
|
+
|
26
|
+
[CLDR content]: ../../../README.md#2-cldr-content
|
27
|
+
|
28
|
+
You can use the static method `Globalize.relativeTimeFormatter()`, which uses the default locale.
|
29
|
+
|
30
|
+
```javascript
|
31
|
+
var formatter;
|
32
|
+
|
33
|
+
Globalize.locale( "en" );
|
34
|
+
formatter = Globalize.relativeTimeFormatter( "month" );
|
35
|
+
|
36
|
+
formatter( 1 );
|
37
|
+
// > "next month"
|
38
|
+
|
39
|
+
formatter( 3 );
|
40
|
+
// > "in 3 months"
|
41
|
+
|
42
|
+
formatter( -1 );
|
43
|
+
// > "last month"
|
44
|
+
|
45
|
+
formatter( -3 );
|
46
|
+
// > "3 months ago"
|
47
|
+
```
|
48
|
+
|
49
|
+
You can use the instance method `.relativeTimeFormatter()`, which uses the instance locale.
|
50
|
+
|
51
|
+
```javascript
|
52
|
+
var globalize = new Globalize( "en" ),
|
53
|
+
formatter = globalize.relativeTimeFormatter( "week" );
|
54
|
+
|
55
|
+
formatter( 1 );
|
56
|
+
// > "next week"
|
57
|
+
```
|
58
|
+
|
59
|
+
|
60
|
+
|
@@ -0,0 +1,72 @@
|
|
1
|
+
## .unitFormatter( unit [, options] ) ➜ function( value )
|
2
|
+
|
3
|
+
Returns a function that formats a unit according to the given unit, options, and the default/instance locale.
|
4
|
+
|
5
|
+
The returned function is invoked with one argument: the number `value` to be formatted.
|
6
|
+
|
7
|
+
### Parameters
|
8
|
+
|
9
|
+
#### unit
|
10
|
+
|
11
|
+
String value indicating the unit to be formatted. eg. "day", "week", "month", etc. Could also be a compound unit, eg. "mile-per-hour" or "mile/hour"
|
12
|
+
|
13
|
+
#### options.form
|
14
|
+
|
15
|
+
Optional. String, e.g., `"long"` (default), `"short"` or `"narrow"`.
|
16
|
+
|
17
|
+
#### options.numberFormatter
|
18
|
+
|
19
|
+
Optional. A number formatter function. Defaults to `Globalize.numberFormatter()` for the current locale using the default options.
|
20
|
+
|
21
|
+
#### value
|
22
|
+
|
23
|
+
The number to be formatted.
|
24
|
+
|
25
|
+
### Example
|
26
|
+
|
27
|
+
Prior to using any unit methods, you must load `cldr/main/{locale}/units.json` and the CLDR content required by the plural module. Read [CLDR content][] if you need more information.
|
28
|
+
|
29
|
+
[CLDR content]: ../../../README.md#2-cldr-content
|
30
|
+
|
31
|
+
You can use the static method `Globalize.unitFormatter()`, which uses the default locale.
|
32
|
+
|
33
|
+
```javascript
|
34
|
+
var customNumberFormatter, formatter;
|
35
|
+
|
36
|
+
Globalize.locale( "en" );
|
37
|
+
formatter = Globalize.unitFormatter( "month", { form: "long" } );
|
38
|
+
|
39
|
+
formatter( 1 );
|
40
|
+
// > "1 month"
|
41
|
+
|
42
|
+
formatter( 3 );
|
43
|
+
// > "3 months"
|
44
|
+
|
45
|
+
formatter( 3000 );
|
46
|
+
// > "3,000 months"
|
47
|
+
```
|
48
|
+
|
49
|
+
You can pass a custom number formatter to format the number of units.
|
50
|
+
|
51
|
+
```javascript
|
52
|
+
var customNumberFormatter, formatter;
|
53
|
+
|
54
|
+
Globalize.locale( "en" );
|
55
|
+
customNumberFormatter = Globalize.numberFormatter({ useGrouping = false })
|
56
|
+
formatter = Globalize.unitFormatter( "mile-per-hour", {
|
57
|
+
form: "narrow", numberFormatter: customNumberFormatter
|
58
|
+
} );
|
59
|
+
|
60
|
+
formatter(5000)
|
61
|
+
// > "5000mph"
|
62
|
+
```
|
63
|
+
|
64
|
+
You can use the instance method `.unitFormatter()`, which uses the instance locale.
|
65
|
+
|
66
|
+
```javascript
|
67
|
+
var globalize = new Globalize( "en" ),
|
68
|
+
formatter = globalize.unitFormatter( "mile-per-hour", { form: "narrow" } );
|
69
|
+
|
70
|
+
formatter( 10 );
|
71
|
+
// > "10mph"
|
72
|
+
```
|