Skip to content

Commit 5e8bc5c

Browse files
authored
feat: Add new intl-based formatter builder (#227)
* feat: Add new intl-based formatter builder * fixes * add updated example * fix type error * remove duplicate object key
1 parent 6c2e83f commit 5e8bc5c

File tree

6 files changed

+471
-23
lines changed

6 files changed

+471
-23
lines changed

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,46 @@ A simple time-ago component for [React].
1515
React.createElement(TimeAgo, {date: 'Aug 29, 2014'})
1616
```
1717

18+
## Intl Formatter
19+
20+
Since v8.2.0 `react-timeago` comes with a formatter that uses the `Intl` that is built-in to Javascript
21+
to create relative time format in any locale you want with little configuration.
22+
23+
However, this option is not the default. (The default is a custom formatter for English). You can use it
24+
by manually importing the `makeIntlFormatter` function.
25+
26+
```tsx
27+
import TimeAgo from 'react-timeago'
28+
import {makeIntlFormatter} from 'react-timeago/defaultFormatter';
29+
30+
const intlFormatter = makeIntlFormatter({
31+
locale: undefined, // string
32+
localeMatcher?: 'best fit', // 'lookup' | 'best fit',
33+
numberingSystem?: 'latn' // Intl$NumberingSystem such as 'arab', 'deva', 'hebr' etc.
34+
style?: 'long', // 'long' | 'short' | 'narrow',
35+
numeric?: 'auto', // 'always' | 'auto', Using 'auto` will convert "1 day ago" to "yesterday" etc.
36+
});
37+
38+
<TimeAgo date='Feb 1, 1966' formatter={intlFormatter} />
39+
```
40+
41+
`makeIntlFormatter` can be called without any arguments at all and you'll get good defaults.
42+
43+
This new formatter should be your first choice when using `react-timeago` going forward as long as it
44+
has the [browser support](https://caniuse.com/mdn-javascript_builtins_intl_relativetimeformat_format) you need.
45+
1846
## Language support
1947

20-
Since v3.1.0 `react-timeago` now comes with support for a large number of languages out of the box.
48+
Since v3.1.0 `react-timeago` comes with support for a large number of languages out of the box.
2149
This support is based on the string objects taken from `jquery-timeago` and then updated with the help of the
2250
community. Many thanks to all those who contribute language support.
2351

2452
### Usage
53+
2554
To use any of the languages provided, other than the default english, you will have to
2655
import the language strings and build a custom formatter.
2756

28-
```jsx
57+
```tsx
2958
import TimeAgo from 'react-timeago'
3059
import frenchStrings from 'react-timeago/lib/language-strings/fr'
3160
import buildFormatter from 'react-timeago/lib/formatters/buildFormatter'

examples/simple/index.js

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,79 @@
11
/* @flow strict */
22
import * as React from 'react'
3+
import { useMemo } from 'react'
34
import ReactDom from 'react-dom/client'
45
import TimeAgo from '../../src/index'
6+
import { makeIntlFormatter } from '../../src/defaultFormatter'
57

68
const appElement = document.getElementById('app')
79

8-
appElement &&
9-
ReactDom.createRoot(appElement).render(
10+
function Bare({ date }: { date: number }) {
11+
return (
12+
<>
13+
You opened this page <TimeAgo date={date} />
14+
</>
15+
)
16+
}
17+
18+
const intlFormatter = makeIntlFormatter({
19+
locale: 'en',
20+
style: 'long',
21+
numeric: 'auto',
22+
})
23+
24+
function Intl({ date }: { date: number }) {
25+
const [locale, setLocale] = React.useState('en')
26+
const [style, setStyle] = React.useState('long')
27+
const [numeric, setNumeric] = React.useState('auto')
28+
29+
const intlFormatter = useMemo(
30+
() =>
31+
makeIntlFormatter({
32+
locale,
33+
style,
34+
numeric,
35+
}),
36+
[locale, style, numeric],
37+
)
38+
return (
1039
<div>
11-
You opened this page <TimeAgo date={Date.now()} />
12-
</div>,
40+
<select value={locale} onChange={(e) => setLocale(e.target.value)}>
41+
<option value="en">en</option>
42+
<option value="fr">fr</option>
43+
<option value="es">es</option>
44+
<option value="de">de</option>
45+
<option value="it">it</option>
46+
<option value="ja">ja</option>
47+
<option value="ko">ko</option>
48+
<option value="zh">zh</option>
49+
</select>
50+
<select value={style} onChange={(e) => setStyle(e.target.value)}>
51+
<option value="long">long</option>
52+
<option value="short">short</option>
53+
<option value="narrow">narrow</option>
54+
</select>
55+
<select value={numeric} onChange={(e) => setNumeric(e.target.value)}>
56+
<option value="auto">auto</option>
57+
<option value="always">always</option>
58+
</select>
59+
<TimeAgo date={date} formatter={intlFormatter} />
60+
</div>
1361
)
62+
}
63+
64+
function App() {
65+
const [date, setDate] = React.useState(Date.now())
66+
67+
return (
68+
<>
69+
<h1>Bare</h1>
70+
<Bare date={date} />
71+
<h1>Intl</h1>
72+
You opened this page <Intl date={date} />
73+
<hr />
74+
<button onClick={() => setDate(Date.now())}>Reset Time</button>
75+
</>
76+
)
77+
}
78+
79+
appElement && ReactDom.createRoot(appElement).render(<App />)

flow-typed/browser.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)