Skip to content

Commit f9c8ea5

Browse files
authored
docs(use-i18n): add formatList to README.md (#634)
* docs(use-i18n): add `formatList` to README.md * fix: old code example
1 parent 4cd78fe commit f9c8ea5

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

packages/use-i18n/README.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ const App = () => {
186186
const { formatDate } = useI18n()
187187

188188
const units = [
189-
formatDate(new Date(2020, 1, 13, 16, 28)) // "Feb 13, 2020"
190-
formatDate(1581607680000, 'long') // "February 13, 2020"
189+
formatDate(new Date(2020, 1, 13, 16, 28)), // "Feb 13, 2020"
190+
formatDate(1581607680000, 'long'), // "February 13, 2020"
191191
formatDate('2020-02-13T15:28:00.000Z', {
192192
day: "numeric",
193193
era: "short",
@@ -209,6 +209,35 @@ const App = () => {
209209
}
210210
```
211211
212+
### formatList
213+
214+
This hook exposes a `formatList` function which can be used to format lists of strings.
215+
216+
The first parameter is an array of strings to format.
217+
218+
It accepts an `options` as second parameter which is an [Intl.ListFormat `options` object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat/ListFormat).
219+
220+
```js
221+
import { useI18n } from '@scaleway/use-i18n'
222+
223+
const App = () => {
224+
const { formatList } = useI18n()
225+
226+
const cities = [
227+
formatList(['Paris', 'New York', 'London']), // Paris, New York and London
228+
formatList(['Paris', 'New York', 'London'], {
229+
type: 'disjunction'
230+
}) // Paris, New York or London
231+
]
232+
233+
return (
234+
<div>
235+
{cities}
236+
</div>
237+
)
238+
}
239+
```
240+
212241
### formatUnit
213242
214243
This hook also exposes a `formatUnit` function which can be used to format bits/bytes until [ECMA-402 Unit Preferences](https://github.com/tc39/proposal-smart-unit-preferences) is standardised
@@ -229,8 +258,8 @@ const App = () => {
229258
const { formatUnit } = useI18n()
230259

231260
const units = [
232-
formatUnit(12, { unit: 'kilobyte' }) // "12 KB" or "12 Ko" in fr an ro
233-
formatUnit(10 ** 8, { unit: 'bytes-humanized' }) // "100 MB" or "100 Mo" in fr an ro
261+
formatUnit(12, { unit: 'kilobyte' }), // "12 KB" or "12 Ko" in fr an ro
262+
formatUnit(10 ** 8, { unit: 'bytes-humanized' }), // "100 MB" or "100 Mo" in fr an ro
234263
formatUnit(10 ** 8, { unit: 'bits-per-second-humanized' }) // "100Mbs"
235264
]
236265

0 commit comments

Comments
 (0)