react-intl 4.2.2 → 4.5.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/CHANGELOG.md +28 -0
- package/README.md +1 -1
- package/dist/components/createFormattedComponent.js +3 -1
- package/dist/components/injectIntl.js +2 -3
- package/dist/components/message.d.ts +1 -1
- package/dist/error.d.ts +4 -2
- package/dist/error.js +2 -1
- package/dist/formatters/message.js +4 -4
- package/dist/formatters/relativeTime.js +1 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.js +4 -0
- package/dist/react-intl.api.md +42 -35
- package/dist/react-intl.d.ts +72 -59
- package/dist/react-intl.js +74 -31
- package/dist/react-intl.js.map +1 -1
- package/dist/react-intl.min.js +1 -1
- package/dist/react-intl.min.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/lib/components/createFormattedComponent.js +3 -1
- package/lib/components/injectIntl.js +2 -3
- package/lib/components/message.d.ts +1 -1
- package/lib/error.d.ts +4 -2
- package/lib/error.js +2 -1
- package/lib/formatters/message.js +4 -4
- package/lib/formatters/relativeTime.js +1 -4
- package/lib/index.d.ts +1 -0
- package/lib/index.js +3 -0
- package/lib/react-intl.d.ts +51 -47
- package/lib/tsdoc-metadata.json +1 -1
- package/lib/types.d.ts +2 -2
- package/package.json +32 -39
- package/src/components/createFormattedComponent.tsx +50 -48
- package/src/components/injectIntl.tsx +33 -34
- package/src/components/message.tsx +33 -32
- package/src/components/plural.tsx +22 -22
- package/src/components/provider.tsx +36 -36
- package/src/components/relative.tsx +70 -70
- package/src/components/useIntl.ts +7 -7
- package/src/error.ts +26 -13
- package/src/formatters/dateTime.ts +25 -33
- package/src/formatters/displayName.ts +11 -11
- package/src/formatters/list.ts +29 -29
- package/src/formatters/message.ts +47 -50
- package/src/formatters/number.ts +15 -15
- package/src/formatters/plural.ts +10 -10
- package/src/formatters/relativeTime.ts +18 -19
- package/src/index.ts +49 -36
- package/src/tsconfig.cjs.json +8 -8
- package/src/tsconfig.json +7 -7
- package/src/types.ts +59 -59
- package/src/utils.ts +29 -24
- package/src/vendor.d.ts +1 -1
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import {IntlConfig, Formatters, IntlFormatters} from '../types'
|
|
2
|
-
import {filterProps} from '../utils'
|
|
1
|
+
import {IntlConfig, Formatters, IntlFormatters} from '../types'
|
|
2
|
+
import {filterProps} from '../utils'
|
|
3
3
|
import {
|
|
4
4
|
DisplayNamesOptions,
|
|
5
5
|
DisplayNames as IntlDisplayNames,
|
|
6
|
-
} from '@formatjs/intl-displaynames'
|
|
7
|
-
import {FormatError, ErrorCode} from 'intl-messageformat'
|
|
8
|
-
import {ReactIntlErrorCode, ReactIntlError} from '../error'
|
|
6
|
+
} from '@formatjs/intl-displaynames'
|
|
7
|
+
import {FormatError, ErrorCode} from 'intl-messageformat'
|
|
8
|
+
import {ReactIntlErrorCode, ReactIntlError} from '../error'
|
|
9
9
|
|
|
10
10
|
const DISPLAY_NAMES_OPTONS: Array<keyof DisplayNamesOptions> = [
|
|
11
11
|
'localeMatcher',
|
|
12
12
|
'style',
|
|
13
13
|
'type',
|
|
14
14
|
'fallback',
|
|
15
|
-
]
|
|
15
|
+
]
|
|
16
16
|
|
|
17
17
|
export function formatDisplayName(
|
|
18
18
|
{locale, onError}: Pick<IntlConfig, 'locale' | 'onError'>,
|
|
@@ -20,7 +20,7 @@ export function formatDisplayName(
|
|
|
20
20
|
value: Parameters<IntlFormatters['formatDisplayName']>[0],
|
|
21
21
|
options: Parameters<IntlFormatters['formatDisplayName']>[1] = {}
|
|
22
22
|
): string | undefined {
|
|
23
|
-
const DisplayNames: typeof IntlDisplayNames = (Intl as any).DisplayNames
|
|
23
|
+
const DisplayNames: typeof IntlDisplayNames = (Intl as any).DisplayNames
|
|
24
24
|
if (!DisplayNames) {
|
|
25
25
|
onError(
|
|
26
26
|
new FormatError(
|
|
@@ -29,11 +29,11 @@ Try polyfilling it using "@formatjs/intl-displaynames"
|
|
|
29
29
|
`,
|
|
30
30
|
ErrorCode.MISSING_INTL_API
|
|
31
31
|
)
|
|
32
|
-
)
|
|
32
|
+
)
|
|
33
33
|
}
|
|
34
|
-
const filteredOptions = filterProps(options, DISPLAY_NAMES_OPTONS)
|
|
34
|
+
const filteredOptions = filterProps(options, DISPLAY_NAMES_OPTONS)
|
|
35
35
|
try {
|
|
36
|
-
return getDisplayNames(locale, filteredOptions).of(value)
|
|
36
|
+
return getDisplayNames(locale, filteredOptions).of(value)
|
|
37
37
|
} catch (e) {
|
|
38
38
|
onError(
|
|
39
39
|
new ReactIntlError(
|
|
@@ -41,6 +41,6 @@ Try polyfilling it using "@formatjs/intl-displaynames"
|
|
|
41
41
|
'Error formatting display name.',
|
|
42
42
|
e
|
|
43
43
|
)
|
|
44
|
-
)
|
|
44
|
+
)
|
|
45
45
|
}
|
|
46
46
|
}
|
package/src/formatters/list.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import * as React from 'react'
|
|
2
|
-
import {IntlConfig, Formatters, IntlFormatters} from '../types'
|
|
3
|
-
import {filterProps} from '../utils'
|
|
4
|
-
import IntlListFormat, {IntlListFormatOptions} from '@formatjs/intl-listformat'
|
|
5
|
-
import {FormatError, ErrorCode} from 'intl-messageformat'
|
|
6
|
-
import {ReactIntlError, ReactIntlErrorCode} from '../error'
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import {IntlConfig, Formatters, IntlFormatters} from '../types'
|
|
3
|
+
import {filterProps} from '../utils'
|
|
4
|
+
import IntlListFormat, {IntlListFormatOptions} from '@formatjs/intl-listformat'
|
|
5
|
+
import {FormatError, ErrorCode} from 'intl-messageformat'
|
|
6
|
+
import {ReactIntlError, ReactIntlErrorCode} from '../error'
|
|
7
7
|
|
|
8
8
|
const LIST_FORMAT_OPTIONS: Array<keyof IntlListFormatOptions> = [
|
|
9
9
|
'localeMatcher',
|
|
10
10
|
'type',
|
|
11
11
|
'style',
|
|
12
|
-
]
|
|
12
|
+
]
|
|
13
13
|
|
|
14
|
-
const now = Date.now()
|
|
14
|
+
const now = Date.now()
|
|
15
15
|
|
|
16
16
|
function generateToken(i: number): string {
|
|
17
|
-
return `${now}_${i}_${now}
|
|
17
|
+
return `${now}_${i}_${now}`
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
export function formatList(
|
|
@@ -22,14 +22,14 @@ export function formatList(
|
|
|
22
22
|
getListFormat: Formatters['getListFormat'],
|
|
23
23
|
values: Array<string>,
|
|
24
24
|
options: Parameters<IntlFormatters['formatList']>[1]
|
|
25
|
-
): string
|
|
25
|
+
): string
|
|
26
26
|
export function formatList(
|
|
27
27
|
{locale, onError}: Pick<IntlConfig, 'locale' | 'onError'>,
|
|
28
28
|
getListFormat: Formatters['getListFormat'],
|
|
29
29
|
values: Parameters<IntlFormatters['formatList']>[0],
|
|
30
30
|
options: Parameters<IntlFormatters['formatList']>[1] = {}
|
|
31
31
|
): React.ReactNode {
|
|
32
|
-
const ListFormat: typeof IntlListFormat = (Intl as any).ListFormat
|
|
32
|
+
const ListFormat: typeof IntlListFormat = (Intl as any).ListFormat
|
|
33
33
|
if (!ListFormat) {
|
|
34
34
|
onError(
|
|
35
35
|
new FormatError(
|
|
@@ -38,37 +38,37 @@ Try polyfilling it using "@formatjs/intl-listformat"
|
|
|
38
38
|
`,
|
|
39
39
|
ErrorCode.MISSING_INTL_API
|
|
40
40
|
)
|
|
41
|
-
)
|
|
41
|
+
)
|
|
42
42
|
}
|
|
43
|
-
const filteredOptions = filterProps(options, LIST_FORMAT_OPTIONS)
|
|
43
|
+
const filteredOptions = filterProps(options, LIST_FORMAT_OPTIONS)
|
|
44
44
|
|
|
45
45
|
try {
|
|
46
|
-
const richValues: Record<string, React.ReactNode> = {}
|
|
46
|
+
const richValues: Record<string, React.ReactNode> = {}
|
|
47
47
|
const serializedValues = values.map((v, i) => {
|
|
48
48
|
if (typeof v === 'object') {
|
|
49
|
-
const id = generateToken(i)
|
|
50
|
-
richValues[id] = v
|
|
51
|
-
return id
|
|
49
|
+
const id = generateToken(i)
|
|
50
|
+
richValues[id] = v
|
|
51
|
+
return id
|
|
52
52
|
}
|
|
53
|
-
return String(v)
|
|
54
|
-
})
|
|
53
|
+
return String(v)
|
|
54
|
+
})
|
|
55
55
|
if (!Object.keys(richValues).length) {
|
|
56
|
-
return getListFormat(locale, filteredOptions).format(serializedValues)
|
|
56
|
+
return getListFormat(locale, filteredOptions).format(serializedValues)
|
|
57
57
|
}
|
|
58
58
|
const parts = getListFormat(locale, filteredOptions).formatToParts(
|
|
59
59
|
serializedValues
|
|
60
|
-
)
|
|
60
|
+
)
|
|
61
61
|
return parts.reduce((all: Array<string | React.ReactNode>, el) => {
|
|
62
|
-
const val = el.value
|
|
62
|
+
const val = el.value
|
|
63
63
|
if (richValues[val]) {
|
|
64
|
-
all.push(richValues[val])
|
|
64
|
+
all.push(richValues[val])
|
|
65
65
|
} else if (typeof all[all.length - 1] === 'string') {
|
|
66
|
-
all[all.length - 1] += val
|
|
66
|
+
all[all.length - 1] += val
|
|
67
67
|
} else {
|
|
68
|
-
all.push(val)
|
|
68
|
+
all.push(val)
|
|
69
69
|
}
|
|
70
|
-
return all
|
|
71
|
-
}, [])
|
|
70
|
+
return all
|
|
71
|
+
}, [])
|
|
72
72
|
} catch (e) {
|
|
73
73
|
onError(
|
|
74
74
|
new ReactIntlError(
|
|
@@ -76,8 +76,8 @@ Try polyfilling it using "@formatjs/intl-listformat"
|
|
|
76
76
|
'Error formatting list.',
|
|
77
77
|
e
|
|
78
78
|
)
|
|
79
|
-
)
|
|
79
|
+
)
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
return values
|
|
82
|
+
return values
|
|
83
83
|
}
|
|
@@ -4,21 +4,21 @@
|
|
|
4
4
|
* See the accompanying LICENSE file for terms.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import * as React from 'react'
|
|
8
|
-
import {invariant} from '@formatjs/intl-utils'
|
|
7
|
+
import * as React from 'react'
|
|
8
|
+
import {invariant} from '@formatjs/intl-utils'
|
|
9
9
|
|
|
10
10
|
import {
|
|
11
11
|
Formatters,
|
|
12
12
|
IntlConfig,
|
|
13
13
|
MessageDescriptor,
|
|
14
14
|
CustomFormats,
|
|
15
|
-
} from '../types'
|
|
15
|
+
} from '../types'
|
|
16
16
|
|
|
17
17
|
import IntlMessageFormat, {
|
|
18
18
|
FormatXMLElementFn,
|
|
19
19
|
PrimitiveType,
|
|
20
|
-
} from 'intl-messageformat'
|
|
21
|
-
import {ReactIntlError, ReactIntlErrorCode} from '../error'
|
|
20
|
+
} from 'intl-messageformat'
|
|
21
|
+
import {ReactIntlError, ReactIntlErrorCode} from '../error'
|
|
22
22
|
|
|
23
23
|
function setTimeZoneInOptions(
|
|
24
24
|
opts: Record<string, Intl.DateTimeFormatOptions>,
|
|
@@ -29,25 +29,25 @@ function setTimeZoneInOptions(
|
|
|
29
29
|
all[k] = {
|
|
30
30
|
timeZone,
|
|
31
31
|
...opts[k],
|
|
32
|
-
}
|
|
33
|
-
return all
|
|
32
|
+
}
|
|
33
|
+
return all
|
|
34
34
|
},
|
|
35
35
|
{}
|
|
36
|
-
)
|
|
36
|
+
)
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
function deepMergeOptions(
|
|
40
40
|
opts1: Record<string, Intl.DateTimeFormatOptions>,
|
|
41
41
|
opts2: Record<string, Intl.DateTimeFormatOptions>
|
|
42
42
|
): Record<string, Intl.DateTimeFormatOptions> {
|
|
43
|
-
const keys = Object.keys({...opts1, ...opts2})
|
|
43
|
+
const keys = Object.keys({...opts1, ...opts2})
|
|
44
44
|
return keys.reduce((all: Record<string, Intl.DateTimeFormatOptions>, k) => {
|
|
45
45
|
all[k] = {
|
|
46
46
|
...(opts1[k] || {}),
|
|
47
47
|
...(opts2[k] || {}),
|
|
48
|
-
}
|
|
49
|
-
return all
|
|
50
|
-
}, {})
|
|
48
|
+
}
|
|
49
|
+
return all
|
|
50
|
+
}, {})
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
function deepMergeFormatsAndSetTimeZone(
|
|
@@ -55,9 +55,9 @@ function deepMergeFormatsAndSetTimeZone(
|
|
|
55
55
|
timeZone?: string
|
|
56
56
|
): CustomFormats {
|
|
57
57
|
if (!timeZone) {
|
|
58
|
-
return f1
|
|
58
|
+
return f1
|
|
59
59
|
}
|
|
60
|
-
const mfFormats = IntlMessageFormat.formats
|
|
60
|
+
const mfFormats = IntlMessageFormat.formats
|
|
61
61
|
return {
|
|
62
62
|
...mfFormats,
|
|
63
63
|
...f1,
|
|
@@ -69,13 +69,13 @@ function deepMergeFormatsAndSetTimeZone(
|
|
|
69
69
|
setTimeZoneInOptions(mfFormats.time, timeZone),
|
|
70
70
|
setTimeZoneInOptions(f1.time || {}, timeZone)
|
|
71
71
|
),
|
|
72
|
-
}
|
|
72
|
+
}
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
function prepareIntlMessageFormatHtmlOutput
|
|
76
|
-
chunks:
|
|
75
|
+
function prepareIntlMessageFormatHtmlOutput(
|
|
76
|
+
chunks: React.ReactNodeArray
|
|
77
77
|
): React.ReactElement {
|
|
78
|
-
return React.createElement(React.Fragment, null, ...chunks)
|
|
78
|
+
return React.createElement(React.Fragment, null, ...chunks)
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
export function formatMessage(
|
|
@@ -98,8 +98,8 @@ export function formatMessage(
|
|
|
98
98
|
state: Formatters,
|
|
99
99
|
messageDescriptor?: MessageDescriptor,
|
|
100
100
|
values?: Record<string, PrimitiveType>
|
|
101
|
-
): string
|
|
102
|
-
export function formatMessage
|
|
101
|
+
): string
|
|
102
|
+
export function formatMessage(
|
|
103
103
|
{
|
|
104
104
|
locale,
|
|
105
105
|
formats,
|
|
@@ -125,43 +125,39 @@ export function formatMessage<T>(
|
|
|
125
125
|
values:
|
|
126
126
|
| Record<
|
|
127
127
|
string,
|
|
128
|
-
|
|
|
129
|
-
| number
|
|
130
|
-
| boolean
|
|
128
|
+
| React.ReactNode
|
|
131
129
|
| Date
|
|
132
|
-
|
|
|
133
|
-
| FormatXMLElementFn<T>
|
|
134
|
-
| null
|
|
135
|
-
| undefined
|
|
130
|
+
| FormatXMLElementFn<React.ReactNode, React.ReactNode>
|
|
136
131
|
>
|
|
137
132
|
| undefined = {}
|
|
138
133
|
): React.ReactNode {
|
|
139
|
-
const {id, defaultMessage} = messageDescriptor
|
|
134
|
+
const {id, defaultMessage} = messageDescriptor
|
|
140
135
|
|
|
141
136
|
// `id` is a required field of a Message Descriptor.
|
|
142
|
-
invariant(!!id, '[React Intl] An `id` must be provided to format a message.')
|
|
143
|
-
const message = messages && messages[String(id)]
|
|
144
|
-
formats = deepMergeFormatsAndSetTimeZone(formats, timeZone)
|
|
145
|
-
defaultFormats = deepMergeFormatsAndSetTimeZone(defaultFormats, timeZone)
|
|
137
|
+
invariant(!!id, '[React Intl] An `id` must be provided to format a message.')
|
|
138
|
+
const message = messages && messages[String(id)]
|
|
139
|
+
formats = deepMergeFormatsAndSetTimeZone(formats, timeZone)
|
|
140
|
+
defaultFormats = deepMergeFormatsAndSetTimeZone(defaultFormats, timeZone)
|
|
146
141
|
|
|
147
|
-
let formattedMessageParts:
|
|
142
|
+
let formattedMessageParts: React.ReactNode = ''
|
|
148
143
|
|
|
149
144
|
if (message) {
|
|
150
145
|
try {
|
|
151
146
|
const formatter = state.getMessageFormat(message, locale, formats, {
|
|
152
147
|
formatters: state,
|
|
153
|
-
})
|
|
148
|
+
})
|
|
154
149
|
|
|
155
|
-
formattedMessageParts = formatter.format(values)
|
|
150
|
+
formattedMessageParts = formatter.format<React.ReactNode>(values)
|
|
156
151
|
} catch (e) {
|
|
157
152
|
onError(
|
|
158
153
|
new ReactIntlError(
|
|
159
154
|
ReactIntlErrorCode.FORMAT_ERROR,
|
|
160
155
|
`Error formatting message: "${id}" for locale: "${locale}"` +
|
|
161
156
|
(defaultMessage ? ', using default message as fallback.' : ''),
|
|
157
|
+
messageDescriptor,
|
|
162
158
|
e
|
|
163
159
|
)
|
|
164
|
-
)
|
|
160
|
+
)
|
|
165
161
|
}
|
|
166
162
|
} else if (
|
|
167
163
|
!defaultMessage ||
|
|
@@ -174,9 +170,10 @@ export function formatMessage<T>(
|
|
|
174
170
|
new ReactIntlError(
|
|
175
171
|
ReactIntlErrorCode.MISSING_TRANSLATION,
|
|
176
172
|
`Missing message: "${id}" for locale: "${locale}"` +
|
|
177
|
-
(defaultMessage ? ', using default message as fallback.' : '')
|
|
173
|
+
(defaultMessage ? ', using default message as fallback.' : ''),
|
|
174
|
+
messageDescriptor
|
|
178
175
|
)
|
|
179
|
-
)
|
|
176
|
+
)
|
|
180
177
|
}
|
|
181
178
|
|
|
182
179
|
if (!formattedMessageParts && defaultMessage) {
|
|
@@ -185,17 +182,18 @@ export function formatMessage<T>(
|
|
|
185
182
|
defaultMessage,
|
|
186
183
|
defaultLocale,
|
|
187
184
|
defaultFormats
|
|
188
|
-
)
|
|
185
|
+
)
|
|
189
186
|
|
|
190
|
-
formattedMessageParts = formatter.format(values)
|
|
187
|
+
formattedMessageParts = formatter.format(values)
|
|
191
188
|
} catch (e) {
|
|
192
189
|
onError(
|
|
193
190
|
new ReactIntlError(
|
|
194
191
|
ReactIntlErrorCode.FORMAT_ERROR,
|
|
195
192
|
`Error formatting the default message for: "${id}"`,
|
|
193
|
+
messageDescriptor,
|
|
196
194
|
e
|
|
197
195
|
)
|
|
198
|
-
)
|
|
196
|
+
)
|
|
199
197
|
}
|
|
200
198
|
}
|
|
201
199
|
|
|
@@ -206,21 +204,20 @@ export function formatMessage<T>(
|
|
|
206
204
|
`Cannot format message: "${id}", ` +
|
|
207
205
|
`using message ${
|
|
208
206
|
message || defaultMessage ? 'source' : 'id'
|
|
209
|
-
} as fallback
|
|
207
|
+
} as fallback.`,
|
|
208
|
+
messageDescriptor
|
|
210
209
|
)
|
|
211
|
-
)
|
|
210
|
+
)
|
|
212
211
|
if (typeof message === 'string') {
|
|
213
|
-
return message || defaultMessage || String(id)
|
|
212
|
+
return message || defaultMessage || String(id)
|
|
214
213
|
}
|
|
215
|
-
return defaultMessage || String(id)
|
|
214
|
+
return defaultMessage || String(id)
|
|
216
215
|
}
|
|
217
216
|
if (Array.isArray(formattedMessageParts)) {
|
|
218
217
|
if (wrapRichTextChunksInFragment) {
|
|
219
|
-
return prepareIntlMessageFormatHtmlOutput
|
|
220
|
-
formattedMessageParts as Array<string | T>
|
|
221
|
-
);
|
|
218
|
+
return prepareIntlMessageFormatHtmlOutput(formattedMessageParts)
|
|
222
219
|
}
|
|
223
|
-
return formattedMessageParts
|
|
220
|
+
return formattedMessageParts
|
|
224
221
|
}
|
|
225
|
-
return formattedMessageParts as
|
|
222
|
+
return formattedMessageParts as React.ReactNode
|
|
226
223
|
}
|
package/src/formatters/number.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {IntlConfig, Formatters, IntlFormatters} from '../types'
|
|
2
|
-
import {getNamedFormat, filterProps} from '../utils'
|
|
3
|
-
import {UnifiedNumberFormatOptions} from '@formatjs/intl-unified-numberformat'
|
|
4
|
-
import {ReactIntlError, ReactIntlErrorCode} from '../error'
|
|
1
|
+
import {IntlConfig, Formatters, IntlFormatters} from '../types'
|
|
2
|
+
import {getNamedFormat, filterProps} from '../utils'
|
|
3
|
+
import {UnifiedNumberFormatOptions} from '@formatjs/intl-unified-numberformat'
|
|
4
|
+
import {ReactIntlError, ReactIntlErrorCode} from '../error'
|
|
5
5
|
|
|
6
6
|
const NUMBER_FORMAT_OPTIONS: Array<keyof UnifiedNumberFormatOptions> = [
|
|
7
7
|
'localeMatcher',
|
|
@@ -27,7 +27,7 @@ const NUMBER_FORMAT_OPTIONS: Array<keyof UnifiedNumberFormatOptions> = [
|
|
|
27
27
|
'signDisplay',
|
|
28
28
|
'unit',
|
|
29
29
|
'unitDisplay',
|
|
30
|
-
]
|
|
30
|
+
]
|
|
31
31
|
|
|
32
32
|
export function getFormatter(
|
|
33
33
|
{
|
|
@@ -38,13 +38,13 @@ export function getFormatter(
|
|
|
38
38
|
getNumberFormat: Formatters['getNumberFormat'],
|
|
39
39
|
options: Parameters<IntlFormatters['formatNumber']>[1] = {}
|
|
40
40
|
): Intl.NumberFormat {
|
|
41
|
-
const {format} = options
|
|
41
|
+
const {format} = options
|
|
42
42
|
const defaults = ((format &&
|
|
43
43
|
getNamedFormat(formats!, 'number', format, onError)) ||
|
|
44
|
-
{}) as UnifiedNumberFormatOptions
|
|
45
|
-
const filteredOptions = filterProps(options, NUMBER_FORMAT_OPTIONS, defaults)
|
|
44
|
+
{}) as UnifiedNumberFormatOptions
|
|
45
|
+
const filteredOptions = filterProps(options, NUMBER_FORMAT_OPTIONS, defaults)
|
|
46
46
|
|
|
47
|
-
return getNumberFormat(locale, filteredOptions)
|
|
47
|
+
return getNumberFormat(locale, filteredOptions)
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
export function formatNumber(
|
|
@@ -54,7 +54,7 @@ export function formatNumber(
|
|
|
54
54
|
options: Parameters<IntlFormatters['formatNumber']>[1] = {}
|
|
55
55
|
): string {
|
|
56
56
|
try {
|
|
57
|
-
return getFormatter(config, getNumberFormat, options).format(value)
|
|
57
|
+
return getFormatter(config, getNumberFormat, options).format(value)
|
|
58
58
|
} catch (e) {
|
|
59
59
|
config.onError(
|
|
60
60
|
new ReactIntlError(
|
|
@@ -62,10 +62,10 @@ export function formatNumber(
|
|
|
62
62
|
'Error formatting number.',
|
|
63
63
|
e
|
|
64
64
|
)
|
|
65
|
-
)
|
|
65
|
+
)
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
return String(value)
|
|
68
|
+
return String(value)
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
export function formatNumberToParts(
|
|
@@ -75,7 +75,7 @@ export function formatNumberToParts(
|
|
|
75
75
|
options: Parameters<IntlFormatters['formatNumber']>[1] = {}
|
|
76
76
|
): Intl.NumberFormatPart[] {
|
|
77
77
|
try {
|
|
78
|
-
return getFormatter(config, getNumberFormat, options).formatToParts(value)
|
|
78
|
+
return getFormatter(config, getNumberFormat, options).formatToParts(value)
|
|
79
79
|
} catch (e) {
|
|
80
80
|
config.onError(
|
|
81
81
|
new ReactIntlError(
|
|
@@ -83,8 +83,8 @@ export function formatNumberToParts(
|
|
|
83
83
|
'Error formatting number.',
|
|
84
84
|
e
|
|
85
85
|
)
|
|
86
|
-
)
|
|
86
|
+
)
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
return []
|
|
89
|
+
return []
|
|
90
90
|
}
|
package/src/formatters/plural.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {IntlConfig, Formatters, IntlFormatters} from '../types'
|
|
2
|
-
import {filterProps} from '../utils'
|
|
3
|
-
import {ReactIntlErrorCode, ReactIntlError} from '../error'
|
|
4
|
-
import {FormatError, ErrorCode} from 'intl-messageformat'
|
|
1
|
+
import {IntlConfig, Formatters, IntlFormatters} from '../types'
|
|
2
|
+
import {filterProps} from '../utils'
|
|
3
|
+
import {ReactIntlErrorCode, ReactIntlError} from '../error'
|
|
4
|
+
import {FormatError, ErrorCode} from 'intl-messageformat'
|
|
5
5
|
|
|
6
6
|
const PLURAL_FORMAT_OPTIONS: Array<keyof Intl.PluralRulesOptions> = [
|
|
7
7
|
'localeMatcher',
|
|
8
8
|
'type',
|
|
9
|
-
]
|
|
9
|
+
]
|
|
10
10
|
|
|
11
11
|
export function formatPlural(
|
|
12
12
|
{locale, onError}: Pick<IntlConfig, 'locale' | 'onError'>,
|
|
@@ -22,12 +22,12 @@ Try polyfilling it using "@formatjs/intl-pluralrules"
|
|
|
22
22
|
`,
|
|
23
23
|
ErrorCode.MISSING_INTL_API
|
|
24
24
|
)
|
|
25
|
-
)
|
|
25
|
+
)
|
|
26
26
|
}
|
|
27
|
-
const filteredOptions = filterProps(options, PLURAL_FORMAT_OPTIONS)
|
|
27
|
+
const filteredOptions = filterProps(options, PLURAL_FORMAT_OPTIONS)
|
|
28
28
|
|
|
29
29
|
try {
|
|
30
|
-
return getPluralRules(locale, filteredOptions).select(value)
|
|
30
|
+
return getPluralRules(locale, filteredOptions).select(value)
|
|
31
31
|
} catch (e) {
|
|
32
32
|
onError(
|
|
33
33
|
new ReactIntlError(
|
|
@@ -35,8 +35,8 @@ Try polyfilling it using "@formatjs/intl-pluralrules"
|
|
|
35
35
|
'Error formatting plural.',
|
|
36
36
|
e
|
|
37
37
|
)
|
|
38
|
-
)
|
|
38
|
+
)
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
return 'other'
|
|
41
|
+
return 'other'
|
|
42
42
|
}
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import {IntlConfig, IntlFormatters, Formatters} from '../types'
|
|
1
|
+
import {IntlConfig, IntlFormatters, Formatters} from '../types'
|
|
2
2
|
|
|
3
|
-
import {getNamedFormat, filterProps} from '../utils'
|
|
3
|
+
import {getNamedFormat, filterProps} from '../utils'
|
|
4
4
|
import RelativeTimeFormat, {
|
|
5
5
|
IntlRelativeTimeFormatOptions,
|
|
6
|
-
} from '@formatjs/intl-relativetimeformat'
|
|
7
|
-
import {FormatError, ErrorCode} from 'intl-messageformat'
|
|
8
|
-
import {ReactIntlError, ReactIntlErrorCode} from '../error'
|
|
6
|
+
} from '@formatjs/intl-relativetimeformat'
|
|
7
|
+
import {FormatError, ErrorCode} from 'intl-messageformat'
|
|
8
|
+
import {ReactIntlError, ReactIntlErrorCode} from '../error'
|
|
9
9
|
|
|
10
|
-
const RELATIVE_TIME_FORMAT_OPTIONS: Array<
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
];
|
|
10
|
+
const RELATIVE_TIME_FORMAT_OPTIONS: Array<
|
|
11
|
+
keyof IntlRelativeTimeFormatOptions
|
|
12
|
+
> = ['numeric', 'style']
|
|
14
13
|
|
|
15
14
|
function getFormatter(
|
|
16
15
|
{
|
|
@@ -21,17 +20,17 @@ function getFormatter(
|
|
|
21
20
|
getRelativeTimeFormat: Formatters['getRelativeTimeFormat'],
|
|
22
21
|
options: Parameters<IntlFormatters['formatRelativeTime']>[2] = {}
|
|
23
22
|
): RelativeTimeFormat {
|
|
24
|
-
const {format} = options
|
|
23
|
+
const {format} = options
|
|
25
24
|
|
|
26
25
|
const defaults =
|
|
27
|
-
(!!format && getNamedFormat(formats, 'relative', format, onError)) || {}
|
|
26
|
+
(!!format && getNamedFormat(formats, 'relative', format, onError)) || {}
|
|
28
27
|
const filteredOptions = filterProps(
|
|
29
28
|
options,
|
|
30
29
|
RELATIVE_TIME_FORMAT_OPTIONS,
|
|
31
30
|
defaults as IntlRelativeTimeFormatOptions
|
|
32
|
-
)
|
|
31
|
+
)
|
|
33
32
|
|
|
34
|
-
return getRelativeTimeFormat(locale, filteredOptions)
|
|
33
|
+
return getRelativeTimeFormat(locale, filteredOptions)
|
|
35
34
|
}
|
|
36
35
|
|
|
37
36
|
export function formatRelativeTime(
|
|
@@ -42,9 +41,9 @@ export function formatRelativeTime(
|
|
|
42
41
|
options: Parameters<IntlFormatters['formatRelativeTime']>[2] = {}
|
|
43
42
|
): string {
|
|
44
43
|
if (!unit) {
|
|
45
|
-
unit = 'second'
|
|
44
|
+
unit = 'second'
|
|
46
45
|
}
|
|
47
|
-
const RelativeTimeFormat = (Intl as any).RelativeTimeFormat
|
|
46
|
+
const RelativeTimeFormat = (Intl as any).RelativeTimeFormat
|
|
48
47
|
if (!RelativeTimeFormat) {
|
|
49
48
|
config.onError(
|
|
50
49
|
new FormatError(
|
|
@@ -53,13 +52,13 @@ Try polyfilling it using "@formatjs/intl-relativetimeformat"
|
|
|
53
52
|
`,
|
|
54
53
|
ErrorCode.MISSING_INTL_API
|
|
55
54
|
)
|
|
56
|
-
)
|
|
55
|
+
)
|
|
57
56
|
}
|
|
58
57
|
try {
|
|
59
58
|
return getFormatter(config, getRelativeTimeFormat, options).format(
|
|
60
59
|
value,
|
|
61
60
|
unit
|
|
62
|
-
)
|
|
61
|
+
)
|
|
63
62
|
} catch (e) {
|
|
64
63
|
config.onError(
|
|
65
64
|
new ReactIntlError(
|
|
@@ -67,8 +66,8 @@ Try polyfilling it using "@formatjs/intl-relativetimeformat"
|
|
|
67
66
|
'Error formatting relative time.',
|
|
68
67
|
e
|
|
69
68
|
)
|
|
70
|
-
)
|
|
69
|
+
)
|
|
71
70
|
}
|
|
72
71
|
|
|
73
|
-
return String(value)
|
|
72
|
+
return String(value)
|
|
74
73
|
}
|