15.18.0 • Published 3 months ago

@getcircuit/intl-tools v15.18.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

@getcircuit/intl-tools

Package of i18n related tools for Circuit's web apps.

About

Circuit has its specific requirements regarding i18n. Our i18n approach is based react-intl, but we have a few additional requirements that are not supported natively by it.

@getcircuit/intl-tools is a wrapper around react-intl that provides a few additional features that will be explained in the following sections.

How to use

This package is supposed to work exactly like react-intl, so make sure to check out their documentation.

The main key differences are the following:

IntlToolsProvider instead of IntlProvider

Instead of using IntlProvider from react-intl, use IntlToolsProvider from @getcircuit/intl-tools.

-import { IntlProvider } from 'react-intl'
+import { IntlToolsProvider } from '@getcircuit/intl-tools'

const App = () => {
  return (
-   <IntlProvider locale="en" messages={messages}>
+   <IntlToolsProvider locale="en" messages={messages} preferredHourFormat="24-hour">
      <App />
+   </IntlToolsProvider>
-   </IntlProvider>
)
}

useIntlTools instead of useIntl

Instead of using intl from react-intl, use intlTools from @getcircuit/intl-tools.

-import { useIntl } from 'react-intl'
+import { useIntlTools } from '@getcircuit/intl-tools'

const MyComponent = () => {
-  const intl = useIntl()
+  const intlTools = useIntlTools()
   const { preferredHourFormat, formatEpochTime, formatEpochDate, formatCurrency } = intlTools
   // ...
}

IntlToolsBag instead of IntlShape

The object exposed by the IntlToolsProvider and read via useIntlTools is not the same as the one provided by react-intl. It's composed of a reduced version of the original IntlShape object, plus a few extra properties.

type IntlToolsBag = {
  // Picked, original properties
  locale: string
  timeZone?: string
  messages: Record<string, string>
  textComponent: React.ElementType
  formatDate
  formatDateToParts
  formatTime
  formatTimeToParts
  formatRelativeTime
  formatNumber
  formatPlural
  formatMessage
  formatList
  // Extra properties
  preferredHourFormat: '12-hour' | '24-hour'
  formatEpochTime
  formatEpochDate
  formatCurrency
}

This decision was done to provide an easier-to-use API for Circuit's use case. We can always add more properties to the IntlToolsBag object if needed. What's important is that we have control over this object and we can add/remove properties as we see fit.

And that's it, really. The rest of the API is the same as react-intl.

Components

@getcircuit/intl-tools provides the same components as react-intl, plusa few additional ones.

import {
  // Original components
  FormattedDate,
  FormattedDateToParts,
  FormattedTime,
  FormattedTimeToParts,
  FormattedNumber,
  FormattedMessage,
  FormattedList,
  FormattedPlural,
  FormattedRelativeTime,
  // Additional components
  FormattedCurrency,
} from '@getcircuit/intl-tools'

The original components documentation can be found in react-intl's page.

FormattedCurrency

Formats a number as the specified currency. It takes the same props as react-intl's FormattedNumber, but it also takes into account the user's preferences and some other Circuit specific requirements.

import { FormattedCurrency } from '@getcircuit/intl-tools'

const MyComponent = () => {
  return (
    <FormattedCurrency value={1000} currency="USD" currencyDisplay="symbol" />
  )
}

FormattedEpochTime

Formats an epoch timestamp as time.

import { FormattedEpochTime } from '@getcircuit/intl-tools'

const MyComponent = () => {
  return <FormattedEpochTime value={1679064790} /> // 15:53
}

FormattedEpochDate

Formats an epoch timestamp as date.

import { FormattedEpochDate } from '@getcircuit/intl-tools'

const MyComponent = () => {
  return <FormattedEpochDate value={1679064790} /> // 2023-03-17
}

FormattedSecondsTime

Formats a number of seconds as time.

import { FormattedSecondsTime } from '@getcircuit/intl-tools'

const MyComponent = () => {
  return <FormattedSecondsTime value={3600} /> // 01:00
}

Utilities

@getcircuit/intl-tools provides a few handy utilities.

getBrowserLocale()

Returns the browser's locale.

import { getBrowserLocale } from '@getcircuit/intl-tools'

const locale = getBrowserLocale() // 'en-GB'

getBrowserTimeZone

Returns the browser's time zone.

import { getBrowserTimeZone } from '@getcircuit/intl-tools'

const timeZone = getBrowserTimeZone() // 'Europe/London'

getBrowserHourFormat

Returns the browser's hour format in Circuit's 12-hour or 24-hour format.

Note ⚠️: this method doesn't work in Firefox. Make sure to provide a fallback value if you need to support Firefox.

import { getBrowserHourFormat } from '@getcircuit/intl-tools'

const hourFormat = getBrowserHourFormat() // '12-hour'
15.18.0

3 months ago

15.17.15

4 months ago

15.17.14

4 months ago

15.17.13

4 months ago

15.17.12

4 months ago

15.17.11

4 months ago

15.17.10

4 months ago

15.17.9

4 months ago

15.17.8

4 months ago

15.17.7

4 months ago

15.17.6

4 months ago

15.17.5

4 months ago

15.17.4

4 months ago

15.17.3

4 months ago

15.17.2

4 months ago

15.17.1

4 months ago

15.18.0-leo.0

4 months ago

15.17.0

4 months ago

15.16.2-leo.0

4 months ago

15.16.1

4 months ago

15.16.0

4 months ago

15.15.2

4 months ago

15.15.1

4 months ago

15.15.0

4 months ago

15.14.0

4 months ago

15.13.6

4 months ago

15.13.5

4 months ago

15.13.4

4 months ago

15.13.3

5 months ago

15.13.2

5 months ago

15.13.1

5 months ago

15.13.0

5 months ago

15.12.12

5 months ago

15.12.11

5 months ago

15.12.10

5 months ago

15.12.6

5 months ago

15.12.2

5 months ago

15.12.1

5 months ago

15.12.0

5 months ago

15.11.4-potato.0

5 months ago

15.12.0-slct.0

5 months ago

15.11.1

5 months ago

15.10.2

5 months ago

15.11.0-leo.0

5 months ago

15.8.2

5 months ago

15.8.1

5 months ago

15.9.0-sd.0

5 months ago

15.7.0

5 months ago

15.7.0-select.0

5 months ago

15.6.12

5 months ago

15.6.11

5 months ago

15.6.9-potato.1

5 months ago

15.6.9-potato.0

5 months ago

15.7.0-pow.0

5 months ago

15.6.8

5 months ago

15.6.8-intl.1

5 months ago

15.6.8-intl.0

5 months ago

15.6.8-pow.0

5 months ago

15.6.5-pow.0

5 months ago

15.6.3

5 months ago

15.6.2-pow.0

5 months ago

15.6.1

5 months ago

15.6.0

5 months ago

15.6.0-pow.0

6 months ago

15.1.1

6 months ago

15.2.0-pow.0

6 months ago

15.0.2

6 months ago

15.1.0-dws.0

6 months ago

15.0.0

6 months ago

14.67.6

7 months ago

14.67.5

7 months ago

14.67.1

7 months ago

14.66.0

7 months ago

14.65.3

7 months ago

14.64.1

7 months ago

14.62.8

7 months ago

14.62.0

8 months ago

14.60.0-ttp.0

8 months ago

14.60.0-pow.0

8 months ago

14.59.0

8 months ago

14.57.8

8 months ago

14.57.4

8 months ago

14.57.3

8 months ago

14.57.2

8 months ago

14.54.2

8 months ago

14.49.0-zoning.0

8 months ago

14.45.3

8 months ago

14.45.1

8 months ago

14.45.1-pow.0

8 months ago

14.45.0

8 months ago

14.44.0

8 months ago

14.35.3-leo.0

9 months ago

14.36.0-alpha.0

9 months ago

14.35.0

9 months ago

14.33.1

9 months ago

14.30.0

9 months ago

14.29.0-potato.0

9 months ago

14.29.0

9 months ago

14.27.2

9 months ago

14.27.1

9 months ago

14.26.2

9 months ago

14.26.1

9 months ago

14.26.0

9 months ago

14.25.0

9 months ago

14.23.0

10 months ago

14.21.0-tll.0

10 months ago

14.20.0

10 months ago

14.19.0-react18.0

10 months ago

14.18.0-react18.0

10 months ago

14.16.0-react18.2

10 months ago

14.16.0-react18.1

10 months ago

14.16.0-react18.0

10 months ago

14.13.0-pow.0

11 months ago

14.12.0-yam.0

11 months ago

14.11.0

11 months ago

14.5.0

11 months ago

14.4.0-alpha.0

11 months ago

14.3.0-alpha.7

11 months ago

14.3.0-alpha.6

11 months ago

14.2.6-pow.0

11 months ago

14.3.0-yam.1.0

11 months ago

14.2.4

11 months ago

14.3.0-read-only.0

11 months ago

14.0.1

12 months ago

14.0.0

12 months ago

13.13.1

12 months ago

13.14.0-pow.0

12 months ago

13.5.0-yam.1.0

1 year ago

13.5.0-yam.0

1 year ago

13.4.0

1 year ago

13.4.0-yam.0

1 year ago

13.3.0

1 year ago

13.0.0

1 year ago

12.18.0-pow.0

1 year ago

12.17.4

1 year ago

12.17.0

1 year ago

12.16.2

1 year ago

12.15.0-yam.2

1 year ago

12.17.0-pow.0

1 year ago

12.15.0-yam.0

1 year ago

12.15.0-pow.0

1 year ago

12.12.0-leo.0

1 year ago

12.12.0-pim.0

1 year ago

12.12.0-rows.0

1 year ago

12.9.0

1 year ago

12.9.0-rows.0

1 year ago

12.7.0-rows.0

1 year ago

12.5.0-rows.0

1 year ago

12.5.0-row.0

1 year ago

12.4.0-row.0

1 year ago

12.3.0

1 year ago

12.3.0-clx.0

1 year ago

12.2.10

1 year ago

12.2.9

1 year ago

12.2.8

1 year ago

12.3.0-cll.0

1 year ago

12.2.7

1 year ago

12.3.0-ico.0

1 year ago

12.2.5

1 year ago

12.2.5-kiwi.0

1 year ago

12.3.0-cells.0

1 year ago

12.2.3

1 year ago

12.3.0-cell.2

1 year ago

12.2.2

1 year ago

12.3.0-cell.0

1 year ago

12.2.1

1 year ago

12.2.0

1 year ago

0.0.2-kiwi.4

1 year ago

0.0.2-kiwi.3

1 year ago

0.0.2-kiwi.2

1 year ago

0.0.2-kiwi.1

1 year ago

0.0.2-kiwi.0

1 year ago

0.0.1-kiwi.0

1 year ago