1
1
import { Locale , formatDistanceToNow , formatDistanceToNowStrict } from 'date-fns'
2
- import memoizeIntlConstructor from 'intl-format-cache'
3
- import IntlTranslationFormat from 'intl-messageformat'
4
2
import PropTypes from 'prop-types'
5
3
import React , {
6
4
ReactElement ,
@@ -16,6 +14,7 @@ import ReactDOM from 'react-dom'
16
14
import 'intl-pluralrules'
17
15
import dateFormat , { FormatDateOptions } from './formatDate'
18
16
import unitFormat , { FormatUnitOptions } from './formatUnit'
17
+ import formatters from './formatters'
19
18
20
19
const LOCALE_ITEM_STORAGE = 'locale'
21
20
@@ -60,7 +59,7 @@ interface Context {
60
59
dateFnsLocale ?: Locale ,
61
60
datetime ?: ( date : Date | number , options ?: Intl . DateTimeFormatOptions ) => string ,
62
61
formatDate ?: ( value : Date | number | string , options : FormatDateOptions ) => string ,
63
- formatList ?: ( listFormat : string [ ] , options ?: Intl . ListFormatOptions ) => string ,
62
+ formatList ?: ( listFormat : [ string | undefined ] , options ?: Intl . ListFormatOptions ) => string ,
64
63
formatNumber ?: ( numb : number , options ?: Intl . NumberFormatOptions ) => string ,
65
64
formatUnit ?: ( value : number , options : FormatUnitOptions ) => string ,
66
65
loadTranslations ?: ( namespace : string , load ?: LoadTranslationsFn ) => Promise < string > ,
@@ -114,12 +113,6 @@ export const useTranslation = (namespaces: string[] = [], load: LoadTranslations
114
113
return { ...context , isLoaded }
115
114
}
116
115
117
- // https://formatjs.io/docs/intl-messageformat/
118
- const getTranslationFormat = memoizeIntlConstructor ( IntlTranslationFormat )
119
- const getNumberFormat = memoizeIntlConstructor ( Intl . NumberFormat )
120
- const getDateTimeFormat = memoizeIntlConstructor ( Intl . DateTimeFormat )
121
- const getListFormat = memoizeIntlConstructor ( Intl . ListFormat )
122
-
123
116
type LoadTranslationsFn = ( { namespace, locale } : { namespace : string , locale : string } ) => Promise < { default : Translations } >
124
117
type LoadLocaleFn = ( locale : string ) => Promise < Locale >
125
118
@@ -218,15 +211,15 @@ const I18nContextProvider = ({
218
211
const formatNumber = useCallback (
219
212
// intl-format-chache does not forwrad return types
220
213
// eslint-disable-next-line
221
- ( numb : number , options ?: Intl . NumberFormatOptions ) => getNumberFormat ( currentLocale , options ) . format ( numb ) ,
214
+ ( numb : number , options ?: Intl . NumberFormatOptions ) => formatters . getNumberFormat ( currentLocale , options ) . format ( numb ) ,
222
215
[ currentLocale ] ,
223
216
)
224
217
225
218
const formatList = useCallback (
226
- ( listFormat : string [ ] , options ?: Intl . ListFormatOptions ) =>
219
+ ( listFormat : [ string | undefined ] , options ?: Intl . ListFormatOptions ) =>
227
220
// intl-format-chache does not forwrad return types
228
221
// eslint-disable-next-line
229
- getListFormat ( currentLocale , options ) . format ( listFormat ) ,
222
+ formatters . getListFormat ( currentLocale , options ) . format ( listFormat ) ,
230
223
[ currentLocale ] ,
231
224
)
232
225
@@ -235,7 +228,7 @@ const I18nContextProvider = ({
235
228
// be able to use formatNumber directly
236
229
const formatUnit = useCallback (
237
230
( value : number , options : FormatUnitOptions ) =>
238
- unitFormat ( currentLocale , value , options , getTranslationFormat ) ,
231
+ unitFormat ( currentLocale , value , options ) ,
239
232
[ currentLocale ] ,
240
233
)
241
234
@@ -248,7 +241,7 @@ const I18nContextProvider = ({
248
241
const datetime = useCallback (
249
242
// intl-format-chache does not forwrad return types
250
243
// eslint-disable-next-line
251
- ( date : Date | number , options ?: Intl . DateTimeFormatOptions ) : string => getDateTimeFormat ( currentLocale , options ) . format ( date ) ,
244
+ ( date : Date | number , options ?: Intl . DateTimeFormatOptions ) : string => formatters . getDateTimeFormat ( currentLocale , options ) . format ( date ) ,
252
245
[ currentLocale ] ,
253
246
)
254
247
@@ -283,7 +276,7 @@ const I18nContextProvider = ({
283
276
[ dateFnsLocale ] ,
284
277
)
285
278
286
- const translate = useCallback (
279
+ const translate = useCallback < TranslateFn > (
287
280
( key : string , context ?: Record < string , PrimitiveType > ) => {
288
281
const value = translations [ currentLocale ] ?. [ key ]
289
282
if ( ! value ) {
@@ -296,7 +289,7 @@ const I18nContextProvider = ({
296
289
if ( context ) {
297
290
// intl-format-chache does not forwrad return types
298
291
// eslint-disable-next-line
299
- return getTranslationFormat ( value , currentLocale ) . format ( context )
292
+ return formatters . getTranslationFormat ( value , currentLocale ) . format ( context ) as string
300
293
}
301
294
302
295
return value
0 commit comments