react-intl 6.0.5 → 6.0.6
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/BUILD +130 -0
- package/CHANGELOG.md +1384 -0
- package/LICENSE.md +0 -0
- package/README.md +0 -0
- package/example-sandboxes/rescripts/.rescriptsrc.js +28 -0
- package/example-sandboxes/rescripts/package-lock.json +18035 -0
- package/example-sandboxes/rescripts/package.json +35 -0
- package/example-sandboxes/rescripts/public/index.html +42 -0
- package/example-sandboxes/rescripts/src/App.tsx +16 -0
- package/example-sandboxes/rescripts/src/index.tsx +5 -0
- package/example-sandboxes/rescripts/src/react-app-env.d.ts +1 -0
- package/example-sandboxes/rescripts/src/styles.css +4 -0
- package/example-sandboxes/rescripts/tsconfig.json +20 -0
- package/example-sandboxes/strict-locale-type/.env +1 -0
- package/example-sandboxes/strict-locale-type/package-lock.json +14595 -0
- package/example-sandboxes/strict-locale-type/package.json +35 -0
- package/example-sandboxes/strict-locale-type/src/App.tsx +48 -0
- package/example-sandboxes/strict-locale-type/src/index.html +28 -0
- package/example-sandboxes/strict-locale-type/src/index.tsx +7 -0
- package/example-sandboxes/strict-locale-type/src/styles.css +4 -0
- package/example-sandboxes/strict-locale-type/tsconfig.json +7 -0
- package/example-sandboxes/strict-message-types/.env +1 -0
- package/example-sandboxes/strict-message-types/package-lock.json +14596 -0
- package/example-sandboxes/strict-message-types/package.json +35 -0
- package/example-sandboxes/strict-message-types/src/App.tsx +31 -0
- package/example-sandboxes/strict-message-types/src/index.html +28 -0
- package/example-sandboxes/strict-message-types/src/index.tsx +7 -0
- package/example-sandboxes/strict-message-types/src/styles.css +4 -0
- package/example-sandboxes/strict-message-types/tsconfig.json +7 -0
- package/examples/BUILD +70 -0
- package/examples/Bug2727.tsx +37 -0
- package/examples/HandleChange.tsx +48 -0
- package/examples/Hooks.tsx +126 -0
- package/examples/Injected.tsx +41 -0
- package/examples/Messages.tsx +82 -0
- package/examples/StaticTypeSafetyAndCodeSplitting/StaticTypeSafetyAndCodeSplitting.tsx +44 -0
- package/examples/StaticTypeSafetyAndCodeSplitting/en.json +3 -0
- package/examples/StaticTypeSafetyAndCodeSplitting/intlHelpers.tsx +39 -0
- package/examples/StaticTypeSafetyAndCodeSplitting/it.json +3 -0
- package/examples/TimeZone.tsx +44 -0
- package/examples/advanced/Advanced.tsx +68 -0
- package/examples/advanced/compiled-lang/en.json +77 -0
- package/examples/advanced/compiled-lang/fr.json +77 -0
- package/examples/index.html +20 -0
- package/examples/index.tsx +44 -0
- package/examples/package.json +20 -0
- package/index.ts +127 -0
- package/jest.config.js +27 -0
- package/package.json +12 -8
- package/src/components/createFormattedComponent.tsx +123 -0
- package/src/components/dateTimeRange.tsx +26 -0
- package/src/components/injectIntl.tsx +111 -0
- package/src/components/message.tsx +73 -0
- package/src/components/plural.tsx +45 -0
- package/src/components/provider.tsx +196 -0
- package/src/components/relative.tsx +192 -0
- package/src/components/useIntl.ts +10 -0
- package/src/types.ts +29 -0
- package/src/utils.ts +77 -0
- package/tests/functional/index.ts +18 -0
- package/tests/functional/support/build.ts +16 -0
- package/tests/functional/support/format.tsx +112 -0
- package/tests/perf/index.tsx +196 -0
- package/tests/setup.js +10 -0
- package/tests/unit/components/__snapshots__/displayName.tsx.snap +19 -0
- package/tests/unit/components/__snapshots__/message.tsx.snap +41 -0
- package/tests/unit/components/__snapshots__/relative.tsx.snap +11 -0
- package/tests/unit/components/__snapshots__/useIntl.tsx.snap +25 -0
- package/tests/unit/components/date.tsx +233 -0
- package/tests/unit/components/dateTimeRange.tsx +103 -0
- package/tests/unit/components/displayName.tsx +57 -0
- package/tests/unit/components/message.tsx +509 -0
- package/tests/unit/components/number.tsx +198 -0
- package/tests/unit/components/plural.tsx +116 -0
- package/tests/unit/components/provider.tsx +215 -0
- package/tests/unit/components/relative.tsx +263 -0
- package/tests/unit/components/time.tsx +242 -0
- package/tests/unit/components/useIntl.tsx +64 -0
- package/tests/unit/components/withIntl.tsx +66 -0
- package/tests/unit/react-intl.ts +88 -0
- package/tests/unit/testUtils.tsx +42 -0
- package/tsconfig.json +5 -0
- package/index.d.ts +0 -43
- package/index.d.ts.map +0 -1
- package/index.js +0 -51
- package/lib/index.d.ts +0 -43
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -26
- package/lib/src/components/createFormattedComponent.d.ts +0 -29
- package/lib/src/components/createFormattedComponent.d.ts.map +0 -1
- package/lib/src/components/createFormattedComponent.js +0 -62
- package/lib/src/components/dateTimeRange.d.ts +0 -11
- package/lib/src/components/dateTimeRange.d.ts.map +0 -1
- package/lib/src/components/dateTimeRange.js +0 -15
- package/lib/src/components/injectIntl.d.ts +0 -22
- package/lib/src/components/injectIntl.d.ts.map +0 -1
- package/lib/src/components/injectIntl.js +0 -29
- package/lib/src/components/message.d.ts +0 -12
- package/lib/src/components/message.d.ts.map +0 -1
- package/lib/src/components/message.js +0 -35
- package/lib/src/components/plural.d.ts +0 -15
- package/lib/src/components/plural.d.ts.map +0 -1
- package/lib/src/components/plural.js +0 -26
- package/lib/src/components/provider.d.ts +0 -35
- package/lib/src/components/provider.d.ts.map +0 -1
- package/lib/src/components/provider.js +0 -108
- package/lib/src/components/relative.d.ts +0 -12
- package/lib/src/components/relative.d.ts.map +0 -1
- package/lib/src/components/relative.js +0 -129
- package/lib/src/components/useIntl.d.ts +0 -3
- package/lib/src/components/useIntl.d.ts.map +0 -1
- package/lib/src/components/useIntl.js +0 -8
- package/lib/src/types.d.ts +0 -12
- package/lib/src/types.d.ts.map +0 -1
- package/lib/src/types.js +0 -1
- package/lib/src/utils.d.ts +0 -14
- package/lib/src/utils.d.ts.map +0 -1
- package/lib/src/utils.js +0 -43
- package/react-intl.iife.js +0 -7485
- package/src/components/createFormattedComponent.d.ts +0 -29
- package/src/components/createFormattedComponent.d.ts.map +0 -1
- package/src/components/createFormattedComponent.js +0 -69
- package/src/components/dateTimeRange.d.ts +0 -11
- package/src/components/dateTimeRange.d.ts.map +0 -1
- package/src/components/dateTimeRange.js +0 -17
- package/src/components/injectIntl.d.ts +0 -22
- package/src/components/injectIntl.d.ts.map +0 -1
- package/src/components/injectIntl.js +0 -33
- package/src/components/message.d.ts +0 -12
- package/src/components/message.d.ts.map +0 -1
- package/src/components/message.js +0 -37
- package/src/components/plural.d.ts +0 -15
- package/src/components/plural.d.ts.map +0 -1
- package/src/components/plural.js +0 -29
- package/src/components/provider.d.ts +0 -35
- package/src/components/provider.d.ts.map +0 -1
- package/src/components/provider.js +0 -112
- package/src/components/relative.d.ts +0 -12
- package/src/components/relative.d.ts.map +0 -1
- package/src/components/relative.js +0 -131
- package/src/components/useIntl.d.ts +0 -3
- package/src/components/useIntl.d.ts.map +0 -1
- package/src/components/useIntl.js +0 -12
- package/src/types.d.ts +0 -12
- package/src/types.d.ts.map +0 -1
- package/src/types.js +0 -2
- package/src/utils.d.ts +0 -14
- package/src/utils.d.ts.map +0 -1
- package/src/utils.js +0 -49
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import {IntlProvider, FormattedMessage} from 'react-intl'
|
|
3
|
+
|
|
4
|
+
type IntlProviderProps = React.ComponentProps<typeof IntlProvider>
|
|
5
|
+
|
|
6
|
+
function loadLocaleData(locale: string) {
|
|
7
|
+
switch (locale) {
|
|
8
|
+
case 'fr':
|
|
9
|
+
return import('./compiled-lang/fr.json')
|
|
10
|
+
default:
|
|
11
|
+
return import('./compiled-lang/en.json')
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface Props {
|
|
16
|
+
locale: IntlProviderProps['locale']
|
|
17
|
+
messages: IntlProviderProps['messages']
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const App: React.FC<Props> = props => {
|
|
21
|
+
return (
|
|
22
|
+
<IntlProvider
|
|
23
|
+
locale={props.locale}
|
|
24
|
+
defaultLocale="en"
|
|
25
|
+
messages={props.messages}
|
|
26
|
+
>
|
|
27
|
+
<p>
|
|
28
|
+
<span style={{fontSize: '30px'}}>AST</span>
|
|
29
|
+
<br />
|
|
30
|
+
<FormattedMessage id="simple" />
|
|
31
|
+
<br />
|
|
32
|
+
<FormattedMessage id="placeholder" values={{name: 'John'}} />
|
|
33
|
+
<br />
|
|
34
|
+
<FormattedMessage id="date" values={{ts: Date.now()}} />
|
|
35
|
+
<br />
|
|
36
|
+
<FormattedMessage id="time" values={{ts: Date.now()}} />
|
|
37
|
+
<br />
|
|
38
|
+
<FormattedMessage id="number" values={{num: Math.random() * 1000}} />
|
|
39
|
+
<br />
|
|
40
|
+
<FormattedMessage id="plural" values={{num: 1}} />
|
|
41
|
+
<br />
|
|
42
|
+
<FormattedMessage id="plural" values={{num: 99}} />
|
|
43
|
+
<br />
|
|
44
|
+
<FormattedMessage id="select" values={{gender: 'male'}} />
|
|
45
|
+
<br />
|
|
46
|
+
<FormattedMessage id="select" values={{gender: 'female'}} />
|
|
47
|
+
<br />
|
|
48
|
+
<FormattedMessage id="selectordinal" values={{order: 1}} />
|
|
49
|
+
<br />
|
|
50
|
+
<FormattedMessage id="selectordinal" values={{order: 2}} />
|
|
51
|
+
<br />
|
|
52
|
+
<FormattedMessage id="selectordinal" values={{order: 3}} />
|
|
53
|
+
<br />
|
|
54
|
+
<FormattedMessage id="selectordinal" values={{order: 4}} />
|
|
55
|
+
<br />
|
|
56
|
+
<FormattedMessage
|
|
57
|
+
id="richtext"
|
|
58
|
+
values={{num: 99, bold: (chunks: any) => <strong>{chunks}</strong>}}
|
|
59
|
+
/>
|
|
60
|
+
</p>
|
|
61
|
+
</IntlProvider>
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function bootstrapApplication(locale: string) {
|
|
66
|
+
const messages = await loadLocaleData(locale)
|
|
67
|
+
return <App locale={locale} messages={messages.default} />
|
|
68
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"simple": [{"type": 0, "value": "Hello world"}],
|
|
3
|
+
"placeholder": [
|
|
4
|
+
{"type": 0, "value": "Hello "},
|
|
5
|
+
{"type": 1, "value": "name"}
|
|
6
|
+
],
|
|
7
|
+
"date": [
|
|
8
|
+
{"type": 0, "value": "Hello "},
|
|
9
|
+
{"type": 3, "style": null, "value": "ts"}
|
|
10
|
+
],
|
|
11
|
+
"time": [
|
|
12
|
+
{"type": 0, "value": "Hello "},
|
|
13
|
+
{"type": 4, "style": null, "value": "ts"}
|
|
14
|
+
],
|
|
15
|
+
"number": [
|
|
16
|
+
{"type": 0, "value": "Hello "},
|
|
17
|
+
{"type": 2, "style": null, "value": "num"}
|
|
18
|
+
],
|
|
19
|
+
"plural": [
|
|
20
|
+
{"type": 0, "value": "I have "},
|
|
21
|
+
{
|
|
22
|
+
"type": 6,
|
|
23
|
+
"pluralType": "cardinal",
|
|
24
|
+
"value": "num",
|
|
25
|
+
"offset": 0,
|
|
26
|
+
"options": {
|
|
27
|
+
"one": {"value": [{"type": 7}, {"type": 0, "value": " dog"}]},
|
|
28
|
+
"other": {"value": [{"type": 7}, {"type": 0, "value": " dogs"}]}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
],
|
|
32
|
+
"select": [
|
|
33
|
+
{"type": 0, "value": "I am a "},
|
|
34
|
+
{
|
|
35
|
+
"type": 5,
|
|
36
|
+
"value": "gender",
|
|
37
|
+
"options": {
|
|
38
|
+
"male": {"value": [{"type": 0, "value": "boy"}]},
|
|
39
|
+
"female": {"value": [{"type": 0, "value": "girl"}]}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
],
|
|
43
|
+
"selectordinal": [
|
|
44
|
+
{"type": 0, "value": "I am the "},
|
|
45
|
+
{
|
|
46
|
+
"type": 6,
|
|
47
|
+
"pluralType": "ordinal",
|
|
48
|
+
"value": "order",
|
|
49
|
+
"offset": 0,
|
|
50
|
+
"options": {
|
|
51
|
+
"one": {"value": [{"type": 7}, {"type": 0, "value": "st person"}]},
|
|
52
|
+
"two": {"value": [{"type": 7}, {"type": 0, "value": "nd person"}]},
|
|
53
|
+
"=3": {"value": [{"type": 7}, {"type": 0, "value": "rd person"}]},
|
|
54
|
+
"other": {"value": [{"type": 7}, {"type": 0, "value": "th person"}]}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
],
|
|
58
|
+
"richtext": [
|
|
59
|
+
{"type": 0, "value": "I have "},
|
|
60
|
+
{
|
|
61
|
+
"type": 8,
|
|
62
|
+
"value": "bold",
|
|
63
|
+
"children": [
|
|
64
|
+
{
|
|
65
|
+
"type": 6,
|
|
66
|
+
"pluralType": "cardinal",
|
|
67
|
+
"value": "num",
|
|
68
|
+
"offset": 0,
|
|
69
|
+
"options": {
|
|
70
|
+
"one": {"value": [{"type": 7}, {"type": 0, "value": " dog"}]},
|
|
71
|
+
"other": {"value": [{"type": 7}, {"type": 0, "value": " dogs"}]}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
]
|
|
75
|
+
}
|
|
76
|
+
]
|
|
77
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"simple": [{"type": 0, "value": "Hello world"}],
|
|
3
|
+
"placeholder": [
|
|
4
|
+
{"type": 0, "value": "Hello "},
|
|
5
|
+
{"type": 1, "value": "name"}
|
|
6
|
+
],
|
|
7
|
+
"date": [
|
|
8
|
+
{"type": 0, "value": "Hello "},
|
|
9
|
+
{"type": 3, "style": null, "value": "ts"}
|
|
10
|
+
],
|
|
11
|
+
"time": [
|
|
12
|
+
{"type": 0, "value": "Hello "},
|
|
13
|
+
{"type": 4, "style": null, "value": "ts"}
|
|
14
|
+
],
|
|
15
|
+
"number": [
|
|
16
|
+
{"type": 0, "value": "Hello "},
|
|
17
|
+
{"type": 2, "style": null, "value": "num"}
|
|
18
|
+
],
|
|
19
|
+
"plural": [
|
|
20
|
+
{"type": 0, "value": "I have "},
|
|
21
|
+
{
|
|
22
|
+
"type": 6,
|
|
23
|
+
"pluralType": "cardinal",
|
|
24
|
+
"value": "num",
|
|
25
|
+
"offset": 0,
|
|
26
|
+
"options": {
|
|
27
|
+
"one": {"value": [{"type": 7}, {"type": 0, "value": " dog"}]},
|
|
28
|
+
"other": {"value": [{"type": 7}, {"type": 0, "value": " dogs"}]}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
],
|
|
32
|
+
"select": [
|
|
33
|
+
{"type": 0, "value": "I am a "},
|
|
34
|
+
{
|
|
35
|
+
"type": 5,
|
|
36
|
+
"value": "gender",
|
|
37
|
+
"options": {
|
|
38
|
+
"male": {"value": [{"type": 0, "value": "boy"}]},
|
|
39
|
+
"female": {"value": [{"type": 0, "value": "girl"}]}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
],
|
|
43
|
+
"selectordinal": [
|
|
44
|
+
{"type": 0, "value": "I am the "},
|
|
45
|
+
{
|
|
46
|
+
"type": 6,
|
|
47
|
+
"pluralType": "ordinal",
|
|
48
|
+
"value": "order",
|
|
49
|
+
"offset": 0,
|
|
50
|
+
"options": {
|
|
51
|
+
"one": {"value": [{"type": 7}, {"type": 0, "value": "st person"}]},
|
|
52
|
+
"two": {"value": [{"type": 7}, {"type": 0, "value": "nd person"}]},
|
|
53
|
+
"=3": {"value": [{"type": 7}, {"type": 0, "value": "rd person"}]},
|
|
54
|
+
"other": {"value": [{"type": 7}, {"type": 0, "value": "th person"}]}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
],
|
|
58
|
+
"richtext": [
|
|
59
|
+
{"type": 0, "value": "I have "},
|
|
60
|
+
{
|
|
61
|
+
"type": 8,
|
|
62
|
+
"value": "bold",
|
|
63
|
+
"children": [
|
|
64
|
+
{
|
|
65
|
+
"type": 6,
|
|
66
|
+
"pluralType": "cardinal",
|
|
67
|
+
"value": "num",
|
|
68
|
+
"offset": 0,
|
|
69
|
+
"options": {
|
|
70
|
+
"one": {"value": [{"type": 7}, {"type": 0, "value": " dog"}]},
|
|
71
|
+
"other": {"value": [{"type": 7}, {"type": 0, "value": " dogs"}]}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
]
|
|
75
|
+
}
|
|
76
|
+
]
|
|
77
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
6
|
+
<title>React Intl Explicit Example</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<div id="timezone"></div>
|
|
10
|
+
<div id="messages"></div>
|
|
11
|
+
<div id="hooks"></div>
|
|
12
|
+
<div id="advanced"></div>
|
|
13
|
+
<div id="injected"></div>
|
|
14
|
+
<div id="handlechange"></div>
|
|
15
|
+
<div id="static-type-safety-and-code-splitting"></div>
|
|
16
|
+
<div id="bug2727"></div>
|
|
17
|
+
<!-- Name of the bazel esbuild target -->
|
|
18
|
+
<script src="./main.js"></script>
|
|
19
|
+
</body>
|
|
20
|
+
</html>
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import 'core-js/stable'
|
|
2
|
+
import '@formatjs/intl-pluralrules/polyfill'
|
|
3
|
+
import * as React from 'react'
|
|
4
|
+
import * as ReactDOM from 'react-dom'
|
|
5
|
+
import Timezone from './TimeZone'
|
|
6
|
+
import Messages from './Messages'
|
|
7
|
+
import Hooks from './Hooks'
|
|
8
|
+
import {bootstrapApplication} from './advanced/Advanced'
|
|
9
|
+
import Injected from './Injected'
|
|
10
|
+
import HandleChange from './HandleChange'
|
|
11
|
+
import StaticTypeSafetyAndCodeSplitting from './StaticTypeSafetyAndCodeSplitting/StaticTypeSafetyAndCodeSplitting'
|
|
12
|
+
import {App as Bug2727} from './Bug2727'
|
|
13
|
+
//polyfills
|
|
14
|
+
import '@formatjs/intl-getcanonicallocales/polyfill'
|
|
15
|
+
import '@formatjs/intl-locale/polyfill'
|
|
16
|
+
|
|
17
|
+
import '@formatjs/intl-pluralrules/polyfill'
|
|
18
|
+
import '@formatjs/intl-pluralrules/locale-data/en' // locale-data for en
|
|
19
|
+
|
|
20
|
+
import '@formatjs/intl-numberformat/polyfill'
|
|
21
|
+
import '@formatjs/intl-numberformat/locale-data/en' // locale-data for en
|
|
22
|
+
|
|
23
|
+
import '@formatjs/intl-datetimeformat/polyfill'
|
|
24
|
+
import '@formatjs/intl-datetimeformat/locale-data/en' // locale-data for en
|
|
25
|
+
import '@formatjs/intl-datetimeformat/add-all-tz' // Add ALL tz data
|
|
26
|
+
|
|
27
|
+
ReactDOM.render(<Timezone />, document.getElementById('timezone'))
|
|
28
|
+
|
|
29
|
+
ReactDOM.render(<Messages />, document.getElementById('messages'))
|
|
30
|
+
ReactDOM.render(<Hooks />, document.getElementById('hooks'))
|
|
31
|
+
|
|
32
|
+
ReactDOM.render(<Injected />, document.getElementById('injected'))
|
|
33
|
+
ReactDOM.render(<HandleChange />, document.getElementById('handlechange'))
|
|
34
|
+
|
|
35
|
+
ReactDOM.render(
|
|
36
|
+
<StaticTypeSafetyAndCodeSplitting />,
|
|
37
|
+
document.getElementById('static-type-safety-and-code-splitting')
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
ReactDOM.render(<Bug2727 />, document.getElementById('bug2727'))
|
|
41
|
+
|
|
42
|
+
bootstrapApplication('en').then(app => {
|
|
43
|
+
ReactDOM.render(app, document.getElementById('advanced'))
|
|
44
|
+
})
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"devDependencies": {
|
|
3
|
+
"@formatjs/intl-datetimeformat": "workspace:*",
|
|
4
|
+
"@formatjs/intl-getcanonicallocales": "workspace:*",
|
|
5
|
+
"@formatjs/intl-locale": "workspace:*",
|
|
6
|
+
"@formatjs/intl-numberformat": "workspace:*",
|
|
7
|
+
"@formatjs/intl-pluralrules": "workspace:*",
|
|
8
|
+
"@formatjs/ecma402-abstract": "workspace:*",
|
|
9
|
+
"@formatjs/icu-messageformat-parser": "workspace:*",
|
|
10
|
+
"@formatjs/intl": "workspace:*",
|
|
11
|
+
"@formatjs/intl-displaynames": "workspace:*",
|
|
12
|
+
"@formatjs/intl-listformat": "workspace:*",
|
|
13
|
+
"@types/hoist-non-react-statics": "^3.3.1",
|
|
14
|
+
"@types/react": "16 || 17 || 18",
|
|
15
|
+
"hoist-non-react-statics": "^3.3.2",
|
|
16
|
+
"intl-messageformat": "workspace:*",
|
|
17
|
+
"react-intl": "workspace:*",
|
|
18
|
+
"tslib": "2.4.0"
|
|
19
|
+
}
|
|
20
|
+
}
|
package/index.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2015, Yahoo Inc.
|
|
3
|
+
* Copyrights licensed under the New BSD License.
|
|
4
|
+
* See the accompanying LICENSE file for terms.
|
|
5
|
+
*/
|
|
6
|
+
import * as React from 'react'
|
|
7
|
+
import {
|
|
8
|
+
createFormattedComponent,
|
|
9
|
+
createFormattedDateTimePartsComponent,
|
|
10
|
+
} from './src/components/createFormattedComponent'
|
|
11
|
+
import {
|
|
12
|
+
MessageDescriptor,
|
|
13
|
+
CustomFormatConfig,
|
|
14
|
+
FormatDateOptions,
|
|
15
|
+
} from '@formatjs/intl'
|
|
16
|
+
import {IntlListFormatOptions} from '@formatjs/intl-listformat'
|
|
17
|
+
import {DisplayNamesOptions} from '@formatjs/intl-displaynames'
|
|
18
|
+
import {NumberFormatOptions} from '@formatjs/ecma402-abstract'
|
|
19
|
+
import injectIntl, {
|
|
20
|
+
Provider as RawIntlProvider,
|
|
21
|
+
Context as IntlContext,
|
|
22
|
+
WithIntlProps,
|
|
23
|
+
WrappedComponentProps,
|
|
24
|
+
} from './src/components/injectIntl'
|
|
25
|
+
import useIntl from './src/components/useIntl'
|
|
26
|
+
import IntlProvider, {createIntl} from './src/components/provider'
|
|
27
|
+
import FormattedRelativeTime from './src/components/relative'
|
|
28
|
+
import FormattedPlural from './src/components/plural'
|
|
29
|
+
import FormattedMessage from './src/components/message'
|
|
30
|
+
import FormattedDateTimeRange from './src/components/dateTimeRange'
|
|
31
|
+
export {
|
|
32
|
+
FormattedDateTimeRange,
|
|
33
|
+
FormattedMessage,
|
|
34
|
+
FormattedPlural,
|
|
35
|
+
FormattedRelativeTime,
|
|
36
|
+
IntlContext,
|
|
37
|
+
IntlProvider,
|
|
38
|
+
RawIntlProvider,
|
|
39
|
+
WithIntlProps,
|
|
40
|
+
WrappedComponentProps,
|
|
41
|
+
createIntl,
|
|
42
|
+
injectIntl,
|
|
43
|
+
useIntl,
|
|
44
|
+
}
|
|
45
|
+
export {IntlConfig, ResolvedIntlConfig, IntlShape} from './src/types'
|
|
46
|
+
export {
|
|
47
|
+
createIntlCache,
|
|
48
|
+
MessageDescriptor,
|
|
49
|
+
IntlCache,
|
|
50
|
+
Formatters,
|
|
51
|
+
IntlFormatters,
|
|
52
|
+
FormatDisplayNameOptions,
|
|
53
|
+
FormatListOptions,
|
|
54
|
+
FormatPluralOptions,
|
|
55
|
+
FormatRelativeTimeOptions,
|
|
56
|
+
FormatNumberOptions,
|
|
57
|
+
FormatDateOptions,
|
|
58
|
+
CustomFormatConfig,
|
|
59
|
+
CustomFormats,
|
|
60
|
+
UnsupportedFormatterError,
|
|
61
|
+
InvalidConfigError,
|
|
62
|
+
MissingDataError,
|
|
63
|
+
MessageFormatError,
|
|
64
|
+
MissingTranslationError,
|
|
65
|
+
IntlErrorCode as ReactIntlErrorCode,
|
|
66
|
+
IntlError as ReactIntlError,
|
|
67
|
+
} from '@formatjs/intl'
|
|
68
|
+
|
|
69
|
+
export function defineMessages<
|
|
70
|
+
K extends keyof any,
|
|
71
|
+
T = MessageDescriptor,
|
|
72
|
+
U extends Record<K, T> = Record<K, T>
|
|
73
|
+
>(msgs: U): U {
|
|
74
|
+
return msgs
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function defineMessage<T extends MessageDescriptor>(msg: T): T {
|
|
78
|
+
return msg
|
|
79
|
+
}
|
|
80
|
+
// IMPORTANT: Explicit here to prevent api-extractor from outputing `import('./src/types').CustomFormatConfig`
|
|
81
|
+
export const FormattedDate: React.FC<
|
|
82
|
+
Intl.DateTimeFormatOptions &
|
|
83
|
+
CustomFormatConfig & {
|
|
84
|
+
value: string | number | Date | undefined
|
|
85
|
+
}
|
|
86
|
+
> = createFormattedComponent('formatDate')
|
|
87
|
+
export const FormattedTime: React.FC<
|
|
88
|
+
Intl.DateTimeFormatOptions &
|
|
89
|
+
CustomFormatConfig & {
|
|
90
|
+
value: string | number | Date | undefined
|
|
91
|
+
}
|
|
92
|
+
> = createFormattedComponent('formatTime')
|
|
93
|
+
// @ts-ignore issue w/ TS Intl types
|
|
94
|
+
export const FormattedNumber: React.FC<
|
|
95
|
+
NumberFormatOptions &
|
|
96
|
+
CustomFormatConfig & {
|
|
97
|
+
value: number | bigint
|
|
98
|
+
}
|
|
99
|
+
> = createFormattedComponent('formatNumber')
|
|
100
|
+
export const FormattedList: React.FC<
|
|
101
|
+
IntlListFormatOptions & {
|
|
102
|
+
value: readonly React.ReactNode[]
|
|
103
|
+
}
|
|
104
|
+
> = createFormattedComponent('formatList')
|
|
105
|
+
export const FormattedDisplayName: React.FC<
|
|
106
|
+
DisplayNamesOptions & {
|
|
107
|
+
value: string | number | Record<string, unknown>
|
|
108
|
+
}
|
|
109
|
+
> = createFormattedComponent('formatDisplayName')
|
|
110
|
+
export const FormattedDateParts: React.FC<
|
|
111
|
+
FormatDateOptions & {
|
|
112
|
+
value: Parameters<Intl.DateTimeFormat['format']>[0] | string
|
|
113
|
+
children(val: Intl.DateTimeFormatPart[]): React.ReactElement | null
|
|
114
|
+
}
|
|
115
|
+
> = createFormattedDateTimePartsComponent('formatDate')
|
|
116
|
+
export const FormattedTimeParts: React.FC<
|
|
117
|
+
FormatDateOptions & {
|
|
118
|
+
value: Parameters<Intl.DateTimeFormat['format']>[0] | string
|
|
119
|
+
children(val: Intl.DateTimeFormatPart[]): React.ReactElement | null
|
|
120
|
+
}
|
|
121
|
+
> = createFormattedDateTimePartsComponent('formatTime')
|
|
122
|
+
|
|
123
|
+
export {
|
|
124
|
+
FormattedNumberParts,
|
|
125
|
+
FormattedListParts,
|
|
126
|
+
} from './src/components/createFormattedComponent'
|
|
127
|
+
export type {MessageFormatElement} from '@formatjs/icu-messageformat-parser'
|
package/jest.config.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
preset: 'ts-jest',
|
|
3
|
+
globals: {
|
|
4
|
+
'ts-jest': {
|
|
5
|
+
tsconfig: 'tsconfig.json',
|
|
6
|
+
diagnostics: false,
|
|
7
|
+
},
|
|
8
|
+
},
|
|
9
|
+
testEnvironment: 'jsdom',
|
|
10
|
+
testRegex: ['/tests/(functional|unit)/.*\\.(ts|tsx)'],
|
|
11
|
+
testPathIgnorePatterns: [
|
|
12
|
+
'tests/functional/support',
|
|
13
|
+
'/tests/unit/testUtils',
|
|
14
|
+
'__snapshots__',
|
|
15
|
+
],
|
|
16
|
+
collectCoverageFrom: ['src/**/*.{ts,tsx}'],
|
|
17
|
+
coverageReporters: ['lcov', 'text', 'text-summary', 'html'],
|
|
18
|
+
setupFilesAfterEnv: ['<rootDir>/tests/setup.js'],
|
|
19
|
+
coverageThreshold: {
|
|
20
|
+
global: {
|
|
21
|
+
branches: 85,
|
|
22
|
+
functions: 100,
|
|
23
|
+
lines: 95,
|
|
24
|
+
statements: 95,
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-intl",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.6",
|
|
4
4
|
"description": "Internationalize React apps. This library provides React components and an API to format dates, numbers, and strings, including pluralization and handling translations.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"intl",
|
|
@@ -128,17 +128,21 @@
|
|
|
128
128
|
"types": "index.d.ts",
|
|
129
129
|
"sideEffects": false,
|
|
130
130
|
"dependencies": {
|
|
131
|
-
"@formatjs/ecma402-abstract": "1.11.
|
|
132
|
-
"@formatjs/icu-messageformat-parser": "2.1.
|
|
133
|
-
"@formatjs/intl": "2.3.
|
|
134
|
-
"@formatjs/intl-displaynames": "6.0
|
|
135
|
-
"@formatjs/intl-listformat": "7.0
|
|
131
|
+
"@formatjs/ecma402-abstract": "1.11.9",
|
|
132
|
+
"@formatjs/icu-messageformat-parser": "2.1.5",
|
|
133
|
+
"@formatjs/intl": "2.3.2",
|
|
134
|
+
"@formatjs/intl-displaynames": "6.1.0",
|
|
135
|
+
"@formatjs/intl-listformat": "7.1.0",
|
|
136
136
|
"@types/hoist-non-react-statics": "^3.3.1",
|
|
137
137
|
"@types/react": "16 || 17 || 18",
|
|
138
138
|
"hoist-non-react-statics": "^3.3.2",
|
|
139
|
-
"intl-messageformat": "10.1.
|
|
139
|
+
"intl-messageformat": "10.1.2",
|
|
140
140
|
"tslib": "2.4.0"
|
|
141
141
|
},
|
|
142
|
+
"devDependencies": {
|
|
143
|
+
"@formatjs/intl-numberformat": "8.1.0",
|
|
144
|
+
"@formatjs/intl-relativetimeformat": "11.1.0"
|
|
145
|
+
},
|
|
142
146
|
"peerDependencies": {
|
|
143
147
|
"react": "^16.6.0 || 17 || 18",
|
|
144
148
|
"typescript": "^4.5"
|
|
@@ -152,4 +156,4 @@
|
|
|
152
156
|
"ie 11"
|
|
153
157
|
],
|
|
154
158
|
"gitHead": "773d6ebf881357f6e4c2dd7e8984b1bd0f69b4ca"
|
|
155
|
-
}
|
|
159
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import {
|
|
3
|
+
FormatDateOptions,
|
|
4
|
+
FormatNumberOptions,
|
|
5
|
+
FormatListOptions,
|
|
6
|
+
FormatDisplayNameOptions,
|
|
7
|
+
} from '@formatjs/intl'
|
|
8
|
+
import {IntlShape} from '../types'
|
|
9
|
+
import useIntl from './useIntl'
|
|
10
|
+
import {Part} from '@formatjs/intl-listformat'
|
|
11
|
+
|
|
12
|
+
enum DisplayName {
|
|
13
|
+
formatDate = 'FormattedDate',
|
|
14
|
+
formatTime = 'FormattedTime',
|
|
15
|
+
formatNumber = 'FormattedNumber',
|
|
16
|
+
formatList = 'FormattedList',
|
|
17
|
+
// Note that this DisplayName is the locale display name, not to be confused with
|
|
18
|
+
// the name of the enum, which is for React component display name in dev tools.
|
|
19
|
+
formatDisplayName = 'FormattedDisplayName',
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
enum DisplayNameParts {
|
|
23
|
+
formatDate = 'FormattedDateParts',
|
|
24
|
+
formatTime = 'FormattedTimeParts',
|
|
25
|
+
formatNumber = 'FormattedNumberParts',
|
|
26
|
+
formatList = 'FormattedListParts',
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type Formatter = {
|
|
30
|
+
formatDate: FormatDateOptions
|
|
31
|
+
formatTime: FormatDateOptions
|
|
32
|
+
formatNumber: FormatNumberOptions
|
|
33
|
+
formatList: FormatListOptions
|
|
34
|
+
formatDisplayName: FormatDisplayNameOptions
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const FormattedNumberParts: React.FC<
|
|
38
|
+
Formatter['formatNumber'] & {
|
|
39
|
+
value: Parameters<IntlShape['formatNumber']>[0]
|
|
40
|
+
|
|
41
|
+
children(val: Intl.NumberFormatPart[]): React.ReactElement | null
|
|
42
|
+
}
|
|
43
|
+
> = props => {
|
|
44
|
+
const intl = useIntl()
|
|
45
|
+
const {value, children, ...formatProps} = props
|
|
46
|
+
return children(intl.formatNumberToParts(value, formatProps))
|
|
47
|
+
}
|
|
48
|
+
FormattedNumberParts.displayName = 'FormattedNumberParts'
|
|
49
|
+
|
|
50
|
+
export const FormattedListParts: React.FC<
|
|
51
|
+
Formatter['formatList'] & {
|
|
52
|
+
value: Parameters<IntlShape['formatList']>[0]
|
|
53
|
+
|
|
54
|
+
children(val: Part[]): React.ReactElement | null
|
|
55
|
+
}
|
|
56
|
+
> = props => {
|
|
57
|
+
const intl = useIntl()
|
|
58
|
+
const {value, children, ...formatProps} = props
|
|
59
|
+
return children(intl.formatListToParts(value, formatProps))
|
|
60
|
+
}
|
|
61
|
+
FormattedNumberParts.displayName = 'FormattedNumberParts'
|
|
62
|
+
|
|
63
|
+
export function createFormattedDateTimePartsComponent<
|
|
64
|
+
Name extends 'formatDate' | 'formatTime'
|
|
65
|
+
>(
|
|
66
|
+
name: Name
|
|
67
|
+
): React.FC<
|
|
68
|
+
Formatter[Name] & {
|
|
69
|
+
value: Parameters<IntlShape[Name]>[0]
|
|
70
|
+
children(val: Intl.DateTimeFormatPart[]): React.ReactElement | null
|
|
71
|
+
}
|
|
72
|
+
> {
|
|
73
|
+
type FormatFn = IntlShape[Name]
|
|
74
|
+
type Props = Formatter[Name] & {
|
|
75
|
+
value: Parameters<FormatFn>[0]
|
|
76
|
+
children(val: Intl.DateTimeFormatPart[]): React.ReactElement | null
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const ComponentParts: React.FC<Props> = props => {
|
|
80
|
+
const intl = useIntl()
|
|
81
|
+
const {value, children, ...formatProps} = props
|
|
82
|
+
const date = typeof value === 'string' ? new Date(value || 0) : value
|
|
83
|
+
const formattedParts: Intl.DateTimeFormatPart[] =
|
|
84
|
+
name === 'formatDate'
|
|
85
|
+
? intl.formatDateToParts(date, formatProps)
|
|
86
|
+
: intl.formatTimeToParts(date, formatProps)
|
|
87
|
+
|
|
88
|
+
return children(formattedParts)
|
|
89
|
+
}
|
|
90
|
+
ComponentParts.displayName = DisplayNameParts[name]
|
|
91
|
+
return ComponentParts
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function createFormattedComponent<Name extends keyof Formatter>(
|
|
95
|
+
name: Name
|
|
96
|
+
): React.FC<
|
|
97
|
+
Formatter[Name] & {
|
|
98
|
+
value: Parameters<IntlShape[Name]>[0]
|
|
99
|
+
children?(val: string): React.ReactElement | null
|
|
100
|
+
}
|
|
101
|
+
> {
|
|
102
|
+
type FormatFn = IntlShape[Name]
|
|
103
|
+
type Props = Formatter[Name] & {
|
|
104
|
+
value: Parameters<FormatFn>[0]
|
|
105
|
+
children?(val: string): React.ReactElement | null
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const Component: React.FC<Props> = props => {
|
|
109
|
+
const intl = useIntl()
|
|
110
|
+
const {value, children, ...formatProps} = props
|
|
111
|
+
// TODO: fix TS type definition for localeMatcher upstream
|
|
112
|
+
const formattedValue = intl[name](value as any, formatProps as any)
|
|
113
|
+
|
|
114
|
+
if (typeof children === 'function') {
|
|
115
|
+
return children(formattedValue as any)
|
|
116
|
+
}
|
|
117
|
+
const Text = intl.textComponent || React.Fragment
|
|
118
|
+
return <Text>{formattedValue}</Text>
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
Component.displayName = DisplayName[name]
|
|
122
|
+
return Component
|
|
123
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import {FormatDateOptions} from '@formatjs/intl'
|
|
3
|
+
import {DateTimeFormat} from '@formatjs/ecma402-abstract'
|
|
4
|
+
import useIntl from './useIntl'
|
|
5
|
+
|
|
6
|
+
interface Props extends FormatDateOptions {
|
|
7
|
+
from: Parameters<DateTimeFormat['formatRange']>[0]
|
|
8
|
+
to: Parameters<DateTimeFormat['formatRange']>[1]
|
|
9
|
+
children?(value: React.ReactNode): React.ReactElement | null
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const FormattedDateTimeRange: React.FC<Props> = props => {
|
|
13
|
+
const intl = useIntl()
|
|
14
|
+
|
|
15
|
+
const {from, to, children, ...formatProps} = props
|
|
16
|
+
const formattedValue = intl.formatDateTimeRange(from, to, formatProps)
|
|
17
|
+
|
|
18
|
+
if (typeof children === 'function') {
|
|
19
|
+
return children(formattedValue as any)
|
|
20
|
+
}
|
|
21
|
+
const Text = intl.textComponent || React.Fragment
|
|
22
|
+
return <Text>{formattedValue}</Text>
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
FormattedDateTimeRange.displayName = 'FormattedDateTimeRange'
|
|
26
|
+
export default FormattedDateTimeRange
|