MCPcopy Index your code
hub / github.com/formatjs/formatjs / createIntl

Function createIntl

packages/intl/create-intl.ts:57–162  ·  view source on GitHub ↗
(
  config: IntlConfig<T>,
  cache?: IntlCache
)

Source from the content-addressed store, hash-verified

55 * @param cache cache for formatter instances to prevent memory leak
56 */
57export function createIntl<T = string>(
58 config: IntlConfig<T>,
59 cache?: IntlCache
60): IntlShape<T> {
61 const formatters = createFormatters(cache)
62 const resolvedConfig: ResolvedIntlConfig<T> = {
63 ...DEFAULT_INTL_CONFIG,
64 ...config,
65 }
66
67 const {locale, defaultLocale, onError} = resolvedConfig
68 if (!locale) {
69 if (onError) {
70 onError(
71 new InvalidConfigError(
72 `"locale" was not configured, using "${defaultLocale}" as fallback. See https://formatjs.github.io/docs/react-intl/api#intlshape for more details`
73 )
74 )
75 }
76 // Since there's no registered locale data for `locale`, this will
77 // fallback to the `defaultLocale` to make sure things can render.
78 // The `messages` are overridden to the `defaultProps` empty object
79 // to maintain referential equality across re-renders. It's assumed
80 // each <FormattedMessage> contains a `defaultMessage` prop.
81 resolvedConfig.locale = resolvedConfig.defaultLocale || 'en'
82 } else if (!Intl.NumberFormat.supportedLocalesOf(locale).length && onError) {
83 onError(
84 new MissingDataError(
85 `Missing locale data for locale: "${locale}" in Intl.NumberFormat. Using default locale: "${defaultLocale}" as fallback. See https://formatjs.github.io/docs/react-intl#runtime-requirements for more details`
86 )
87 )
88 } else if (
89 !Intl.DateTimeFormat.supportedLocalesOf(locale).length &&
90 onError
91 ) {
92 onError(
93 new MissingDataError(
94 `Missing locale data for locale: "${locale}" in Intl.DateTimeFormat. Using default locale: "${defaultLocale}" as fallback. See https://formatjs.github.io/docs/react-intl#runtime-requirements for more details`
95 )
96 )
97 }
98
99 verifyConfigMessages(resolvedConfig)
100 return {
101 ...resolvedConfig,
102 formatters,
103 formatNumber: formatNumber.bind(
104 null,
105 resolvedConfig,
106 formatters.getNumberFormat
107 ),
108 formatNumberToParts: formatNumberToParts.bind(
109 null,
110 resolvedConfig,
111 formatters.getNumberFormat
112 ),
113 formatRelativeTime: formatRelativeTime.bind(
114 null,

Callers 1

Calls 4

createFormattersFunction · 0.85
onErrorFunction · 0.85
verifyConfigMessagesFunction · 0.85
supportedLocalesOfMethod · 0.65

Tested by

no test coverage detected