UNPKG

react-intl

Version:

Internationalize React apps. This library provides React components and an API to format dates, numbers, and strings, including pluralization and handling translations.

55 lines (54 loc) 1.69 kB
/* * Copyright 2015, Yahoo Inc. * Copyrights licensed under the New BSD License. * See the accompanying LICENSE file for terms. */ import { createIntlCache } from "@formatjs/intl"; import * as React from "react"; import { DEFAULT_INTL_CONFIG, invariantIntlContext, shallowEqual } from "../utils.js"; import { createIntl } from "./createIntl.js"; import { Provider } from "./injectIntl.js"; import { jsx as _jsx } from "react/jsx-runtime"; function processIntlConfig(config) { return { locale: config.locale, timeZone: config.timeZone, fallbackOnEmptyString: config.fallbackOnEmptyString, formats: config.formats, textComponent: config.textComponent, messages: config.messages, defaultLocale: config.defaultLocale, defaultFormats: config.defaultFormats, onError: config.onError, onWarn: config.onWarn, wrapRichTextChunksInFragment: config.wrapRichTextChunksInFragment, defaultRichTextElements: config.defaultRichTextElements }; } export default class IntlProvider extends React.PureComponent { static displayName = "IntlProvider"; static defaultProps = DEFAULT_INTL_CONFIG; cache = createIntlCache(); state = { cache: this.cache, intl: createIntl(processIntlConfig(this.props), this.cache), prevConfig: processIntlConfig(this.props) }; static getDerivedStateFromProps(props, { prevConfig, cache }) { const config = processIntlConfig(props); if (!shallowEqual(prevConfig, config)) { return { intl: createIntl(config, cache), prevConfig: config }; } return null; } render() { invariantIntlContext(this.state.intl); return /* @__PURE__ */ _jsx(Provider, { value: this.state.intl, children: this.props.children }); } }