react-intl 3.11.0 → 3.12.0

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.
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,6 +62,7 @@ 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
68
  id?: string | number;
package/dist/utils.js CHANGED
@@ -72,6 +72,7 @@ function createIntlCache() {
72
72
  relativeTime: {},
73
73
  pluralRules: {},
74
74
  list: {},
75
+ displayNames: {},
75
76
  };
76
77
  }
77
78
  exports.createIntlCache = createIntlCache;
@@ -83,6 +84,7 @@ function createFormatters(cache) {
83
84
  if (cache === void 0) { cache = createIntlCache(); }
84
85
  var RelativeTimeFormat = Intl.RelativeTimeFormat;
85
86
  var ListFormat = Intl.ListFormat;
87
+ var DisplayNames = Intl.DisplayNames;
86
88
  return {
87
89
  getDateTimeFormat: intl_format_cache_1.default(Intl.DateTimeFormat, cache.dateTime),
88
90
  getNumberFormat: intl_format_cache_1.default(Intl.NumberFormat, cache.number),
@@ -90,6 +92,7 @@ function createFormatters(cache) {
90
92
  getRelativeTimeFormat: intl_format_cache_1.default(RelativeTimeFormat, cache.relativeTime),
91
93
  getPluralRules: intl_format_cache_1.default(Intl.PluralRules, cache.pluralRules),
92
94
  getListFormat: intl_format_cache_1.default(ListFormat, cache.list),
95
+ getDisplayNames: intl_format_cache_1.default(DisplayNames, cache.displayNames),
93
96
  };
94
97
  }
95
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];
@@ -18,6 +18,9 @@ var DisplayName;
18
18
  DisplayName["formatTime"] = "FormattedTime";
19
19
  DisplayName["formatNumber"] = "FormattedNumber";
20
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";
21
24
  })(DisplayName || (DisplayName = {}));
22
25
  var DisplayNameParts;
23
26
  (function (DisplayNameParts) {
@@ -14,6 +14,7 @@ import { formatPlural } from '../formatters/plural';
14
14
  import { formatMessage, formatHTMLMessage } from '../formatters/message';
15
15
  import * as shallowEquals_ from 'shallow-equal/objects';
16
16
  import { formatList } from '../formatters/list';
17
+ import { formatDisplayName } from '../formatters/displayName';
17
18
  const shallowEquals = shallowEquals_.default || shallowEquals_;
18
19
  function processIntlConfig(config) {
19
20
  return {
@@ -49,7 +50,7 @@ export function createIntl(config, cache) {
49
50
  // each <FormattedMessage> contains a `defaultMessage` prop.
50
51
  resolvedConfig.locale = resolvedConfig.defaultLocale || 'en';
51
52
  }
52
- 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) });
53
+ 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
54
  }
54
55
  export default class IntlProvider extends React.PureComponent {
55
56
  constructor() {
@@ -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
+ }
package/lib/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export { defineMessages } from '@formatjs/macro';
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,6 +20,9 @@ export declare const FormattedNumber: React.FC<UnifiedNumberFormatOptions & Cust
19
20
  export declare const FormattedList: React.FC<IntlListFormatOptions & {
20
21
  value: React.ReactNode[];
21
22
  }>;
23
+ export declare const FormattedDisplayName: React.FC<DisplayNamesOptions & {
24
+ value: string | number | object;
25
+ }>;
22
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;
package/lib/index.js CHANGED
@@ -8,6 +8,7 @@ export const FormattedDate = createFormattedComponent('formatDate');
8
8
  export const FormattedTime = createFormattedComponent('formatTime');
9
9
  export const FormattedNumber = createFormattedComponent('formatNumber');
10
10
  export const FormattedList = createFormattedComponent('formatList');
11
+ export const FormattedDisplayName = createFormattedComponent('formatDisplayName');
11
12
  export const FormattedDateParts = createFormattedDateTimePartsComponent('formatDate');
12
13
  export const FormattedTimeParts = createFormattedDateTimePartsComponent('formatTime');
13
14
  export { FormattedNumberParts } from './components/createFormattedComponent';
@@ -1,4 +1,7 @@
1
1
  import { defineMessages } from '@formatjs/macro';
2
+ import { DisplayNames } from '@formatjs/intl-displaynames';
3
+ import { DisplayNamesOptions } from '@formatjs/intl-displaynames';
4
+ import { DisplayNamesOptions as DisplayNamesOptions_2 } from '@formatjs/intl-displaynames/lib';
2
5
  import { Formats } from 'intl-messageformat';
3
6
  import { FormatXMLElementFn } from 'intl-messageformat';
4
7
  import IntlListFormat from '@formatjs/intl-listformat';
@@ -34,6 +37,8 @@ export { defineMessages }
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;
@@ -51,6 +56,10 @@ export declare const FormattedDateParts: React.FC<Intl.DateTimeFormatOptions & C
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: {
@@ -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> {
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,6 +62,7 @@ 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
68
  id?: string | number;
package/lib/utils.js CHANGED
@@ -64,6 +64,7 @@ export function createIntlCache() {
64
64
  relativeTime: {},
65
65
  pluralRules: {},
66
66
  list: {},
67
+ displayNames: {},
67
68
  };
68
69
  }
69
70
  /**
@@ -73,6 +74,7 @@ export function createIntlCache() {
73
74
  export function createFormatters(cache = createIntlCache()) {
74
75
  const RelativeTimeFormat = Intl.RelativeTimeFormat;
75
76
  const ListFormat = Intl.ListFormat;
77
+ const DisplayNames = Intl.DisplayNames;
76
78
  return {
77
79
  getDateTimeFormat: memoizeIntlConstructor(Intl.DateTimeFormat, cache.dateTime),
78
80
  getNumberFormat: memoizeIntlConstructor(Intl.NumberFormat, cache.number),
@@ -80,6 +82,7 @@ export function createFormatters(cache = createIntlCache()) {
80
82
  getRelativeTimeFormat: memoizeIntlConstructor(RelativeTimeFormat, cache.relativeTime),
81
83
  getPluralRules: memoizeIntlConstructor(Intl.PluralRules, cache.pluralRules),
82
84
  getListFormat: memoizeIntlConstructor(ListFormat, cache.list),
85
+ getDisplayNames: memoizeIntlConstructor(DisplayNames, cache.displayNames),
83
86
  };
84
87
  }
85
88
  export function getNamedFormat(formats, type, name, onError) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-intl",
3
- "version": "3.11.0",
3
+ "version": "3.12.0",
4
4
  "description": "Internationalize React apps. This library provides React components and an API to format dates, numbers, and strings, including pluralization and handling translations.",
5
5
  "keywords": [
6
6
  "intl",
@@ -131,6 +131,7 @@
131
131
  "types": "./lib/react-intl.d.ts",
132
132
  "sideEffects": false,
133
133
  "dependencies": {
134
+ "@formatjs/intl-displaynames": "^1.2.0",
134
135
  "@formatjs/intl-listformat": "^1.3.7",
135
136
  "@formatjs/intl-relativetimeformat": "^4.5.7",
136
137
  "@formatjs/intl-unified-numberformat": "^3.0.4",
@@ -5,6 +5,7 @@ import {
5
5
  FormatDateOptions,
6
6
  FormatNumberOptions,
7
7
  FormatListOptions,
8
+ FormatDisplayNameOptions,
8
9
  } from '../types';
9
10
  import {Context} from './injectIntl';
10
11
 
@@ -13,6 +14,9 @@ enum DisplayName {
13
14
  formatTime = 'FormattedTime',
14
15
  formatNumber = 'FormattedNumber',
15
16
  formatList = 'FormattedList',
17
+ // Note that this DisplayName is the locale display name, not to be confused with
18
+ // the name of the enum, which is for React component display name in dev tools.
19
+ formatDisplayName = 'FormattedDisplayName',
16
20
  }
17
21
 
18
22
  enum DisplayNameParts {
@@ -27,6 +31,7 @@ type Formatter = {
27
31
  formatTime: FormatDateOptions;
28
32
  formatNumber: FormatNumberOptions;
29
33
  formatList: FormatListOptions;
34
+ formatDisplayName: FormatDisplayNameOptions;
30
35
  };
31
36
 
32
37
  export const FormattedNumberParts: React.FC<Formatter['formatNumber'] & {
@@ -27,6 +27,7 @@ import {formatPlural} from '../formatters/plural';
27
27
  import {formatMessage, formatHTMLMessage} from '../formatters/message';
28
28
  import * as shallowEquals_ from 'shallow-equal/objects';
29
29
  import {formatList} from '../formatters/list';
30
+ import {formatDisplayName} from '../formatters/displayName';
30
31
  const shallowEquals: typeof shallowEquals_ =
31
32
  (shallowEquals_ as any).default || shallowEquals_;
32
33
 
@@ -146,6 +147,11 @@ export function createIntl(
146
147
  formatMessage: formatMessage.bind(null, resolvedConfig, formatters),
147
148
  formatHTMLMessage: formatHTMLMessage.bind(null, resolvedConfig, formatters),
148
149
  formatList: formatList.bind(null, resolvedConfig, formatters.getListFormat),
150
+ formatDisplayName: formatDisplayName.bind(
151
+ null,
152
+ resolvedConfig,
153
+ formatters.getDisplayNames
154
+ ),
149
155
  };
150
156
  }
151
157
 
@@ -0,0 +1,35 @@
1
+ import {IntlConfig, Formatters, IntlFormatters} from '../types';
2
+ import {filterProps, createError} from '../utils';
3
+ import {
4
+ DisplayNamesOptions,
5
+ DisplayNames as IntlDisplayNames,
6
+ } from '@formatjs/intl-displaynames';
7
+
8
+ const DISPLAY_NAMES_OPTONS: Array<keyof DisplayNamesOptions> = [
9
+ 'localeMatcher',
10
+ 'style',
11
+ 'type',
12
+ 'fallback',
13
+ ];
14
+
15
+ export function formatDisplayName(
16
+ {locale, onError}: Pick<IntlConfig, 'locale' | 'onError'>,
17
+ getDisplayNames: Formatters['getDisplayNames'],
18
+ value: Parameters<IntlFormatters['formatDisplayName']>[0],
19
+ options: Parameters<IntlFormatters['formatDisplayName']>[1] = {}
20
+ ): string | undefined {
21
+ const DisplayNames: typeof IntlDisplayNames = (Intl as any).DisplayNames;
22
+ if (!DisplayNames) {
23
+ onError(
24
+ createError(`Intl.DisplayNames is not available in this environment.
25
+ Try polyfilling it using "@formatjs/intl-displaynames"
26
+ `)
27
+ );
28
+ }
29
+ const filteredOptions = filterProps(options, DISPLAY_NAMES_OPTONS);
30
+ try {
31
+ return getDisplayNames(locale, filteredOptions).of(value);
32
+ } catch (e) {
33
+ onError(createError('Error formatting display name.', e));
34
+ }
35
+ }
package/src/index.ts CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  import {CustomFormatConfig} from './types';
14
14
  import {UnifiedNumberFormatOptions} from '@formatjs/intl-unified-numberformat';
15
15
  import {IntlListFormatOptions} from '@formatjs/intl-listformat';
16
+ import {DisplayNamesOptions} from '@formatjs/intl-displaynames/lib';
16
17
  export {
17
18
  default as injectIntl,
18
19
  Provider as RawIntlProvider,
@@ -38,6 +39,9 @@ export const FormattedNumber: React.FC<UnifiedNumberFormatOptions &
38
39
  export const FormattedList: React.FC<IntlListFormatOptions & {
39
40
  value: React.ReactNode[];
40
41
  }> = createFormattedComponent('formatList');
42
+ export const FormattedDisplayName: React.FC<DisplayNamesOptions & {
43
+ value: string | number | object;
44
+ }> = createFormattedComponent('formatDisplayName');
41
45
  export const FormattedDateParts = createFormattedDateTimePartsComponent(
42
46
  'formatDate'
43
47
  );
package/src/types.ts CHANGED
@@ -15,6 +15,7 @@ import IntlRelativeTimeFormat, {
15
15
  import {MessageFormatElement} from 'intl-messageformat-parser';
16
16
  import {UnifiedNumberFormatOptions} from '@formatjs/intl-unified-numberformat';
17
17
  import IntlListFormat, {IntlListFormatOptions} from '@formatjs/intl-listformat';
18
+ import {DisplayNames, DisplayNamesOptions} from '@formatjs/intl-displaynames';
18
19
 
19
20
  export interface IntlConfig {
20
21
  locale: string;
@@ -58,6 +59,11 @@ export type FormatPluralOptions = Exclude<
58
59
 
59
60
  export type FormatListOptions = Exclude<IntlListFormatOptions, 'localeMatcher'>;
60
61
 
62
+ export type FormatDisplayNameOptions = Exclude<
63
+ DisplayNamesOptions,
64
+ 'localeMatcher'
65
+ >;
66
+
61
67
  export interface IntlFormatters {
62
68
  formatDate(
63
69
  value: Parameters<Intl.DateTimeFormat['format']>[0] | string,
@@ -107,14 +113,15 @@ export interface IntlFormatters {
107
113
  descriptor: MessageDescriptor,
108
114
  values?: Record<string, PrimitiveType>
109
115
  ): React.ReactNode;
110
- formatList(
111
- values: Array<string>,
112
- opts?: FormatListOptions
113
- ): string;
116
+ formatList(values: Array<string>, opts?: FormatListOptions): string;
114
117
  formatList(
115
118
  values: Array<string | React.ReactNode>,
116
119
  opts?: FormatListOptions
117
120
  ): React.ReactNode;
121
+ formatDisplayName(
122
+ value: Parameters<DisplayNames['of']>[0],
123
+ opts?: FormatDisplayNameOptions
124
+ ): string | undefined;
118
125
  }
119
126
 
120
127
  export interface Formatters {
@@ -136,6 +143,9 @@ export interface Formatters {
136
143
  getListFormat(
137
144
  ...args: ConstructorParameters<typeof IntlListFormat>
138
145
  ): IntlListFormat;
146
+ getDisplayNames(
147
+ ...args: ConstructorParameters<typeof DisplayNames>
148
+ ): DisplayNames;
139
149
  }
140
150
 
141
151
  export interface IntlShape extends IntlConfig, IntlFormatters {
@@ -149,6 +159,7 @@ export interface IntlCache {
149
159
  relativeTime: Record<string, IntlRelativeTimeFormat>;
150
160
  pluralRules: Record<string, Intl.PluralRules>;
151
161
  list: Record<string, IntlListFormat>;
162
+ displayNames: Record<string, DisplayNames>;
152
163
  }
153
164
 
154
165
  export interface MessageDescriptor {
package/src/utils.ts CHANGED
@@ -13,7 +13,7 @@ import {IntlConfig, IntlCache, CustomFormats, Formatters} from './types';
13
13
  import * as React from 'react';
14
14
  import IntlMessageFormat from 'intl-messageformat';
15
15
  import memoizeIntlConstructor from 'intl-format-cache';
16
- import {invariant} from '@formatjs/intl-utils'
16
+ import {invariant} from '@formatjs/intl-utils';
17
17
  import {IntlRelativeTimeFormatOptions} from '@formatjs/intl-relativetimeformat';
18
18
 
19
19
  const ESCAPED_CHARS: Record<number, string> = {
@@ -97,6 +97,7 @@ export function createIntlCache(): IntlCache {
97
97
  relativeTime: {},
98
98
  pluralRules: {},
99
99
  list: {},
100
+ displayNames: {},
100
101
  };
101
102
  }
102
103
 
@@ -109,6 +110,7 @@ export function createFormatters(
109
110
  ): Formatters {
110
111
  const RelativeTimeFormat = (Intl as any).RelativeTimeFormat;
111
112
  const ListFormat = (Intl as any).ListFormat;
113
+ const DisplayNames = (Intl as any).DisplayNames;
112
114
  return {
113
115
  getDateTimeFormat: memoizeIntlConstructor(
114
116
  Intl.DateTimeFormat,
@@ -122,6 +124,7 @@ export function createFormatters(
122
124
  ),
123
125
  getPluralRules: memoizeIntlConstructor(Intl.PluralRules, cache.pluralRules),
124
126
  getListFormat: memoizeIntlConstructor(ListFormat, cache.list),
127
+ getDisplayNames: memoizeIntlConstructor(DisplayNames, cache.displayNames),
125
128
  };
126
129
  }
127
130