react-intl 3.9.3 → 3.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/CHANGELOG.md +34 -1
  2. package/dist/components/createFormattedComponent.d.ts +2 -1
  3. package/dist/components/createFormattedComponent.js +7 -2
  4. package/dist/components/html-message.js +1 -1
  5. package/dist/components/injectIntl.d.ts +1 -1
  6. package/dist/components/message.js +13 -4
  7. package/dist/components/plural.d.ts +1 -1
  8. package/dist/components/provider.js +14 -9
  9. package/dist/components/relative.js +3 -10
  10. package/dist/formatters/dateTime.js +2 -2
  11. package/dist/formatters/displayName.d.ts +2 -0
  12. package/dist/formatters/displayName.js +25 -0
  13. package/dist/formatters/message.d.ts +1 -0
  14. package/dist/formatters/message.js +19 -18
  15. package/dist/index.d.ts +7 -3
  16. package/dist/index.js +5 -2
  17. package/dist/react-intl.api.md +22 -5
  18. package/dist/react-intl.d.ts +64 -15
  19. package/dist/react-intl.js +5821 -5655
  20. package/dist/react-intl.js.map +1 -1
  21. package/dist/react-intl.min.js +1 -1
  22. package/dist/react-intl.min.js.map +1 -1
  23. package/dist/types.d.ts +6 -1
  24. package/dist/utils.d.ts +1 -1
  25. package/dist/utils.js +5 -7
  26. package/lib/components/createFormattedComponent.d.ts +2 -1
  27. package/lib/components/createFormattedComponent.js +7 -2
  28. package/lib/components/html-message.js +1 -1
  29. package/lib/components/injectIntl.d.ts +1 -1
  30. package/lib/components/message.js +5 -3
  31. package/lib/components/plural.d.ts +1 -1
  32. package/lib/components/provider.js +14 -9
  33. package/lib/components/relative.js +3 -10
  34. package/lib/formatters/dateTime.js +2 -2
  35. package/lib/formatters/displayName.d.ts +2 -0
  36. package/lib/formatters/displayName.js +22 -0
  37. package/lib/formatters/message.d.ts +1 -0
  38. package/lib/formatters/message.js +12 -18
  39. package/lib/index.d.ts +7 -3
  40. package/lib/index.js +4 -1
  41. package/lib/react-intl.d.ts +20 -7
  42. package/lib/tsdoc-metadata.json +1 -1
  43. package/lib/types.d.ts +6 -1
  44. package/lib/utils.d.ts +1 -1
  45. package/lib/utils.js +4 -6
  46. package/package.json +38 -39
  47. package/src/components/createFormattedComponent.tsx +5 -0
  48. package/src/components/provider.tsx +26 -11
  49. package/src/components/relative.tsx +6 -15
  50. package/src/formatters/displayName.ts +35 -0
  51. package/src/formatters/message.ts +13 -18
  52. package/src/index.ts +7 -1
  53. package/src/types.ts +16 -5
  54. package/src/utils.ts +5 -7
package/dist/types.d.ts CHANGED
@@ -4,6 +4,7 @@ import IntlRelativeTimeFormat, { IntlRelativeTimeFormatOptions } from '@formatjs
4
4
  import { MessageFormatElement } from 'intl-messageformat-parser';
5
5
  import { UnifiedNumberFormatOptions } from '@formatjs/intl-unified-numberformat';
6
6
  import IntlListFormat, { IntlListFormatOptions } from '@formatjs/intl-listformat';
7
+ import { DisplayNames, DisplayNamesOptions } from '@formatjs/intl-displaynames';
7
8
  export interface IntlConfig {
8
9
  locale: string;
9
10
  timeZone?: string;
@@ -25,6 +26,7 @@ export declare type FormatNumberOptions = Exclude<UnifiedNumberFormatOptions, 'l
25
26
  export declare type FormatRelativeTimeOptions = Exclude<IntlRelativeTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig;
26
27
  export declare type FormatPluralOptions = Exclude<Intl.PluralRulesOptions, 'localeMatcher'> & CustomFormatConfig;
27
28
  export declare type FormatListOptions = Exclude<IntlListFormatOptions, 'localeMatcher'>;
29
+ export declare type FormatDisplayNameOptions = Exclude<DisplayNamesOptions, 'localeMatcher'>;
28
30
  export interface IntlFormatters {
29
31
  formatDate(value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
30
32
  formatTime(value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
@@ -39,6 +41,7 @@ export interface IntlFormatters {
39
41
  formatHTMLMessage(descriptor: MessageDescriptor, values?: Record<string, PrimitiveType>): React.ReactNode;
40
42
  formatList(values: Array<string>, opts?: FormatListOptions): string;
41
43
  formatList(values: Array<string | React.ReactNode>, opts?: FormatListOptions): React.ReactNode;
44
+ formatDisplayName(value: Parameters<DisplayNames['of']>[0], opts?: FormatDisplayNameOptions): string | undefined;
42
45
  }
43
46
  export interface Formatters {
44
47
  getDateTimeFormat(...args: ConstructorParameters<typeof Intl.DateTimeFormat>): Intl.DateTimeFormat;
@@ -47,6 +50,7 @@ export interface Formatters {
47
50
  getRelativeTimeFormat(...args: ConstructorParameters<typeof IntlRelativeTimeFormat>): IntlRelativeTimeFormat;
48
51
  getPluralRules(...args: ConstructorParameters<typeof Intl.PluralRules>): Intl.PluralRules;
49
52
  getListFormat(...args: ConstructorParameters<typeof IntlListFormat>): IntlListFormat;
53
+ getDisplayNames(...args: ConstructorParameters<typeof DisplayNames>): DisplayNames;
50
54
  }
51
55
  export interface IntlShape extends IntlConfig, IntlFormatters {
52
56
  formatters: Formatters;
@@ -58,9 +62,10 @@ export interface IntlCache {
58
62
  relativeTime: Record<string, IntlRelativeTimeFormat>;
59
63
  pluralRules: Record<string, Intl.PluralRules>;
60
64
  list: Record<string, IntlListFormat>;
65
+ displayNames: Record<string, DisplayNames>;
61
66
  }
62
67
  export interface MessageDescriptor {
63
- id?: string;
68
+ id?: string | number;
64
69
  description?: string | object;
65
70
  defaultMessage?: string;
66
71
  }
package/dist/utils.d.ts CHANGED
@@ -2,7 +2,7 @@ import { IntlConfig, IntlCache, CustomFormats, Formatters } from './types';
2
2
  import { IntlRelativeTimeFormatOptions } from '@formatjs/intl-relativetimeformat';
3
3
  export declare function escape(str: string): string;
4
4
  export declare function filterProps<T extends Record<string, any>, K extends string>(props: T, whitelist: Array<K>, defaults?: Partial<T>): Pick<T, K>;
5
- export declare function invariantIntlContext(intl?: any): void;
5
+ export declare function invariantIntlContext(intl?: any): asserts intl;
6
6
  export declare function createError(message: string, exception?: Error): string;
7
7
  export declare function defaultErrorHandler(error: string): void;
8
8
  export declare const DEFAULT_INTL_CONFIG: Pick<IntlConfig, 'formats' | 'messages' | 'timeZone' | 'textComponent' | 'defaultLocale' | 'defaultFormats' | 'onError'>;
package/dist/utils.js CHANGED
@@ -13,12 +13,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
13
13
  var React = require("react");
14
14
  var intl_messageformat_1 = require("intl-messageformat");
15
15
  var intl_format_cache_1 = require("intl-format-cache");
16
- // Since rollup cannot deal with namespace being a function,
17
- // this is to interop with TypeScript since `invariant`
18
- // does not export a default
19
- // https://github.com/rollup/rollup/issues/1267
20
- var invariant_ = require("invariant");
21
- var invariant = invariant_.default || invariant_;
16
+ var intl_utils_1 = require("@formatjs/intl-utils");
22
17
  var ESCAPED_CHARS = {
23
18
  38: '&amp;',
24
19
  62: '&gt;',
@@ -45,7 +40,7 @@ function filterProps(props, whitelist, defaults) {
45
40
  }
46
41
  exports.filterProps = filterProps;
47
42
  function invariantIntlContext(intl) {
48
- invariant(intl, '[React Intl] Could not find required `intl` object. ' +
43
+ intl_utils_1.invariant(intl, '[React Intl] Could not find required `intl` object. ' +
49
44
  '<IntlProvider> needs to exist in the component ancestry.');
50
45
  }
51
46
  exports.invariantIntlContext = invariantIntlContext;
@@ -77,6 +72,7 @@ function createIntlCache() {
77
72
  relativeTime: {},
78
73
  pluralRules: {},
79
74
  list: {},
75
+ displayNames: {},
80
76
  };
81
77
  }
82
78
  exports.createIntlCache = createIntlCache;
@@ -88,6 +84,7 @@ function createFormatters(cache) {
88
84
  if (cache === void 0) { cache = createIntlCache(); }
89
85
  var RelativeTimeFormat = Intl.RelativeTimeFormat;
90
86
  var ListFormat = Intl.ListFormat;
87
+ var DisplayNames = Intl.DisplayNames;
91
88
  return {
92
89
  getDateTimeFormat: intl_format_cache_1.default(Intl.DateTimeFormat, cache.dateTime),
93
90
  getNumberFormat: intl_format_cache_1.default(Intl.NumberFormat, cache.number),
@@ -95,6 +92,7 @@ function createFormatters(cache) {
95
92
  getRelativeTimeFormat: intl_format_cache_1.default(RelativeTimeFormat, cache.relativeTime),
96
93
  getPluralRules: intl_format_cache_1.default(Intl.PluralRules, cache.pluralRules),
97
94
  getListFormat: intl_format_cache_1.default(ListFormat, cache.list),
95
+ getDisplayNames: intl_format_cache_1.default(DisplayNames, cache.displayNames),
98
96
  };
99
97
  }
100
98
  exports.createFormatters = createFormatters;
@@ -1,10 +1,11 @@
1
1
  import * as React from 'react';
2
- import { IntlShape, FormatDateOptions, FormatNumberOptions, FormatListOptions } from '../types';
2
+ import { IntlShape, FormatDateOptions, FormatNumberOptions, FormatListOptions, FormatDisplayNameOptions } from '../types';
3
3
  declare type Formatter = {
4
4
  formatDate: FormatDateOptions;
5
5
  formatTime: FormatDateOptions;
6
6
  formatNumber: FormatNumberOptions;
7
7
  formatList: FormatListOptions;
8
+ formatDisplayName: FormatDisplayNameOptions;
8
9
  };
9
10
  export declare const FormattedNumberParts: React.FC<Formatter['formatNumber'] & {
10
11
  value: Parameters<IntlShape['formatNumber']>[0];
@@ -3,8 +3,10 @@ var __rest = (this && this.__rest) || function (s, e) {
3
3
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
4
  t[p] = s[p];
5
5
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
7
- t[p[i]] = s[p[i]];
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
8
10
  return t;
9
11
  };
10
12
  import * as React from 'react';
@@ -16,6 +18,9 @@ var DisplayName;
16
18
  DisplayName["formatTime"] = "FormattedTime";
17
19
  DisplayName["formatNumber"] = "FormattedNumber";
18
20
  DisplayName["formatList"] = "FormattedList";
21
+ // Note that this DisplayName is the locale display name, not to be confused with
22
+ // the name of the enum, which is for React component display name in dev tools.
23
+ DisplayName["formatDisplayName"] = "FormattedDisplayName";
19
24
  })(DisplayName || (DisplayName = {}));
20
25
  var DisplayNameParts;
21
26
  (function (DisplayNameParts) {
@@ -39,5 +39,5 @@ class FormattedHTMLMessage extends FormattedMessage {
39
39
  }
40
40
  }
41
41
  FormattedHTMLMessage.displayName = 'FormattedHTMLMessage';
42
- FormattedHTMLMessage.defaultProps = Object.assign({}, FormattedMessage.defaultProps, { tagName: 'span' });
42
+ FormattedHTMLMessage.defaultProps = Object.assign(Object.assign({}, FormattedMessage.defaultProps), { tagName: 'span' });
43
43
  export default FormattedHTMLMessage;
@@ -1,6 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import { IntlShape, Omit } from '../types';
3
- export declare const Provider: React.ProviderExoticComponent<React.ProviderProps<IntlShape>>;
3
+ export declare const Provider: React.Provider<IntlShape>;
4
4
  export declare const Context: React.Context<IntlShape>;
5
5
  export interface Opts<IntlPropName extends string = 'intl', ForwardRef extends boolean = false> {
6
6
  intlPropName?: IntlPropName;
@@ -8,8 +8,10 @@ var __rest = (this && this.__rest) || function (s, e) {
8
8
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
9
9
  t[p] = s[p];
10
10
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
11
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
12
- t[p[i]] = s[p[i]];
11
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
12
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
13
+ t[p[i]] = s[p[i]];
14
+ }
13
15
  return t;
14
16
  };
15
17
  import * as React from 'react';
@@ -22,7 +24,7 @@ const defaultFormatMessage = (descriptor, values) => {
22
24
  if (process.env.NODE_ENV !== 'production') {
23
25
  console.error('[React Intl] Could not find required `intl` object. <IntlProvider> needs to exist in the component ancestry. Using default message as fallback.');
24
26
  }
25
- return formatMessage(Object.assign({}, DEFAULT_INTL_CONFIG, { locale: 'en' }), createFormatters(), descriptor, values);
27
+ return formatMessage(Object.assign(Object.assign({}, DEFAULT_INTL_CONFIG), { locale: 'en' }), createFormatters(), descriptor, values);
26
28
  };
27
29
  class FormattedMessage extends React.Component {
28
30
  shouldComponentUpdate(nextProps) {
@@ -11,7 +11,7 @@ interface Props extends FormatPluralOptions {
11
11
  many?: React.ReactNode;
12
12
  children?(value: React.ReactNode): React.ReactElement | null;
13
13
  }
14
- declare const _default: React.ForwardRefExoticComponent<Pick<Props, "children" | "other" | "zero" | "one" | "two" | "few" | "many" | "localeMatcher" | "type" | "format" | "value"> & {
14
+ declare const _default: React.ForwardRefExoticComponent<Pick<Props, "children" | "other" | "zero" | "one" | "two" | "few" | "many" | "localeMatcher" | "format" | "type" | "value"> & {
15
15
  forwardedRef?: ((instance: any) => void) | React.RefObject<any> | null | undefined;
16
16
  } & React.RefAttributes<any>> & {
17
17
  WrappedComponent: React.ComponentType<Props>;
@@ -6,7 +6,6 @@
6
6
  import * as React from 'react';
7
7
  import { Provider } from './injectIntl';
8
8
  import { createError, DEFAULT_INTL_CONFIG, createFormatters, invariantIntlContext, createIntlCache, } from '../utils';
9
- import areIntlLocalesSupported from 'intl-locales-supported';
10
9
  import { formatNumber, formatNumberToParts } from '../formatters/number';
11
10
  import { formatRelativeTime } from '../formatters/relativeTime';
12
11
  import { formatDate, formatTime, formatDateToParts, formatTimeToParts, } from '../formatters/dateTime';
@@ -14,6 +13,7 @@ import { formatPlural } from '../formatters/plural';
14
13
  import { formatMessage, formatHTMLMessage } from '../formatters/message';
15
14
  import * as shallowEquals_ from 'shallow-equal/objects';
16
15
  import { formatList } from '../formatters/list';
16
+ import { formatDisplayName } from '../formatters/displayName';
17
17
  const shallowEquals = shallowEquals_.default || shallowEquals_;
18
18
  function processIntlConfig(config) {
19
19
  return {
@@ -34,13 +34,11 @@ function processIntlConfig(config) {
34
34
  */
35
35
  export function createIntl(config, cache) {
36
36
  const formatters = createFormatters(cache);
37
- const resolvedConfig = Object.assign({}, DEFAULT_INTL_CONFIG, config);
38
- if (!resolvedConfig.locale ||
39
- !areIntlLocalesSupported(resolvedConfig.locale)) {
40
- const { locale, defaultLocale, onError } = resolvedConfig;
41
- if (typeof onError === 'function') {
42
- onError(createError(`Missing locale data for locale: "${locale}". ` +
43
- `Using default locale: "${defaultLocale}" as fallback.`));
37
+ const resolvedConfig = Object.assign(Object.assign({}, DEFAULT_INTL_CONFIG), config);
38
+ const { locale, defaultLocale, onError } = resolvedConfig;
39
+ if (!locale) {
40
+ if (onError) {
41
+ onError(createError(`"locale" was not configured, using "${defaultLocale}" as fallback. See https://github.com/formatjs/react-intl/blob/master/docs/API.md#intlshape for more details`));
44
42
  }
45
43
  // Since there's no registered locale data for `locale`, this will
46
44
  // fallback to the `defaultLocale` to make sure things can render.
@@ -49,7 +47,14 @@ export function createIntl(config, cache) {
49
47
  // each <FormattedMessage> contains a `defaultMessage` prop.
50
48
  resolvedConfig.locale = resolvedConfig.defaultLocale || 'en';
51
49
  }
52
- return Object.assign({}, resolvedConfig, { formatters, formatNumber: formatNumber.bind(null, resolvedConfig, formatters.getNumberFormat), formatNumberToParts: formatNumberToParts.bind(null, resolvedConfig, formatters.getNumberFormat), formatRelativeTime: formatRelativeTime.bind(null, resolvedConfig, formatters.getRelativeTimeFormat), formatDate: formatDate.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateToParts: formatDateToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTime: formatTime.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTimeToParts: formatTimeToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatPlural: formatPlural.bind(null, resolvedConfig, formatters.getPluralRules), formatMessage: formatMessage.bind(null, resolvedConfig, formatters), formatHTMLMessage: formatHTMLMessage.bind(null, resolvedConfig, formatters), formatList: formatList.bind(null, resolvedConfig, formatters.getListFormat) });
50
+ else if (!Intl.NumberFormat.supportedLocalesOf(locale).length && onError) {
51
+ onError(createError(`Missing locale data for locale: "${locale}" in Intl.NumberFormat. Using default locale: "${defaultLocale}" as fallback. See https://github.com/formatjs/react-intl/blob/master/docs/Getting-Started.md#runtime-requirements for more details`));
52
+ }
53
+ else if (!Intl.DateTimeFormat.supportedLocalesOf(locale).length &&
54
+ onError) {
55
+ onError(createError(`Missing locale data for locale: "${locale}" in Intl.DateTimeFormat. Using default locale: "${defaultLocale}" as fallback. See https://github.com/formatjs/react-intl/blob/master/docs/Getting-Started.md#runtime-requirements for more details`));
56
+ }
57
+ return Object.assign(Object.assign({}, resolvedConfig), { formatters, formatNumber: formatNumber.bind(null, resolvedConfig, formatters.getNumberFormat), formatNumberToParts: formatNumberToParts.bind(null, resolvedConfig, formatters.getNumberFormat), formatRelativeTime: formatRelativeTime.bind(null, resolvedConfig, formatters.getRelativeTimeFormat), formatDate: formatDate.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatDateToParts: formatDateToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTime: formatTime.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatTimeToParts: formatTimeToParts.bind(null, resolvedConfig, formatters.getDateTimeFormat), formatPlural: formatPlural.bind(null, resolvedConfig, formatters.getPluralRules), formatMessage: formatMessage.bind(null, resolvedConfig, formatters), formatHTMLMessage: formatHTMLMessage.bind(null, resolvedConfig, formatters), formatList: formatList.bind(null, resolvedConfig, formatters.getListFormat), formatDisplayName: formatDisplayName.bind(null, resolvedConfig, formatters.getDisplayNames) });
53
58
  }
54
59
  export default class IntlProvider extends React.PureComponent {
55
60
  constructor() {
@@ -6,12 +6,7 @@
6
6
  import * as React from 'react';
7
7
  import { Context } from './injectIntl';
8
8
  import { invariantIntlContext } from '../utils';
9
- // Since rollup cannot deal with namespace being a function,
10
- // this is to interop with TypeScript since `invariant`
11
- // does not export a default
12
- // https://github.com/rollup/rollup/issues/1267
13
- import * as invariant_ from 'invariant';
14
- const invariant = invariant_.default || invariant_;
9
+ import { invariant } from '@formatjs/intl-utils';
15
10
  const MINUTE = 60;
16
11
  const HOUR = 60 * 60;
17
12
  const DAY = 60 * 60 * 24;
@@ -57,9 +52,6 @@ const INCREMENTABLE_UNITS = ['second', 'minute', 'hour'];
57
52
  function canIncrement(unit = 'second') {
58
53
  return INCREMENTABLE_UNITS.includes(unit);
59
54
  }
60
- function verifyProps(updateIntervalInSeconds, unit) {
61
- invariant(!updateIntervalInSeconds || (updateIntervalInSeconds && canIncrement(unit)), 'Cannot schedule update with unit longer than hour');
62
- }
63
55
  export class FormattedRelativeTime extends React.PureComponent {
64
56
  constructor(props) {
65
57
  super(props);
@@ -72,7 +64,8 @@ export class FormattedRelativeTime extends React.PureComponent {
72
64
  ? valueToSeconds(this.props.value, this.props.unit)
73
65
  : 0,
74
66
  };
75
- verifyProps(props.updateIntervalInSeconds, props.unit);
67
+ invariant(!props.updateIntervalInSeconds ||
68
+ !!(props.updateIntervalInSeconds && canIncrement(props.unit)), 'Cannot schedule update with unit longer than hour');
76
69
  }
77
70
  scheduleNextUpdate({ updateIntervalInSeconds, unit }, { currentValueInSeconds }) {
78
71
  clearTimeout(this._updateTimer);
@@ -21,14 +21,14 @@ const DATE_TIME_FORMAT_OPTIONS = [
21
21
  ];
22
22
  export function getFormatter({ locale, formats, onError, timeZone, }, type, getDateTimeFormat, options = {}) {
23
23
  const { format } = options;
24
- const defaults = Object.assign({}, (timeZone && { timeZone }), (format && getNamedFormat(formats, type, format, onError)));
24
+ const defaults = Object.assign(Object.assign({}, (timeZone && { timeZone })), (format && getNamedFormat(formats, type, format, onError)));
25
25
  let filteredOptions = filterProps(options, DATE_TIME_FORMAT_OPTIONS, defaults);
26
26
  if (type === 'time' &&
27
27
  !filteredOptions.hour &&
28
28
  !filteredOptions.minute &&
29
29
  !filteredOptions.second) {
30
30
  // Add default formatting options if hour, minute, or second isn't defined.
31
- filteredOptions = Object.assign({}, filteredOptions, { hour: 'numeric', minute: 'numeric' });
31
+ filteredOptions = Object.assign(Object.assign({}, filteredOptions), { hour: 'numeric', minute: 'numeric' });
32
32
  }
33
33
  return getDateTimeFormat(locale, filteredOptions);
34
34
  }
@@ -0,0 +1,2 @@
1
+ import { IntlConfig, Formatters, IntlFormatters } from '../types';
2
+ export declare function formatDisplayName({ locale, onError }: Pick<IntlConfig, 'locale' | 'onError'>, getDisplayNames: Formatters['getDisplayNames'], value: Parameters<IntlFormatters['formatDisplayName']>[0], options?: Parameters<IntlFormatters['formatDisplayName']>[1]): string | undefined;
@@ -0,0 +1,22 @@
1
+ import { filterProps, createError } from '../utils';
2
+ const DISPLAY_NAMES_OPTONS = [
3
+ 'localeMatcher',
4
+ 'style',
5
+ 'type',
6
+ 'fallback',
7
+ ];
8
+ export function formatDisplayName({ locale, onError }, getDisplayNames, value, options = {}) {
9
+ const DisplayNames = Intl.DisplayNames;
10
+ if (!DisplayNames) {
11
+ onError(createError(`Intl.DisplayNames is not available in this environment.
12
+ Try polyfilling it using "@formatjs/intl-displaynames"
13
+ `));
14
+ }
15
+ const filteredOptions = filterProps(options, DISPLAY_NAMES_OPTONS);
16
+ try {
17
+ return getDisplayNames(locale, filteredOptions).of(value);
18
+ }
19
+ catch (e) {
20
+ onError(createError('Error formatting display name.', e));
21
+ }
22
+ }
@@ -1,5 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import { Formatters, IntlConfig, MessageDescriptor } from '../types';
3
3
  import { PrimitiveType } from 'intl-messageformat';
4
+ export declare const prepareIntlMessageFormatHtmlOutput: (chunks: (string | object)[]) => React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)>;
4
5
  export declare function formatMessage({ locale, formats, messages, defaultLocale, defaultFormats, onError, }: Pick<IntlConfig, 'locale' | 'formats' | 'messages' | 'defaultLocale' | 'defaultFormats' | 'onError'>, state: Formatters, messageDescriptor?: MessageDescriptor, values?: Record<string, PrimitiveType>): string;
5
6
  export declare function formatHTMLMessage(config: Pick<IntlConfig, 'locale' | 'formats' | 'messages' | 'defaultLocale' | 'defaultFormats' | 'onError'>, state: Formatters, messageDescriptor?: MessageDescriptor, rawValues?: Record<string, PrimitiveType>): React.ReactNode;
@@ -3,12 +3,8 @@
3
3
  * Copyrights licensed under the New BSD License.
4
4
  * See the accompanying LICENSE file for terms.
5
5
  */
6
- // Since rollup cannot deal with namespace being a function,
7
- // this is to interop with TypeScript since `invariant`
8
- // does not export a default
9
- // https://github.com/rollup/rollup/issues/1267
10
- import * as invariant_ from 'invariant';
11
- const invariant = invariant_.default || invariant_;
6
+ import * as React from 'react';
7
+ import { invariant } from '@formatjs/intl-utils';
12
8
  import { createError, escape } from '../utils';
13
9
  import IntlMessageFormat from 'intl-messageformat';
14
10
  function setTimeZoneInOptions(opts, timeZone) {
@@ -18,9 +14,9 @@ function setTimeZoneInOptions(opts, timeZone) {
18
14
  }, {});
19
15
  }
20
16
  function deepMergeOptions(opts1, opts2) {
21
- const keys = Object.keys(Object.assign({}, opts1, opts2));
17
+ const keys = Object.keys(Object.assign(Object.assign({}, opts1), opts2));
22
18
  return keys.reduce((all, k) => {
23
- all[k] = Object.assign({}, (opts1[k] || {}), (opts2[k] || {}));
19
+ all[k] = Object.assign(Object.assign({}, (opts1[k] || {})), (opts2[k] || {}));
24
20
  return all;
25
21
  }, {});
26
22
  }
@@ -29,16 +25,14 @@ function deepMergeFormatsAndSetTimeZone(f1, timeZone) {
29
25
  return f1;
30
26
  }
31
27
  const mfFormats = IntlMessageFormat.formats;
32
- return Object.assign({}, mfFormats, f1, { date: deepMergeOptions(setTimeZoneInOptions(mfFormats.date, timeZone), setTimeZoneInOptions(f1.date || {}, timeZone)), time: deepMergeOptions(setTimeZoneInOptions(mfFormats.time, timeZone), setTimeZoneInOptions(f1.time || {}, timeZone)) });
28
+ return Object.assign(Object.assign(Object.assign({}, mfFormats), f1), { date: deepMergeOptions(setTimeZoneInOptions(mfFormats.date, timeZone), setTimeZoneInOptions(f1.date || {}, timeZone)), time: deepMergeOptions(setTimeZoneInOptions(mfFormats.time, timeZone), setTimeZoneInOptions(f1.time || {}, timeZone)) });
33
29
  }
30
+ export const prepareIntlMessageFormatHtmlOutput = (chunks) => React.createElement(React.Fragment, null, ...chunks);
34
31
  export function formatMessage({ locale, formats, messages, defaultLocale, defaultFormats, onError, timeZone, }, state, messageDescriptor = { id: '' }, values = {}) {
35
32
  const { id, defaultMessage } = messageDescriptor;
36
33
  // `id` is a required field of a Message Descriptor.
37
- invariant(id, '[React Intl] An `id` must be provided to format a message.');
38
- if (!id) {
39
- throw new Error('[React Intl] An `id` must be provided to format a message.');
40
- }
41
- const message = messages && messages[id];
34
+ invariant(!!id, '[React Intl] An `id` must be provided to format a message.');
35
+ const message = messages && messages[String(id)];
42
36
  formats = deepMergeFormatsAndSetTimeZone(formats, timeZone);
43
37
  defaultFormats = deepMergeFormatsAndSetTimeZone(defaultFormats, timeZone);
44
38
  let formattedMessageParts = [];
@@ -77,15 +71,15 @@ export function formatMessage({ locale, formats, messages, defaultLocale, defaul
77
71
  onError(createError(`Cannot format message: "${id}", ` +
78
72
  `using message ${message || defaultMessage ? 'source' : 'id'} as fallback.`));
79
73
  if (typeof message === 'string') {
80
- return message || defaultMessage || id;
74
+ return message || defaultMessage || String(id);
81
75
  }
82
- return defaultMessage || id;
76
+ return defaultMessage || String(id);
83
77
  }
84
78
  if (formattedMessageParts.length === 1 &&
85
79
  typeof formattedMessageParts[0] === 'string') {
86
- return formattedMessageParts[0] || defaultMessage || id;
80
+ return formattedMessageParts[0] || defaultMessage || String(id);
87
81
  }
88
- return formattedMessageParts;
82
+ return prepareIntlMessageFormatHtmlOutput(formattedMessageParts);
89
83
  }
90
84
  export function formatHTMLMessage(config, state, messageDescriptor = { id: '' }, rawValues = {}) {
91
85
  // Process all the values before they are used when formatting the ICU
package/lib/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import * as React from 'react';
2
2
  export * from './types';
3
- export { defineMessages } from '@formatjs/macro';
3
+ export declare function defineMessages<T, U extends Record<string, T>>(msgs: U): U;
4
4
  import { CustomFormatConfig } from './types';
5
5
  import { UnifiedNumberFormatOptions } from '@formatjs/intl-unified-numberformat';
6
6
  import { IntlListFormatOptions } from '@formatjs/intl-listformat';
7
+ import { DisplayNamesOptions } from '@formatjs/intl-displaynames/lib';
7
8
  export { default as injectIntl, Provider as RawIntlProvider, Context as IntlContext, WithIntlProps, WrappedComponentProps, } from './components/injectIntl';
8
9
  export { default as useIntl } from './components/useIntl';
9
10
  export { default as IntlProvider, createIntl } from './components/provider';
@@ -19,11 +20,14 @@ export declare const FormattedNumber: React.FC<UnifiedNumberFormatOptions & Cust
19
20
  export declare const FormattedList: React.FC<IntlListFormatOptions & {
20
21
  value: React.ReactNode[];
21
22
  }>;
22
- export declare const FormattedDateParts: React.FunctionComponent<Intl.DateTimeFormatOptions & CustomFormatConfig & {
23
+ export declare const FormattedDisplayName: React.FC<DisplayNamesOptions & {
24
+ value: string | number | object;
25
+ }>;
26
+ export declare const FormattedDateParts: React.FC<Intl.DateTimeFormatOptions & CustomFormatConfig & {
23
27
  value: string | number | Date | undefined;
24
28
  children(val: Intl.DateTimeFormatPart[]): React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)> | null;
25
29
  }>;
26
- export declare const FormattedTimeParts: React.FunctionComponent<Intl.DateTimeFormatOptions & CustomFormatConfig & {
30
+ export declare const FormattedTimeParts: React.FC<Intl.DateTimeFormatOptions & CustomFormatConfig & {
27
31
  value: string | number | Date | undefined;
28
32
  children(val: Intl.DateTimeFormatPart[]): React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)> | null;
29
33
  }>;
package/lib/index.js CHANGED
@@ -1,4 +1,6 @@
1
- export { defineMessages } from '@formatjs/macro';
1
+ export function defineMessages(msgs) {
2
+ return msgs;
3
+ }
2
4
  import { createFormattedComponent, createFormattedDateTimePartsComponent, } from './components/createFormattedComponent';
3
5
  export { default as injectIntl, Provider as RawIntlProvider, Context as IntlContext, } from './components/injectIntl';
4
6
  export { default as useIntl } from './components/useIntl';
@@ -8,6 +10,7 @@ export const FormattedDate = createFormattedComponent('formatDate');
8
10
  export const FormattedTime = createFormattedComponent('formatTime');
9
11
  export const FormattedNumber = createFormattedComponent('formatNumber');
10
12
  export const FormattedList = createFormattedComponent('formatList');
13
+ export const FormattedDisplayName = createFormattedComponent('formatDisplayName');
11
14
  export const FormattedDateParts = createFormattedDateTimePartsComponent('formatDate');
12
15
  export const FormattedTimeParts = createFormattedDateTimePartsComponent('formatTime');
13
16
  export { FormattedNumberParts } from './components/createFormattedComponent';
@@ -1,4 +1,6 @@
1
- import { defineMessages } from '@formatjs/macro';
1
+ import { DisplayNames } from '@formatjs/intl-displaynames';
2
+ import { DisplayNamesOptions } from '@formatjs/intl-displaynames';
3
+ import { DisplayNamesOptions as DisplayNamesOptions_2 } from '@formatjs/intl-displaynames/lib';
2
4
  import { Formats } from 'intl-messageformat';
3
5
  import { FormatXMLElementFn } from 'intl-messageformat';
4
6
  import IntlListFormat from '@formatjs/intl-listformat';
@@ -30,10 +32,13 @@ export declare interface CustomFormats extends Partial<Formats> {
30
32
  }
31
33
 
32
34
  declare const DEFAULT_INTL_CONFIG: Pick<IntlConfig, 'formats' | 'messages' | 'timeZone' | 'textComponent' | 'defaultLocale' | 'defaultFormats' | 'onError'>;
33
- export { defineMessages }
35
+
36
+ export declare function defineMessages<T, U extends Record<string, T>>(msgs: U): U;
34
37
 
35
38
  export declare type FormatDateOptions = Exclude<Intl.DateTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig;
36
39
 
40
+ export declare type FormatDisplayNameOptions = Exclude<DisplayNamesOptions, 'localeMatcher'>;
41
+
37
42
  export declare type FormatListOptions = Exclude<IntlListFormatOptions, 'localeMatcher'>;
38
43
 
39
44
  export declare type FormatNumberOptions = Exclude<UnifiedNumberFormatOptions, 'localeMatcher'> & CustomFormatConfig;
@@ -46,11 +51,15 @@ export declare const FormattedDate: React.FC<Intl.DateTimeFormatOptions & Custom
46
51
  value: string | number | Date | undefined;
47
52
  }>;
48
53
 
49
- export declare const FormattedDateParts: React.FunctionComponent<Intl.DateTimeFormatOptions & CustomFormatConfig & {
54
+ export declare const FormattedDateParts: React.FC<Intl.DateTimeFormatOptions & CustomFormatConfig & {
50
55
  value: string | number | Date | undefined;
51
56
  children(val: Intl.DateTimeFormatPart[]): React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)> | null;
52
57
  }>;
53
58
 
59
+ export declare const FormattedDisplayName: React.FC<DisplayNamesOptions_2 & {
60
+ value: string | number | object;
61
+ }>;
62
+
54
63
  export declare class FormattedHTMLMessage extends FormattedMessage<Record<string, PrimitiveType>> {
55
64
  static displayName: string;
56
65
  static defaultProps: {
@@ -82,7 +91,7 @@ export declare const FormattedNumberParts: React.FC<Formatter['formatNumber'] &
82
91
  children(val: Intl.NumberFormatPart[]): React.ReactElement | null;
83
92
  }>;
84
93
 
85
- export declare const FormattedPlural: React.ForwardRefExoticComponent<Pick<Props_2, "children" | "other" | "zero" | "one" | "two" | "few" | "many" | "localeMatcher" | "type" | "format" | "value"> & {
94
+ export declare const FormattedPlural: React.ForwardRefExoticComponent<Pick<Props_2, "children" | "other" | "zero" | "one" | "two" | "few" | "many" | "localeMatcher" | "format" | "type" | "value"> & {
86
95
  forwardedRef?: ((instance: any) => void) | React.RefObject<any> | null | undefined;
87
96
  } & React.RefAttributes<any>> & {
88
97
  WrappedComponent: React.ComponentType<Props_2>;
@@ -106,7 +115,7 @@ export declare const FormattedTime: React.FC<Intl.DateTimeFormatOptions & Custom
106
115
  value: string | number | Date | undefined;
107
116
  }>;
108
117
 
109
- export declare const FormattedTimeParts: React.FunctionComponent<Intl.DateTimeFormatOptions & CustomFormatConfig & {
118
+ export declare const FormattedTimeParts: React.FC<Intl.DateTimeFormatOptions & CustomFormatConfig & {
110
119
  value: string | number | Date | undefined;
111
120
  children(val: Intl.DateTimeFormatPart[]): React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)> | null;
112
121
  }>;
@@ -116,6 +125,7 @@ declare type Formatter = {
116
125
  formatTime: FormatDateOptions;
117
126
  formatNumber: FormatNumberOptions;
118
127
  formatList: FormatListOptions;
128
+ formatDisplayName: FormatDisplayNameOptions;
119
129
  };
120
130
 
121
131
  export declare interface Formatters {
@@ -125,6 +135,7 @@ export declare interface Formatters {
125
135
  getRelativeTimeFormat(...args: ConstructorParameters<typeof IntlRelativeTimeFormat>): IntlRelativeTimeFormat;
126
136
  getPluralRules(...args: ConstructorParameters<typeof Intl.PluralRules>): Intl.PluralRules;
127
137
  getListFormat(...args: ConstructorParameters<typeof IntlListFormat>): IntlListFormat;
138
+ getDisplayNames(...args: ConstructorParameters<typeof DisplayNames>): DisplayNames;
128
139
  }
129
140
 
130
141
  export declare function injectIntl<IntlPropName extends string, P extends WrappedComponentProps<IntlPropName> = WrappedComponentProps<any>>(WrappedComponent: React.ComponentType<P>, options?: Opts<IntlPropName, false>): React.FC<WithIntlProps<P>> & {
@@ -142,6 +153,7 @@ export declare interface IntlCache {
142
153
  relativeTime: Record<string, IntlRelativeTimeFormat>;
143
154
  pluralRules: Record<string, Intl.PluralRules>;
144
155
  list: Record<string, IntlListFormat>;
156
+ displayNames: Record<string, DisplayNames>;
145
157
  }
146
158
 
147
159
  export declare interface IntlConfig {
@@ -171,6 +183,7 @@ export declare interface IntlFormatters {
171
183
  formatHTMLMessage(descriptor: MessageDescriptor, values?: Record<string, PrimitiveType>): React.ReactNode;
172
184
  formatList(values: Array<string>, opts?: FormatListOptions): string;
173
185
  formatList(values: Array<string | React.ReactNode>, opts?: FormatListOptions): React.ReactNode;
186
+ formatDisplayName(value: Parameters<DisplayNames['of']>[0], opts?: FormatDisplayNameOptions): string | undefined;
174
187
  }
175
188
 
176
189
  export declare class IntlProvider extends React.PureComponent<OptionalIntlConfig, State> {
@@ -187,7 +200,7 @@ export declare interface IntlShape extends IntlConfig, IntlFormatters {
187
200
  }
188
201
 
189
202
  export declare interface MessageDescriptor {
190
- id?: string;
203
+ id?: string | number;
191
204
  description?: string | object;
192
205
  defaultMessage?: string;
193
206
  }
@@ -227,7 +240,7 @@ declare interface Props_3<V extends Record<string, any> = Record<string, React.R
227
240
  children?(...nodes: React.ReactNodeArray): React.ReactNode;
228
241
  }
229
242
 
230
- export declare const RawIntlProvider: React.ProviderExoticComponent<React.ProviderProps<IntlShape>>;
243
+ export declare const RawIntlProvider: React.Provider<IntlShape>;
231
244
 
232
245
  declare interface State {
233
246
  /**
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.7.0"
8
+ "packageVersion": "7.7.8"
9
9
  }
10
10
  ]
11
11
  }
package/lib/types.d.ts CHANGED
@@ -4,6 +4,7 @@ import IntlRelativeTimeFormat, { IntlRelativeTimeFormatOptions } from '@formatjs
4
4
  import { MessageFormatElement } from 'intl-messageformat-parser';
5
5
  import { UnifiedNumberFormatOptions } from '@formatjs/intl-unified-numberformat';
6
6
  import IntlListFormat, { IntlListFormatOptions } from '@formatjs/intl-listformat';
7
+ import { DisplayNames, DisplayNamesOptions } from '@formatjs/intl-displaynames';
7
8
  export interface IntlConfig {
8
9
  locale: string;
9
10
  timeZone?: string;
@@ -25,6 +26,7 @@ export declare type FormatNumberOptions = Exclude<UnifiedNumberFormatOptions, 'l
25
26
  export declare type FormatRelativeTimeOptions = Exclude<IntlRelativeTimeFormatOptions, 'localeMatcher'> & CustomFormatConfig;
26
27
  export declare type FormatPluralOptions = Exclude<Intl.PluralRulesOptions, 'localeMatcher'> & CustomFormatConfig;
27
28
  export declare type FormatListOptions = Exclude<IntlListFormatOptions, 'localeMatcher'>;
29
+ export declare type FormatDisplayNameOptions = Exclude<DisplayNamesOptions, 'localeMatcher'>;
28
30
  export interface IntlFormatters {
29
31
  formatDate(value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
30
32
  formatTime(value: Parameters<Intl.DateTimeFormat['format']>[0] | string, opts?: FormatDateOptions): string;
@@ -39,6 +41,7 @@ export interface IntlFormatters {
39
41
  formatHTMLMessage(descriptor: MessageDescriptor, values?: Record<string, PrimitiveType>): React.ReactNode;
40
42
  formatList(values: Array<string>, opts?: FormatListOptions): string;
41
43
  formatList(values: Array<string | React.ReactNode>, opts?: FormatListOptions): React.ReactNode;
44
+ formatDisplayName(value: Parameters<DisplayNames['of']>[0], opts?: FormatDisplayNameOptions): string | undefined;
42
45
  }
43
46
  export interface Formatters {
44
47
  getDateTimeFormat(...args: ConstructorParameters<typeof Intl.DateTimeFormat>): Intl.DateTimeFormat;
@@ -47,6 +50,7 @@ export interface Formatters {
47
50
  getRelativeTimeFormat(...args: ConstructorParameters<typeof IntlRelativeTimeFormat>): IntlRelativeTimeFormat;
48
51
  getPluralRules(...args: ConstructorParameters<typeof Intl.PluralRules>): Intl.PluralRules;
49
52
  getListFormat(...args: ConstructorParameters<typeof IntlListFormat>): IntlListFormat;
53
+ getDisplayNames(...args: ConstructorParameters<typeof DisplayNames>): DisplayNames;
50
54
  }
51
55
  export interface IntlShape extends IntlConfig, IntlFormatters {
52
56
  formatters: Formatters;
@@ -58,9 +62,10 @@ export interface IntlCache {
58
62
  relativeTime: Record<string, IntlRelativeTimeFormat>;
59
63
  pluralRules: Record<string, Intl.PluralRules>;
60
64
  list: Record<string, IntlListFormat>;
65
+ displayNames: Record<string, DisplayNames>;
61
66
  }
62
67
  export interface MessageDescriptor {
63
- id?: string;
68
+ id?: string | number;
64
69
  description?: string | object;
65
70
  defaultMessage?: string;
66
71
  }
package/lib/utils.d.ts CHANGED
@@ -2,7 +2,7 @@ import { IntlConfig, IntlCache, CustomFormats, Formatters } from './types';
2
2
  import { IntlRelativeTimeFormatOptions } from '@formatjs/intl-relativetimeformat';
3
3
  export declare function escape(str: string): string;
4
4
  export declare function filterProps<T extends Record<string, any>, K extends string>(props: T, whitelist: Array<K>, defaults?: Partial<T>): Pick<T, K>;
5
- export declare function invariantIntlContext(intl?: any): void;
5
+ export declare function invariantIntlContext(intl?: any): asserts intl;
6
6
  export declare function createError(message: string, exception?: Error): string;
7
7
  export declare function defaultErrorHandler(error: string): void;
8
8
  export declare const DEFAULT_INTL_CONFIG: Pick<IntlConfig, 'formats' | 'messages' | 'timeZone' | 'textComponent' | 'defaultLocale' | 'defaultFormats' | 'onError'>;