use-intl 2.10.0-alpha.3 → 2.10.0-alpha.4

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 (76) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/core/use-intl.esm.js +35 -151
  3. package/dist/core/use-intl.esm.js.map +1 -1
  4. package/dist/core/use-intl.esm3.js +151 -35
  5. package/dist/core/use-intl.esm3.js.map +1 -1
  6. package/dist/core/use-intl.esm5.js +2 -2
  7. package/dist/core/use-intl.esm5.js.map +1 -1
  8. package/dist/core/use-intl.esm6.js +29 -7
  9. package/dist/core/use-intl.esm6.js.map +1 -1
  10. package/dist/core/use-intl.esm7.js +7 -7
  11. package/dist/core/use-intl.esm7.js.map +1 -1
  12. package/dist/core/use-intl.esm8.js +7 -29
  13. package/dist/core/use-intl.esm8.js.map +1 -1
  14. package/dist/react/IntlContext.d.ts +19 -2
  15. package/dist/react/IntlProvider.d.ts +16 -2
  16. package/dist/react/use-intl.esm.js +26 -7
  17. package/dist/react/use-intl.esm.js.map +1 -1
  18. package/dist/react/use-intl.esm2.js +44 -19
  19. package/dist/react/use-intl.esm2.js.map +1 -1
  20. package/dist/react/use-intl.esm3.js +2 -2
  21. package/dist/react/use-intl.esm4.js +19 -44
  22. package/dist/react/use-intl.esm4.js.map +1 -1
  23. package/dist/react/use-intl.esm5.js +4 -4
  24. package/dist/react/use-intl.esm5.js.map +1 -1
  25. package/dist/react/use-intl.esm6.js +4 -4
  26. package/dist/react/use-intl.esm6.js.map +1 -1
  27. package/dist/react/use-intl.esm7.js +12 -3
  28. package/dist/react/use-intl.esm7.js.map +1 -1
  29. package/dist/react/use-intl.esm8.js +38 -27
  30. package/dist/react/use-intl.esm8.js.map +1 -1
  31. package/dist/react/use-intl.esm9.js +3 -12
  32. package/dist/react/use-intl.esm9.js.map +1 -1
  33. package/dist/react/useIntlContext.d.ts +1 -1
  34. package/dist/src/core/createBaseTranslator.js +7 -11
  35. package/dist/src/core/createBaseTranslator.js.map +1 -1
  36. package/dist/src/core/createIntl.js +2 -2
  37. package/dist/src/core/createIntl.js.map +1 -1
  38. package/dist/src/core/createTranslatorImpl.js +1 -1
  39. package/dist/src/core/createTranslatorImpl.js.map +1 -1
  40. package/dist/src/react/IntlContext.d.ts +19 -2
  41. package/dist/src/react/IntlContext.js.map +1 -1
  42. package/dist/src/react/IntlProvider.d.ts +16 -2
  43. package/dist/src/react/IntlProvider.js +13 -4
  44. package/dist/src/react/IntlProvider.js.map +1 -1
  45. package/dist/src/react/useIntlContext.d.ts +1 -1
  46. package/dist/src/react/useIntlContext.js +1 -1
  47. package/dist/src/react/useIntlContext.js.map +1 -1
  48. package/dist/use-intl.cjs.development.js +28 -37
  49. package/dist/use-intl.cjs.development.js.map +1 -1
  50. package/dist/use-intl.cjs.production.min.js +1 -1
  51. package/dist/use-intl.cjs.production.min.js.map +1 -1
  52. package/dist/use-intl.esm.js +6 -6
  53. package/package.json +2 -2
  54. package/src/core/createBaseTranslator.tsx +7 -11
  55. package/src/core/createIntl.tsx +2 -2
  56. package/src/core/createTranslatorImpl.tsx +1 -1
  57. package/src/react/IntlContext.tsx +20 -2
  58. package/src/react/IntlProvider.tsx +36 -5
  59. package/src/react/useIntlContext.tsx +1 -1
  60. package/dist/react/IntlContextValue.d.ts +0 -19
  61. package/dist/react/IntlProviderProps.d.ts +0 -15
  62. package/dist/react/getIntlContextValue.d.ts +0 -18
  63. package/dist/react/use-intl.esm10.js +0 -44
  64. package/dist/react/use-intl.esm10.js.map +0 -1
  65. package/dist/src/react/IntlContextValue.d.ts +0 -19
  66. package/dist/src/react/IntlContextValue.js +0 -2
  67. package/dist/src/react/IntlContextValue.js.map +0 -1
  68. package/dist/src/react/IntlProviderProps.d.ts +0 -15
  69. package/dist/src/react/IntlProviderProps.js +0 -2
  70. package/dist/src/react/IntlProviderProps.js.map +0 -1
  71. package/dist/src/react/getIntlContextValue.d.ts +0 -18
  72. package/dist/src/react/getIntlContextValue.js +0 -21
  73. package/dist/src/react/getIntlContextValue.js.map +0 -1
  74. package/src/react/IntlContextValue.tsx +0 -21
  75. package/src/react/IntlProviderProps.tsx +0 -17
  76. package/src/react/getIntlContextValue.tsx +0 -30
package/CHANGELOG.md CHANGED
@@ -3,6 +3,14 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## 2.9.2 (2022-12-08)
7
+
8
+ **Note:** Version bump only for package use-intl
9
+
10
+
11
+
12
+
13
+
6
14
  ## 2.9.1 (2022-11-03)
7
15
 
8
16
  **Note:** Version bump only for package use-intl
@@ -1,155 +1,39 @@
1
- import { extends as _extends } from '../_virtual/use-intl.esm.js';
2
- import IntlError, { IntlErrorCode } from './use-intl.esm2.js';
3
- import { defaultOnError } from './use-intl.esm4.js';
4
-
5
- var MINUTE = 60;
6
- var HOUR = MINUTE * 60;
7
- var DAY = HOUR * 24;
8
- var WEEK = DAY * 7;
9
- var MONTH = DAY * (365 / 12); // Approximation
10
-
11
- var YEAR = DAY * 365;
12
-
13
- function getRelativeTimeFormatConfig(seconds) {
14
- var absValue = Math.abs(seconds);
15
- var value, unit; // We have to round the resulting values, as `Intl.RelativeTimeFormat`
16
- // will include fractions like '2.1 hours ago'.
17
-
18
- if (absValue < MINUTE) {
19
- unit = 'second';
20
- value = Math.round(seconds);
21
- } else if (absValue < HOUR) {
22
- unit = 'minute';
23
- value = Math.round(seconds / MINUTE);
24
- } else if (absValue < DAY) {
25
- unit = 'hour';
26
- value = Math.round(seconds / HOUR);
27
- } else if (absValue < WEEK) {
28
- unit = 'day';
29
- value = Math.round(seconds / DAY);
30
- } else if (absValue < MONTH) {
31
- unit = 'week';
32
- value = Math.round(seconds / WEEK);
33
- } else if (absValue < YEAR) {
34
- unit = 'month';
35
- value = Math.round(seconds / MONTH);
36
- } else {
37
- unit = 'year';
38
- value = Math.round(seconds / YEAR);
39
- }
40
-
41
- return {
42
- value: value,
43
- unit: unit
44
- };
45
- }
46
-
47
- function createIntl(_ref) {
48
- var formats = _ref.formats,
49
- locale = _ref.locale,
50
- globalNow = _ref.now,
51
- _ref$onError = _ref.onError,
1
+ import { objectWithoutPropertiesLoose as _objectWithoutPropertiesLoose, extends as _extends } from '../_virtual/use-intl.esm.js';
2
+ import createTranslatorImpl from './use-intl.esm5.js';
3
+ import { defaultOnError, defaultGetMessageFallback } from './use-intl.esm4.js';
4
+
5
+ var _excluded = ["onError", "getMessageFallback", "messages", "namespace"];
6
+ /**
7
+ * Translates messages from the given namespace by using the ICU syntax.
8
+ * See https://formatjs.io/docs/core-concepts/icu-syntax.
9
+ *
10
+ * If no namespace is provided, all available messages are returned.
11
+ * The namespace can also indicate nesting by using a dot
12
+ * (e.g. `namespace.Component`).
13
+ */
14
+
15
+ function createTranslator(_ref) {
16
+ var _ref$onError = _ref.onError,
52
17
  onError = _ref$onError === void 0 ? defaultOnError : _ref$onError,
53
- timeZone = _ref.timeZone;
54
-
55
- function resolveFormatOrOptions(typeFormats, formatOrOptions) {
56
- var options;
57
-
58
- if (typeof formatOrOptions === 'string') {
59
- var formatName = formatOrOptions;
60
- options = typeFormats == null ? void 0 : typeFormats[formatName];
61
-
62
- if (!options) {
63
- var error = new IntlError(IntlErrorCode.MISSING_FORMAT, process.env.NODE_ENV !== 'production' ? "Format `" + formatName + "` is not available. You can configure it on the provider or provide custom options." : undefined);
64
- onError(error);
65
- throw error;
66
- }
67
- } else {
68
- options = formatOrOptions;
69
- }
70
-
71
- return options;
72
- }
73
-
74
- function getFormattedValue(value, formatOrOptions, typeFormats, formatter) {
75
- var options;
76
-
77
- try {
78
- options = resolveFormatOrOptions(typeFormats, formatOrOptions);
79
- } catch (error) {
80
- return String(value);
81
- }
82
-
83
- try {
84
- return formatter(options);
85
- } catch (error) {
86
- onError(new IntlError(IntlErrorCode.FORMATTING_ERROR, error.message));
87
- return String(value);
88
- }
89
- }
90
-
91
- function formatDateTime(
92
- /** If a number is supplied, this is interpreted as a UTC timestamp. */
93
- value,
94
- /** If a time zone is supplied, the `value` is converted to that time zone.
95
- * Otherwise the user time zone will be used. */
96
- formatOrOptions) {
97
- return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.dateTime, function (options) {
98
- var _options;
99
-
100
- if (timeZone && !((_options = options) != null && _options.timeZone)) {
101
- options = _extends({}, options, {
102
- timeZone: timeZone
103
- });
104
- }
105
-
106
- return new Intl.DateTimeFormat(locale, options).format(value);
107
- });
108
- }
109
-
110
- function formatNumber(value, formatOrOptions) {
111
- return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.number, function (options) {
112
- return new Intl.NumberFormat(locale, options).format(value);
113
- });
114
- }
115
-
116
- function formatRelativeTime(
117
- /** The date time that needs to be formatted. */
118
- date,
119
- /** The reference point in time to which `date` will be formatted in relation to. */
120
- now) {
121
- try {
122
- if (!now) {
123
- if (globalNow) {
124
- now = globalNow;
125
- } else {
126
- throw new Error(process.env.NODE_ENV !== 'production' ? "The `now` parameter wasn't provided to `formatRelativeTime` and there was no global fallback configured on the provider." : undefined);
127
- }
128
- }
129
-
130
- var dateDate = date instanceof Date ? date : new Date(date);
131
- var nowDate = now instanceof Date ? now : new Date(now);
132
- var seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
133
-
134
- var _getRelativeTimeForma = getRelativeTimeFormatConfig(seconds),
135
- unit = _getRelativeTimeForma.unit,
136
- value = _getRelativeTimeForma.value;
137
-
138
- return new Intl.RelativeTimeFormat(locale, {
139
- numeric: 'auto'
140
- }).format(value, unit);
141
- } catch (error) {
142
- onError(new IntlError(IntlErrorCode.FORMATTING_ERROR, error.message));
143
- return String(date);
144
- }
145
- }
146
-
147
- return {
148
- formatDateTime: formatDateTime,
149
- formatNumber: formatNumber,
150
- formatRelativeTime: formatRelativeTime
151
- };
18
+ _ref$getMessageFallba = _ref.getMessageFallback,
19
+ getMessageFallback = _ref$getMessageFallba === void 0 ? defaultGetMessageFallback : _ref$getMessageFallba,
20
+ messages = _ref.messages,
21
+ namespace = _ref.namespace,
22
+ rest = _objectWithoutPropertiesLoose(_ref, _excluded);
23
+
24
+ // We have to wrap the actual function so the type inference for the optional
25
+ // namespace works correctly. See https://stackoverflow.com/a/71529575/343045
26
+ // The prefix ("!") is arbitrary.
27
+ return createTranslatorImpl(_extends({}, rest, {
28
+ onError: onError,
29
+ getMessageFallback: getMessageFallback,
30
+ messages: {
31
+ '!': messages
32
+ },
33
+ // @ts-ignore
34
+ namespace: namespace ? "!." + namespace : '!'
35
+ }), '!');
152
36
  }
153
37
 
154
- export { createIntl as default };
38
+ export { createTranslator as default };
155
39
  //# sourceMappingURL=use-intl.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm.js","sources":["../../src/core/createIntl.tsx"],"sourcesContent":["import DateTimeFormatOptions from './DateTimeFormatOptions';\nimport Formats from './Formats';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport NumberFormatOptions from './NumberFormatOptions';\nimport {defaultOnError} from './defaults';\n\nconst MINUTE = 60;\nconst HOUR = MINUTE * 60;\nconst DAY = HOUR * 24;\nconst WEEK = DAY * 7;\nconst MONTH = DAY * (365 / 12); // Approximation\nconst YEAR = DAY * 365;\n\nfunction getRelativeTimeFormatConfig(seconds: number) {\n const absValue = Math.abs(seconds);\n let value, unit: Intl.RelativeTimeFormatUnit;\n\n // We have to round the resulting values, as `Intl.RelativeTimeFormat`\n // will include fractions like '2.1 hours ago'.\n\n if (absValue < MINUTE) {\n unit = 'second';\n value = Math.round(seconds);\n } else if (absValue < HOUR) {\n unit = 'minute';\n value = Math.round(seconds / MINUTE);\n } else if (absValue < DAY) {\n unit = 'hour';\n value = Math.round(seconds / HOUR);\n } else if (absValue < WEEK) {\n unit = 'day';\n value = Math.round(seconds / DAY);\n } else if (absValue < MONTH) {\n unit = 'week';\n value = Math.round(seconds / WEEK);\n } else if (absValue < YEAR) {\n unit = 'month';\n value = Math.round(seconds / MONTH);\n } else {\n unit = 'year';\n value = Math.round(seconds / YEAR);\n }\n\n return {value, unit};\n}\n\ntype Props = {\n locale: string;\n timeZone?: string;\n onError?(error: IntlError): void;\n formats?: Partial<Formats>;\n now?: Date;\n};\n\nexport default function createIntl({\n formats,\n locale,\n now: globalNow,\n onError = defaultOnError,\n timeZone\n}: Props) {\n function resolveFormatOrOptions<Options>(\n typeFormats: Record<string, Options> | undefined,\n formatOrOptions?: string | Options\n ) {\n let options;\n if (typeof formatOrOptions === 'string') {\n const formatName = formatOrOptions;\n options = typeFormats?.[formatName];\n\n if (!options) {\n const error = new IntlError(\n IntlErrorCode.MISSING_FORMAT,\n process.env.NODE_ENV !== 'production'\n ? `Format \\`${formatName}\\` is not available. You can configure it on the provider or provide custom options.`\n : undefined\n );\n onError(error);\n throw error;\n }\n } else {\n options = formatOrOptions;\n }\n\n return options;\n }\n\n function getFormattedValue<Value, Options>(\n value: Value,\n formatOrOptions: string | Options | undefined,\n typeFormats: Record<string, Options> | undefined,\n formatter: (options?: Options) => string\n ) {\n let options;\n try {\n options = resolveFormatOrOptions(typeFormats, formatOrOptions);\n } catch (error) {\n return String(value);\n }\n\n try {\n return formatter(options);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(value);\n }\n }\n\n function formatDateTime(\n /** If a number is supplied, this is interpreted as a UTC timestamp. */\n value: Date | number,\n /** If a time zone is supplied, the `value` is converted to that time zone.\n * Otherwise the user time zone will be used. */\n formatOrOptions?: string | DateTimeFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.dateTime,\n (options) => {\n if (timeZone && !options?.timeZone) {\n options = {...options, timeZone};\n }\n\n return new Intl.DateTimeFormat(locale, options).format(value);\n }\n );\n }\n\n function formatNumber(\n value: number,\n formatOrOptions?: string | NumberFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.number,\n (options) => new Intl.NumberFormat(locale, options).format(value)\n );\n }\n\n function formatRelativeTime(\n /** The date time that needs to be formatted. */\n date: number | Date,\n /** The reference point in time to which `date` will be formatted in relation to. */\n now?: number | Date\n ) {\n try {\n if (!now) {\n if (globalNow) {\n now = globalNow;\n } else {\n throw new Error(\n process.env.NODE_ENV !== 'production'\n ? `The \\`now\\` parameter wasn't provided to \\`formatRelativeTime\\` and there was no global fallback configured on the provider.`\n : undefined\n );\n }\n }\n\n const dateDate = date instanceof Date ? date : new Date(date);\n const nowDate = now instanceof Date ? now : new Date(now);\n\n const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;\n const {unit, value} = getRelativeTimeFormatConfig(seconds);\n\n return new Intl.RelativeTimeFormat(locale, {\n numeric: 'auto'\n }).format(value, unit);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(date);\n }\n }\n\n return {formatDateTime, formatNumber, formatRelativeTime};\n}\n"],"names":["MINUTE","HOUR","DAY","WEEK","MONTH","YEAR","getRelativeTimeFormatConfig","seconds","absValue","Math","abs","value","unit","round","createIntl","formats","locale","globalNow","now","onError","defaultOnError","timeZone","resolveFormatOrOptions","typeFormats","formatOrOptions","options","formatName","error","IntlError","IntlErrorCode","MISSING_FORMAT","process","env","NODE_ENV","undefined","getFormattedValue","formatter","String","FORMATTING_ERROR","message","formatDateTime","dateTime","Intl","DateTimeFormat","format","formatNumber","number","NumberFormat","formatRelativeTime","date","Error","dateDate","Date","nowDate","getTime","RelativeTimeFormat","numeric"],"mappings":";;;;AAMA,IAAMA,MAAM,GAAG,EAAf,CAAA;AACA,IAAMC,IAAI,GAAGD,MAAM,GAAG,EAAtB,CAAA;AACA,IAAME,GAAG,GAAGD,IAAI,GAAG,EAAnB,CAAA;AACA,IAAME,IAAI,GAAGD,GAAG,GAAG,CAAnB,CAAA;AACA,IAAME,KAAK,GAAGF,GAAG,IAAI,MAAM,EAAV,CAAjB;;AACA,IAAMG,IAAI,GAAGH,GAAG,GAAG,GAAnB,CAAA;;AAEA,SAASI,2BAAT,CAAqCC,OAArC,EAA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ACa,kBAAT,CAA4BvC,MAA5B,EAAoC;AACzCwC,QAAAA,OAAO,EAAE,MAAA;AADgC,OAApC,EAEJZ,MAFI,CAEGjC,KAFH,EAEUC,IAFV,CAAP,CAAA;AAGD,KAtBD,CAsBE,OAAOe,KAAP,EAAc;AACdR,MAAAA,OAAO,CACL,IAAIS,SAAJ,CAAcC,aAAa,CAACS,gBAA5B,EAA+CX,KAAe,CAACY,OAA/D,CADK,CAAP,CAAA;AAGA,MAAOF,OAAAA,MAAM,CAACY,IAAD,CAAb,CAAA;AACD,KAAA;AACF,GAAA;;AAED,EAAO,OAAA;AAACT,IAAAA,cAAc,EAAdA,cAAD;AAAiBK,IAAAA,YAAY,EAAZA,YAAjB;AAA+BG,IAAAA,kBAAkB,EAAlBA,kBAAAA;AAA/B,GAAP,CAAA;AACD;;;;"}
1
+ {"version":3,"file":"use-intl.esm.js","sources":["../../src/core/createTranslator.tsx"],"sourcesContent":["import Formats from './Formats';\nimport IntlConfiguration from './IntlConfiguration';\nimport TranslationValues from './TranslationValues';\nimport createTranslatorImpl, {\n CoreRichTranslationValues\n} from './createTranslatorImpl';\nimport {defaultGetMessageFallback, defaultOnError} from './defaults';\nimport MessageKeys from './utils/MessageKeys';\nimport NamespaceKeys from './utils/NamespaceKeys';\nimport NestedKeyOf from './utils/NestedKeyOf';\nimport NestedValueOf from './utils/NestedValueOf';\n\n/**\n * Translates messages from the given namespace by using the ICU syntax.\n * See https://formatjs.io/docs/core-concepts/icu-syntax.\n *\n * If no namespace is provided, all available messages are returned.\n * The namespace can also indicate nesting by using a dot\n * (e.g. `namespace.Component`).\n */\nexport default function createTranslator<\n NestedKey extends NamespaceKeys<\n IntlMessages,\n NestedKeyOf<IntlMessages>\n > = never\n>({\n onError = defaultOnError,\n getMessageFallback = defaultGetMessageFallback,\n messages,\n namespace,\n ...rest\n}: IntlConfiguration & {\n messages: IntlMessages;\n namespace?: NestedKey;\n}): // Explicitly defining the return type is necessary as TypeScript would get it wrong\n{\n // Default invocation\n <\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: TranslationValues,\n formats?: Partial<Formats>\n ): string;\n\n // `rich`\n rich<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: CoreRichTranslationValues,\n formats?: Partial<Formats>\n ): string;\n\n // `raw`\n raw<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey\n ): any;\n} {\n // We have to wrap the actual function so the type inference for the optional\n // namespace works correctly. See https://stackoverflow.com/a/71529575/343045\n // The prefix (\"!\") is arbitrary.\n return createTranslatorImpl<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >(\n {\n ...rest,\n onError,\n getMessageFallback,\n messages: {'!': messages},\n // @ts-ignore\n namespace: namespace ? `!.${namespace}` : '!'\n },\n '!'\n );\n}\n"],"names":["createTranslator","onError","defaultOnError","getMessageFallback","defaultGetMessageFallback","messages","namespace","rest","createTranslatorImpl"],"mappings":";;;;;AAYA;;;;;;;AAOG;;AACqB,SAAAA,gBAAA,CAcvB,IAAA,EAAA;AAAA,EAAA,IAAA,YAAA,GAAA,IAAA,CARCC,OAQD;AAAA,MARCA,OAQD,6BARWC,cAQX,GAAA,YAAA;AAAA,MAAA,qBAAA,GAAA,IAAA,CAPCC,kBAOD;AAAA,MAPCA,kBAOD,sCAPsBC,yBAOtB,GAAA,qBAAA;AAAA,MANCC,QAMD,QANCA,QAMD;AAAA,MALCC,SAKD,QALCA,SAKD;AAAA,MAJIC,IAIJ,GAAA,6BAAA,CAAA,IAAA,EAAA,SAAA,CAAA,CAAA;;AA4DC;AACA;AACA;AACA,EAAOC,OAAAA,oBAAoB,cAKpBD,IALoB,EAAA;AAMvBN,IAAAA,OAAO,EAAPA,OANuB;AAOvBE,IAAAA,kBAAkB,EAAlBA,kBAPuB;AAQvBE,IAAAA,QAAQ,EAAE;AAAC,MAAKA,GAAAA,EAAAA,QAAAA;AAAN,KARa;AASvB;AACAC,IAAAA,SAAS,EAAEA,SAAS,GAAA,IAAA,GAAQA,SAAR,GAAsB,GAAA;AAVnB,GAAA,CAAA,EAYzB,GAZyB,CAA3B,CAAA;AAcD;;;;"}
@@ -1,39 +1,155 @@
1
- import { objectWithoutPropertiesLoose as _objectWithoutPropertiesLoose, extends as _extends } from '../_virtual/use-intl.esm.js';
2
- import createTranslatorImpl from './use-intl.esm5.js';
3
- import { defaultOnError, defaultGetMessageFallback } from './use-intl.esm4.js';
4
-
5
- var _excluded = ["onError", "getMessageFallback", "messages", "namespace"];
6
- /**
7
- * Translates messages from the given namespace by using the ICU syntax.
8
- * See https://formatjs.io/docs/core-concepts/icu-syntax.
9
- *
10
- * If no namespace is provided, all available messages are returned.
11
- * The namespace can also indicate nesting by using a dot
12
- * (e.g. `namespace.Component`).
13
- */
14
-
15
- function createTranslator(_ref) {
16
- var _ref$onError = _ref.onError,
1
+ import { extends as _extends } from '../_virtual/use-intl.esm.js';
2
+ import IntlError, { IntlErrorCode } from './use-intl.esm2.js';
3
+ import { defaultOnError } from './use-intl.esm4.js';
4
+
5
+ var MINUTE = 60;
6
+ var HOUR = MINUTE * 60;
7
+ var DAY = HOUR * 24;
8
+ var WEEK = DAY * 7;
9
+ var MONTH = DAY * (365 / 12); // Approximation
10
+
11
+ var YEAR = DAY * 365;
12
+
13
+ function getRelativeTimeFormatConfig(seconds) {
14
+ var absValue = Math.abs(seconds);
15
+ var value, unit; // We have to round the resulting values, as `Intl.RelativeTimeFormat`
16
+ // will include fractions like '2.1 hours ago'.
17
+
18
+ if (absValue < MINUTE) {
19
+ unit = 'second';
20
+ value = Math.round(seconds);
21
+ } else if (absValue < HOUR) {
22
+ unit = 'minute';
23
+ value = Math.round(seconds / MINUTE);
24
+ } else if (absValue < DAY) {
25
+ unit = 'hour';
26
+ value = Math.round(seconds / HOUR);
27
+ } else if (absValue < WEEK) {
28
+ unit = 'day';
29
+ value = Math.round(seconds / DAY);
30
+ } else if (absValue < MONTH) {
31
+ unit = 'week';
32
+ value = Math.round(seconds / WEEK);
33
+ } else if (absValue < YEAR) {
34
+ unit = 'month';
35
+ value = Math.round(seconds / MONTH);
36
+ } else {
37
+ unit = 'year';
38
+ value = Math.round(seconds / YEAR);
39
+ }
40
+
41
+ return {
42
+ value: value,
43
+ unit: unit
44
+ };
45
+ }
46
+
47
+ function createIntl(_ref) {
48
+ var formats = _ref.formats,
49
+ locale = _ref.locale,
50
+ globalNow = _ref.now,
51
+ _ref$onError = _ref.onError,
17
52
  onError = _ref$onError === void 0 ? defaultOnError : _ref$onError,
18
- _ref$getMessageFallba = _ref.getMessageFallback,
19
- getMessageFallback = _ref$getMessageFallba === void 0 ? defaultGetMessageFallback : _ref$getMessageFallba,
20
- messages = _ref.messages,
21
- namespace = _ref.namespace,
22
- rest = _objectWithoutPropertiesLoose(_ref, _excluded);
23
-
24
- // We have to wrap the actual function so the type inference for the optional
25
- // namespace works correctly. See https://stackoverflow.com/a/71529575/343045
26
- // The prefix ("!") is arbitrary.
27
- return createTranslatorImpl(_extends({}, rest, {
28
- onError: onError,
29
- getMessageFallback: getMessageFallback,
30
- messages: {
31
- '!': messages
32
- },
33
- // @ts-ignore
34
- namespace: namespace ? "!." + namespace : '!'
35
- }), '!');
53
+ timeZone = _ref.timeZone;
54
+
55
+ function resolveFormatOrOptions(typeFormats, formatOrOptions) {
56
+ var options;
57
+
58
+ if (typeof formatOrOptions === 'string') {
59
+ var formatName = formatOrOptions;
60
+ options = typeFormats == null ? void 0 : typeFormats[formatName];
61
+
62
+ if (!options) {
63
+ var error = new IntlError(IntlErrorCode.MISSING_FORMAT, process.env.NODE_ENV !== "production" ? "Format `" + formatName + "` is not available. You can configure it on the provider or provide custom options." : undefined);
64
+ onError(error);
65
+ throw error;
66
+ }
67
+ } else {
68
+ options = formatOrOptions;
69
+ }
70
+
71
+ return options;
72
+ }
73
+
74
+ function getFormattedValue(value, formatOrOptions, typeFormats, formatter) {
75
+ var options;
76
+
77
+ try {
78
+ options = resolveFormatOrOptions(typeFormats, formatOrOptions);
79
+ } catch (error) {
80
+ return String(value);
81
+ }
82
+
83
+ try {
84
+ return formatter(options);
85
+ } catch (error) {
86
+ onError(new IntlError(IntlErrorCode.FORMATTING_ERROR, error.message));
87
+ return String(value);
88
+ }
89
+ }
90
+
91
+ function formatDateTime(
92
+ /** If a number is supplied, this is interpreted as a UTC timestamp. */
93
+ value,
94
+ /** If a time zone is supplied, the `value` is converted to that time zone.
95
+ * Otherwise the user time zone will be used. */
96
+ formatOrOptions) {
97
+ return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.dateTime, function (options) {
98
+ var _options;
99
+
100
+ if (timeZone && !((_options = options) != null && _options.timeZone)) {
101
+ options = _extends({}, options, {
102
+ timeZone: timeZone
103
+ });
104
+ }
105
+
106
+ return new Intl.DateTimeFormat(locale, options).format(value);
107
+ });
108
+ }
109
+
110
+ function formatNumber(value, formatOrOptions) {
111
+ return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.number, function (options) {
112
+ return new Intl.NumberFormat(locale, options).format(value);
113
+ });
114
+ }
115
+
116
+ function formatRelativeTime(
117
+ /** The date time that needs to be formatted. */
118
+ date,
119
+ /** The reference point in time to which `date` will be formatted in relation to. */
120
+ now) {
121
+ try {
122
+ if (!now) {
123
+ if (globalNow) {
124
+ now = globalNow;
125
+ } else {
126
+ throw new Error(process.env.NODE_ENV !== "production" ? "The `now` parameter wasn't provided to `formatRelativeTime` and there was no global fallback configured on the provider." : undefined);
127
+ }
128
+ }
129
+
130
+ var dateDate = date instanceof Date ? date : new Date(date);
131
+ var nowDate = now instanceof Date ? now : new Date(now);
132
+ var seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
133
+
134
+ var _getRelativeTimeForma = getRelativeTimeFormatConfig(seconds),
135
+ unit = _getRelativeTimeForma.unit,
136
+ value = _getRelativeTimeForma.value;
137
+
138
+ return new Intl.RelativeTimeFormat(locale, {
139
+ numeric: 'auto'
140
+ }).format(value, unit);
141
+ } catch (error) {
142
+ onError(new IntlError(IntlErrorCode.FORMATTING_ERROR, error.message));
143
+ return String(date);
144
+ }
145
+ }
146
+
147
+ return {
148
+ formatDateTime: formatDateTime,
149
+ formatNumber: formatNumber,
150
+ formatRelativeTime: formatRelativeTime
151
+ };
36
152
  }
37
153
 
38
- export { createTranslator as default };
154
+ export { createIntl as default };
39
155
  //# sourceMappingURL=use-intl.esm3.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm3.js","sources":["../../src/core/createTranslator.tsx"],"sourcesContent":["import Formats from './Formats';\nimport IntlConfiguration from './IntlConfiguration';\nimport TranslationValues from './TranslationValues';\nimport createTranslatorImpl, {\n CoreRichTranslationValues\n} from './createTranslatorImpl';\nimport {defaultGetMessageFallback, defaultOnError} from './defaults';\nimport MessageKeys from './utils/MessageKeys';\nimport NamespaceKeys from './utils/NamespaceKeys';\nimport NestedKeyOf from './utils/NestedKeyOf';\nimport NestedValueOf from './utils/NestedValueOf';\n\n/**\n * Translates messages from the given namespace by using the ICU syntax.\n * See https://formatjs.io/docs/core-concepts/icu-syntax.\n *\n * If no namespace is provided, all available messages are returned.\n * The namespace can also indicate nesting by using a dot\n * (e.g. `namespace.Component`).\n */\nexport default function createTranslator<\n NestedKey extends NamespaceKeys<\n IntlMessages,\n NestedKeyOf<IntlMessages>\n > = never\n>({\n onError = defaultOnError,\n getMessageFallback = defaultGetMessageFallback,\n messages,\n namespace,\n ...rest\n}: IntlConfiguration & {\n messages: IntlMessages;\n namespace?: NestedKey;\n}): // Explicitly defining the return type is necessary as TypeScript would get it wrong\n{\n // Default invocation\n <\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: TranslationValues,\n formats?: Partial<Formats>\n ): string;\n\n // `rich`\n rich<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey,\n values?: CoreRichTranslationValues,\n formats?: Partial<Formats>\n ): string;\n\n // `raw`\n raw<\n TargetKey extends MessageKeys<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >,\n NestedKeyOf<\n NestedValueOf<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >\n >\n >\n >(\n key: TargetKey\n ): any;\n} {\n // We have to wrap the actual function so the type inference for the optional\n // namespace works correctly. See https://stackoverflow.com/a/71529575/343045\n // The prefix (\"!\") is arbitrary.\n return createTranslatorImpl<\n {'!': IntlMessages},\n [NestedKey] extends [never] ? '!' : `!.${NestedKey}`\n >(\n {\n ...rest,\n onError,\n getMessageFallback,\n messages: {'!': messages},\n // @ts-ignore\n namespace: namespace ? `!.${namespace}` : '!'\n },\n '!'\n );\n}\n"],"names":["createTranslator","onError","defaultOnError","getMessageFallback","defaultGetMessageFallback","messages","namespace","rest","createTranslatorImpl"],"mappings":";;;;;AAYA;;;;;;;AAOG;;AACqB,SAAAA,gBAAA,CAcvB,IAAA,EAAA;AAAA,EAAA,IAAA,YAAA,GAAA,IAAA,CARCC,OAQD;AAAA,MARCA,OAQD,6BARWC,cAQX,GAAA,YAAA;AAAA,MAAA,qBAAA,GAAA,IAAA,CAPCC,kBAOD;AAAA,MAPCA,kBAOD,sCAPsBC,yBAOtB,GAAA,qBAAA;AAAA,MANCC,QAMD,QANCA,QAMD;AAAA,MALCC,SAKD,QALCA,SAKD;AAAA,MAJIC,IAIJ,GAAA,6BAAA,CAAA,IAAA,EAAA,SAAA,CAAA,CAAA;;AA4DC;AACA;AACA;AACA,EAAOC,OAAAA,oBAAoB,cAKpBD,IALoB,EAAA;AAMvBN,IAAAA,OAAO,EAAPA,OANuB;AAOvBE,IAAAA,kBAAkB,EAAlBA,kBAPuB;AAQvBE,IAAAA,QAAQ,EAAE;AAAC,MAAKA,GAAAA,EAAAA,QAAAA;AAAN,KARa;AASvB;AACAC,IAAAA,SAAS,EAAEA,SAAS,GAAA,IAAA,GAAQA,SAAR,GAAsB,GAAA;AAVnB,GAAA,CAAA,EAYzB,GAZyB,CAA3B,CAAA;AAcD;;;;"}
1
+ {"version":3,"file":"use-intl.esm3.js","sources":["../../src/core/createIntl.tsx"],"sourcesContent":["import DateTimeFormatOptions from './DateTimeFormatOptions';\nimport Formats from './Formats';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport NumberFormatOptions from './NumberFormatOptions';\nimport {defaultOnError} from './defaults';\n\nconst MINUTE = 60;\nconst HOUR = MINUTE * 60;\nconst DAY = HOUR * 24;\nconst WEEK = DAY * 7;\nconst MONTH = DAY * (365 / 12); // Approximation\nconst YEAR = DAY * 365;\n\nfunction getRelativeTimeFormatConfig(seconds: number) {\n const absValue = Math.abs(seconds);\n let value, unit: Intl.RelativeTimeFormatUnit;\n\n // We have to round the resulting values, as `Intl.RelativeTimeFormat`\n // will include fractions like '2.1 hours ago'.\n\n if (absValue < MINUTE) {\n unit = 'second';\n value = Math.round(seconds);\n } else if (absValue < HOUR) {\n unit = 'minute';\n value = Math.round(seconds / MINUTE);\n } else if (absValue < DAY) {\n unit = 'hour';\n value = Math.round(seconds / HOUR);\n } else if (absValue < WEEK) {\n unit = 'day';\n value = Math.round(seconds / DAY);\n } else if (absValue < MONTH) {\n unit = 'week';\n value = Math.round(seconds / WEEK);\n } else if (absValue < YEAR) {\n unit = 'month';\n value = Math.round(seconds / MONTH);\n } else {\n unit = 'year';\n value = Math.round(seconds / YEAR);\n }\n\n return {value, unit};\n}\n\ntype Props = {\n locale: string;\n timeZone?: string;\n onError?(error: IntlError): void;\n formats?: Partial<Formats>;\n now?: Date;\n};\n\nexport default function createIntl({\n formats,\n locale,\n now: globalNow,\n onError = defaultOnError,\n timeZone\n}: Props) {\n function resolveFormatOrOptions<Options>(\n typeFormats: Record<string, Options> | undefined,\n formatOrOptions?: string | Options\n ) {\n let options;\n if (typeof formatOrOptions === 'string') {\n const formatName = formatOrOptions;\n options = typeFormats?.[formatName];\n\n if (!options) {\n const error = new IntlError(\n IntlErrorCode.MISSING_FORMAT,\n __DEV__\n ? `Format \\`${formatName}\\` is not available. You can configure it on the provider or provide custom options.`\n : undefined\n );\n onError(error);\n throw error;\n }\n } else {\n options = formatOrOptions;\n }\n\n return options;\n }\n\n function getFormattedValue<Value, Options>(\n value: Value,\n formatOrOptions: string | Options | undefined,\n typeFormats: Record<string, Options> | undefined,\n formatter: (options?: Options) => string\n ) {\n let options;\n try {\n options = resolveFormatOrOptions(typeFormats, formatOrOptions);\n } catch (error) {\n return String(value);\n }\n\n try {\n return formatter(options);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(value);\n }\n }\n\n function formatDateTime(\n /** If a number is supplied, this is interpreted as a UTC timestamp. */\n value: Date | number,\n /** If a time zone is supplied, the `value` is converted to that time zone.\n * Otherwise the user time zone will be used. */\n formatOrOptions?: string | DateTimeFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.dateTime,\n (options) => {\n if (timeZone && !options?.timeZone) {\n options = {...options, timeZone};\n }\n\n return new Intl.DateTimeFormat(locale, options).format(value);\n }\n );\n }\n\n function formatNumber(\n value: number,\n formatOrOptions?: string | NumberFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.number,\n (options) => new Intl.NumberFormat(locale, options).format(value)\n );\n }\n\n function formatRelativeTime(\n /** The date time that needs to be formatted. */\n date: number | Date,\n /** The reference point in time to which `date` will be formatted in relation to. */\n now?: number | Date\n ) {\n try {\n if (!now) {\n if (globalNow) {\n now = globalNow;\n } else {\n throw new Error(\n __DEV__\n ? `The \\`now\\` parameter wasn't provided to \\`formatRelativeTime\\` and there was no global fallback configured on the provider.`\n : undefined\n );\n }\n }\n\n const dateDate = date instanceof Date ? date : new Date(date);\n const nowDate = now instanceof Date ? now : new Date(now);\n\n const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;\n const {unit, value} = getRelativeTimeFormatConfig(seconds);\n\n return new Intl.RelativeTimeFormat(locale, {\n numeric: 'auto'\n }).format(value, unit);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(date);\n }\n }\n\n return {formatDateTime, formatNumber, formatRelativeTime};\n}\n"],"names":["MINUTE","HOUR","DAY","WEEK","MONTH","YEAR","getRelativeTimeFormatConfig","seconds","absValue","Math","abs","value","unit","round","createIntl","formats","locale","globalNow","now","onError","defaultOnError","timeZone","resolveFormatOrOptions","typeFormats","formatOrOptions","options","formatName","error","IntlError","IntlErrorCode","MISSING_FORMAT","undefined","getFormattedValue","formatter","String","FORMATTING_ERROR","message","formatDateTime","dateTime","Intl","DateTimeFormat","format","formatNumber","number","NumberFormat","formatRelativeTime","date","Error","dateDate","Date","nowDate","getTime","RelativeTimeFormat","numeric"],"mappings":";;;;AAMA,IAAMA,MAAM,GAAG,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A5C,CAAA;AAEA,MAAA,IAAMX,OAAO,GAAG,CAACyC,QAAQ,CAACG,OAAT,EAAqBD,GAAAA,OAAO,CAACC,OAAR,EAAtB,IAA2C,IAA3D,CAAA;;AACA,MAAsB7C,IAAAA,qBAAAA,GAAAA,2BAA2B,CAACC,OAAD,CAAjD;AAAA,UAAOK,IAAP,yBAAOA,IAAP;AAAA,UAAaD,KAAb,yBAAaA,KAAb,CAAA;;AAEA,MAAA,OAAO,IAAI4B,IAAI,CAACa,kBAAT,CAA4BpC,MAA5B,EAAoC;AACzCqC,QAAAA,OAAO,EAAE,MAAA;AADgC,OAApC,EAEJZ,MAFI,CAEG9B,KAFH,EAEUC,IAFV,CAAP,CAAA;AAGD,KAtBD,CAsBE,OAAOe,KAAP,EAAc;AACdR,MAAAA,OAAO,CACL,IAAIS,SAAJ,CAAcC,aAAa,CAACM,gBAA5B,EAA+CR,KAAe,CAACS,OAA/D,CADK,CAAP,CAAA;AAGA,MAAOF,OAAAA,MAAM,CAACY,IAAD,CAAb,CAAA;AACD,KAAA;AACF,GAAA;;AAED,EAAO,OAAA;AAACT,IAAAA,cAAc,EAAdA,cAAD;AAAiBK,IAAAA,YAAY,EAAZA,YAAjB;AAA+BG,IAAAA,kBAAkB,EAAlBA,kBAAAA;AAA/B,GAAP,CAAA;AACD;;;;"}
@@ -1,7 +1,7 @@
1
1
  import { objectWithoutPropertiesLoose as _objectWithoutPropertiesLoose, extends as _extends } from '../_virtual/use-intl.esm.js';
2
2
  import IntlError, { IntlErrorCode } from './use-intl.esm2.js';
3
3
  import createBaseTranslator, { getMessagesOrError } from './use-intl.esm7.js';
4
- import resolveNamespace from './use-intl.esm6.js';
4
+ import resolveNamespace from './use-intl.esm8.js';
5
5
 
6
6
  var _excluded = ["getMessageFallback", "messages", "namespace", "onError"];
7
7
  function createTranslatorImpl(_ref, namespacePrefix) {
@@ -39,7 +39,7 @@ function createTranslatorImpl(_ref, namespacePrefix) {
39
39
  var result = originalRich(key, values, formats); // When only string chunks are provided to the parser, only strings should be returned here.
40
40
 
41
41
  if (typeof result !== 'string') {
42
- var error = new IntlError(IntlErrorCode.FORMATTING_ERROR, process.env.NODE_ENV !== 'production' ? "`createTranslator` only accepts functions for rich text formatting that receive and return strings.\n\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})" : undefined);
42
+ var error = new IntlError(IntlErrorCode.FORMATTING_ERROR, process.env.NODE_ENV !== "production" ? "`createTranslator` only accepts functions for rich text formatting that receive and return strings.\n\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})" : undefined);
43
43
  onError(error);
44
44
  return getMessageFallback({
45
45
  error: error,
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm5.js","sources":["../../src/core/createTranslatorImpl.tsx"],"sourcesContent":["import AbstractIntlMessages from './AbstractIntlMessages';\nimport {InitializedIntlConfiguration} from './IntlConfiguration';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport {RichTranslationValues, TranslationValue} from './TranslationValues';\nimport createBaseTranslator, {getMessagesOrError} from './createBaseTranslator';\nimport resolveNamespace from './resolveNamespace';\nimport NestedKeyOf from './utils/NestedKeyOf';\n\nexport type CoreRichTranslationValues = Record<\n string,\n TranslationValue | ((chunks: string) => string)\n>;\n\nexport type CreateTranslatorImplProps<Messages> =\n InitializedIntlConfiguration & {\n namespace: string;\n messages: Messages;\n };\n\nexport default function createTranslatorImpl<\n Messages extends AbstractIntlMessages,\n NestedKey extends NestedKeyOf<Messages>\n>(\n {\n getMessageFallback,\n messages,\n namespace,\n onError,\n ...rest\n }: CreateTranslatorImplProps<Messages>,\n namespacePrefix: string\n) {\n // The `namespacePrefix` is part of the type system.\n // See the comment in the function invocation.\n messages = messages[namespacePrefix] as Messages;\n namespace = resolveNamespace(namespace, namespacePrefix) as NestedKey;\n\n const translator = createBaseTranslator<Messages, NestedKey>({\n ...rest,\n onError,\n getMessageFallback,\n messagesOrError: getMessagesOrError({\n messages,\n namespace,\n onError\n }) as Messages | IntlError\n });\n\n const originalRich = translator.rich;\n\n function base(...args: Parameters<typeof translator>) {\n return translator(...args);\n }\n\n // Augment `t.rich` to return plain strings\n base.rich = (\n key: Parameters<typeof originalRich>[0],\n /** Key value pairs for values to interpolate into the message. */\n values: CoreRichTranslationValues,\n formats?: Parameters<typeof originalRich>[2]\n ): string => {\n // `chunks` is returned as a string when no React element\n // is used, therefore it's safe to cast this type.\n const result = originalRich(key, values as RichTranslationValues, formats);\n\n // When only string chunks are provided to the parser, only strings should be returned here.\n if (typeof result !== 'string') {\n const error = new IntlError(\n IntlErrorCode.FORMATTING_ERROR,\n process.env.NODE_ENV !== 'production'\n ? \"`createTranslator` only accepts functions for rich text formatting that receive and return strings.\\n\\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})\"\n : undefined\n );\n\n onError(error);\n return getMessageFallback({error, key, namespace});\n }\n\n return result;\n };\n\n base.raw = translator.raw;\n\n return base;\n}\n"],"names":["createTranslatorImpl","namespacePrefix","getMessageFallback","messages","namespace","onError","rest","resolveNamespace","translator","createBaseTranslator","messagesOrError","getMessagesOrError","originalRich","rich","base","key","values","formats","result","error","IntlError","IntlErrorCode","FORMATTING_ERROR","process","env","NODE_ENV","undefined","raw"],"mappings":";;;;;;AAmBc,SAAUA,oBAAV,CAWZC,IAAAA,EAAAA,eAXY,EAWW;AAAA,EANrBC,IAAAA,kBAMqB,QANrBA,kBAMqB;AAAA,MALrBC,QAKqB,QALrBA,QAKqB;AAAA,MAJrBC,SAIqB,QAJrBA,SAIqB;AAAA,MAHrBC,OAGqB,QAHrBA,OAGqB;AAAA,MAFlBC,IAEkB,GAAA,6BAAA,CAAA,IAAA,EAAA,SAAA,CAAA,CAAA;;AAEvB;AACA;AACAH,EAAAA,QAAQ,GAAGA,QAAQ,CAACF,eAAD,CAAnB,CAAA;AACAG,EAAAA,SAAS,GAAGG,gBAAgB,CAACH,SAAD,EAAYH,eAAZ,CAA5B,CAAA;AAEA,EAAA,IAAMO,UAAU,GAAG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nB,UAAU,CAACmB,GAAtB,CAAA;AAEA,EAAA,OAAOb,IAAP,CAAA;AACD;;;;"}
1
+ {"version":3,"file":"use-intl.esm5.js","sources":["../../src/core/createTranslatorImpl.tsx"],"sourcesContent":["import AbstractIntlMessages from './AbstractIntlMessages';\nimport {InitializedIntlConfiguration} from './IntlConfiguration';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport {RichTranslationValues, TranslationValue} from './TranslationValues';\nimport createBaseTranslator, {getMessagesOrError} from './createBaseTranslator';\nimport resolveNamespace from './resolveNamespace';\nimport NestedKeyOf from './utils/NestedKeyOf';\n\nexport type CoreRichTranslationValues = Record<\n string,\n TranslationValue | ((chunks: string) => string)\n>;\n\nexport type CreateTranslatorImplProps<Messages> =\n InitializedIntlConfiguration & {\n namespace: string;\n messages: Messages;\n };\n\nexport default function createTranslatorImpl<\n Messages extends AbstractIntlMessages,\n NestedKey extends NestedKeyOf<Messages>\n>(\n {\n getMessageFallback,\n messages,\n namespace,\n onError,\n ...rest\n }: CreateTranslatorImplProps<Messages>,\n namespacePrefix: string\n) {\n // The `namespacePrefix` is part of the type system.\n // See the comment in the function invocation.\n messages = messages[namespacePrefix] as Messages;\n namespace = resolveNamespace(namespace, namespacePrefix) as NestedKey;\n\n const translator = createBaseTranslator<Messages, NestedKey>({\n ...rest,\n onError,\n getMessageFallback,\n messagesOrError: getMessagesOrError({\n messages,\n namespace,\n onError\n }) as Messages | IntlError\n });\n\n const originalRich = translator.rich;\n\n function base(...args: Parameters<typeof translator>) {\n return translator(...args);\n }\n\n // Augment `t.rich` to return plain strings\n base.rich = (\n key: Parameters<typeof originalRich>[0],\n /** Key value pairs for values to interpolate into the message. */\n values: CoreRichTranslationValues,\n formats?: Parameters<typeof originalRich>[2]\n ): string => {\n // `chunks` is returned as a string when no React element\n // is used, therefore it's safe to cast this type.\n const result = originalRich(key, values as RichTranslationValues, formats);\n\n // When only string chunks are provided to the parser, only strings should be returned here.\n if (typeof result !== 'string') {\n const error = new IntlError(\n IntlErrorCode.FORMATTING_ERROR,\n __DEV__\n ? \"`createTranslator` only accepts functions for rich text formatting that receive and return strings.\\n\\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})\"\n : undefined\n );\n\n onError(error);\n return getMessageFallback({error, key, namespace});\n }\n\n return result;\n };\n\n base.raw = translator.raw;\n\n return base;\n}\n"],"names":["createTranslatorImpl","namespacePrefix","getMessageFallback","messages","namespace","onError","rest","resolveNamespace","translator","createBaseTranslator","messagesOrError","getMessagesOrError","originalRich","rich","base","key","values","formats","result","error","IntlError","IntlErrorCode","FORMATTING_ERROR","undefined","raw"],"mappings":";;;;;;AAmBc,SAAUA,oBAAV,CAWZC,IAAAA,EAAAA,eAXY,EAWW;AAAA,EANrBC,IAAAA,kBAMqB,QANrBA,kBAMqB;AAAA,MALrBC,QAKqB,QALrBA,QAKqB;AAAA,MAJrBC,SAIqB,QAJrBA,SAIqB;AAAA,MAHrBC,OAGqB,QAHrBA,OAGqB;AAAA,MAFlBC,IAEkB,GAAA,6BAAA,CAAA,IAAA,EAAA,SAAA,CAAA,CAAA;;AAEvB;AACA;AACAH,EAAAA,QAAQ,GAAGA,QAAQ,CAACF,eAAD,CAAnB,CAAA;AACAG,EAAAA,SAAS,GAAGG,gBAAgB,CAACH,SAAD,EAAYH,eAAZ,CAA5B,CAAA;AAEA,EAAA,IAAMO,UAAU,GAAGC,oBAAoB,CAAA,QAAA,CAAA,EAAA,EAClCH,IADkC,EAAA;AAErCD,IAAAA,OAAO,EAAPA,OAFqC;AAGrCH,IAAAA,kBAAkB,EAAlBA,kBAHqC;AAIrCQ,IAAAA,eAAe,EAAEC,kBAAkB,CAAC;AAClCR,MAAAA,QAAQ,EAARA,QADkC;AAElCC,MAAAA,SAAS,EAATA,SAFkC;AAGlCC,MAAAA,OAAO,EAAPA,OAAAA;AAHkC,KAAD,CAAA;AAJE,GAAvC,CAAA,CAAA,CAAA;AAWA,EAAA,IAAMO,YAAY,GAAGJ,UAAU,CAACK,IAAhC,CAAA;;AAEA,EAAA,SAASC,IAAT,GAAoD;AAClD,IAAON,OAAAA,UAAU,MAAV,CAAP,KAAA,CAAA,EAAA,SAAA,CAAA,CAAA;AACD,GAtBsB;;;AAyBvBM,EAAAA,IAAI,CAACD,IAAL,GAAY,UACVE,GADU;AAEV;AACAC,EAAAA,MAHU,EAIVC,OAJU,EAKA;AACV;AACA;AACA,IAAMC,IAAAA,MAAM,GAAGN,YAAY,CAACG,GAAD,EAAMC,MAAN,EAAuCC,OAAvC,CAA3B,CAHU;;AAMV,IAAA,IAAI,OAAOC,MAAP,KAAkB,QAAtB,EAAgC;AAC9B,MAAA,IAAMC,KAAK,GAAG,IAAIC,SAAJ,CACZC,aAAa,CAACC,gBADF,EAEZ,OAAA,CAAA,GAAA,CAAA,QAAA,KAAA,YAAA,GACI,iKADJ,GAEIC,SAJQ,CAAd,CAAA;AAOAlB,MAAAA,OAAO,CAACc,KAAD,CAAP,CAAA;AACA,MAAA,OAAOjB,kBAAkB,CAAC;AAACiB,QAAAA,KAAK,EAALA,KAAD;AAAQJ,QAAAA,GAAG,EAAHA,GAAR;AAAaX,QAAAA,SAAS,EAATA,SAAAA;AAAb,OAAD,CAAzB,CAAA;AACD,KAAA;;AAED,IAAA,OAAOc,MAAP,CAAA;AACD,GAxBD,CAAA;;AA0BAJ,EAAAA,IAAI,CAACU,GAAL,GAAWhB,UAAU,CAACgB,GAAtB,CAAA;AAEA,EAAA,OAAOV,IAAP,CAAA;AACD;;;;"}
@@ -1,10 +1,32 @@
1
- /**
2
- * For the strictly typed messages to work we have to wrap the namespace into
3
- * a mandatory prefix. See https://stackoverflow.com/a/71529575/343045
4
- */
5
- function resolveNamespace(namespace, namespacePrefix) {
6
- return namespace === namespacePrefix ? undefined : namespace.slice((namespacePrefix + '.').length);
1
+ import IntlError, { IntlErrorCode } from './use-intl.esm2.js';
2
+
3
+ function validateMessagesSegment(messages, invalidKeyLabels, parentPath) {
4
+ Object.entries(messages).forEach(function (_ref) {
5
+ var key = _ref[0],
6
+ messageOrMessages = _ref[1];
7
+
8
+ if (key.includes('.')) {
9
+ var keyLabel = key;
10
+ if (parentPath) keyLabel += " (at " + parentPath + ")";
11
+ invalidKeyLabels.push(keyLabel);
12
+ }
13
+
14
+ if (messageOrMessages != null && typeof messageOrMessages === 'object') {
15
+ validateMessagesSegment(messageOrMessages, invalidKeyLabels, [parentPath, key].filter(function (part) {
16
+ return part != null;
17
+ }).join('.'));
18
+ }
19
+ });
20
+ }
21
+
22
+ function validateMessages(messages, onError) {
23
+ var invalidKeyLabels = [];
24
+ validateMessagesSegment(messages, invalidKeyLabels);
25
+
26
+ if (invalidKeyLabels.length > 0) {
27
+ onError(new IntlError(IntlErrorCode.INVALID_KEY, "Namespace keys can not contain the character \".\" as this is used to express nesting. Please remove it or replace it with another character.\n\nInvalid " + (invalidKeyLabels.length === 1 ? 'key' : 'keys') + ": " + invalidKeyLabels.join(', ')));
28
+ }
7
29
  }
8
30
 
9
- export { resolveNamespace as default };
31
+ export { validateMessages as default };
10
32
  //# sourceMappingURL=use-intl.esm6.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm6.js","sources":["../../src/core/resolveNamespace.tsx"],"sourcesContent":["/**\n * For the strictly typed messages to work we have to wrap the namespace into\n * a mandatory prefix. See https://stackoverflow.com/a/71529575/343045\n */\nexport default function resolveNamespace(\n namespace: string,\n namespacePrefix: string\n) {\n return namespace === namespacePrefix\n ? undefined\n : namespace.slice((namespacePrefix + '.').length);\n}\n"],"names":["resolveNamespace","namespace","namespacePrefix","undefined","slice","length"],"mappings":"AAAA;;;AAGG;AACW,SAAUA,gBAAV,CACZC,SADY,EAEZC,eAFY,EAEW;AAEvB,EAAA,OAAOD,SAAS,KAAKC,eAAd,GACHC,SADG,GAEHF,SAAS,CAACG,KAAV,CAAgB,CAACF,eAAe,GAAG,GAAnB,EAAwBG,MAAxC,CAFJ,CAAA;AAGD;;;;"}
1
+ {"version":3,"file":"use-intl.esm6.js","sources":["../../src/core/validateMessages.tsx"],"sourcesContent":["import AbstractIntlMessages from './AbstractIntlMessages';\nimport IntlError, {IntlErrorCode} from './IntlError';\n\nfunction validateMessagesSegment(\n messages: AbstractIntlMessages,\n invalidKeyLabels: Array<string>,\n parentPath?: string\n) {\n Object.entries(messages).forEach(([key, messageOrMessages]) => {\n if (key.includes('.')) {\n let keyLabel = key;\n if (parentPath) keyLabel += ` (at ${parentPath})`;\n invalidKeyLabels.push(keyLabel);\n }\n\n if (messageOrMessages != null && typeof messageOrMessages === 'object') {\n validateMessagesSegment(\n messageOrMessages,\n invalidKeyLabels,\n [parentPath, key].filter((part) => part != null).join('.')\n );\n }\n });\n}\n\nexport default function validateMessages(\n messages: AbstractIntlMessages,\n onError: (error: IntlError) => void\n) {\n const invalidKeyLabels: Array<string> = [];\n validateMessagesSegment(messages, invalidKeyLabels);\n\n if (invalidKeyLabels.length > 0) {\n onError(\n new IntlError(\n IntlErrorCode.INVALID_KEY,\n `Namespace keys can not contain the character \".\" as this is used to express nesting. Please remove it or replace it with another character.\\n\\nInvalid ${\n invalidKeyLabels.length === 1 ? 'key' : 'keys'\n }: ${invalidKeyLabels.join(', ')}`\n )\n );\n }\n}\n"],"names":["validateMessagesSegment","messages","invalidKeyLabels","parentPath","Object","entries","forEach","key","messageOrMessages","includes","keyLabel","push","filter","part","join","validateMessages","onError","length","IntlError","IntlErrorCode","INVALID_KEY"],"mappings":";;AAGA,SAASA,uBAAT,CACEC,QADF,EAEEC,gBAFF,EAGEC,UAHF,EAGqB;AAEnBC,EAAAA,MAAM,CAACC,OAAP,CAAeJ,QAAf,CAAyBK,CAAAA,OAAzB,CAAiC,UAA6B,IAAA,EAAA;AAAA,IAAA,IAA3BC,GAA2B,GAAA,IAAA,CAAA,CAAA,CAAA;AAAA,QAAtBC,iBAAsB,GAAA,IAAA,CAAA,CAAA,CAAA,CAAA;;AAC5D,IAAA,IAAID,GAAG,CAACE,QAAJ,CAAa,GAAb,CAAJ,EAAuB;AACrB,MAAIC,IAAAA,QAAQ,GAAGH,GAAf,CAAA;AACA,MAAA,IAAIJ,UAAJ,EAAgBO,QAAQ,IAAA,OAAA,GAAYP,UAAZ,GAAR,GAAA,CAAA;AAChBD,MAAAA,gBAAgB,CAACS,IAAjB,CAAsBD,QAAtB,CAAA,CAAA;AACD,KAAA;;AAED,IAAIF,IAAAA,iBAAiB,IAAI,IAArB,IAA6B,OAAOA,iBAAP,KAA6B,QAA9D,EAAwE;AACtER,MAAAA,uBAAuB,CACrBQ,iBADqB,EAErBN,gBAFqB,EAGrB,CAACC,UAAD,EAAaI,GAAb,CAAA,CAAkBK,MAAlB,CAAyB,UAACC,IAAD,EAAA;AAAA,QAAUA,OAAAA,IAAI,IAAI,IAAlB,CAAA;AAAA,OAAzB,CAAiDC,CAAAA,IAAjD,CAAsD,GAAtD,CAHqB,CAAvB,CAAA;AAKD,KAAA;AACF,GAdD,CAAA,CAAA;AAeD,CAAA;;AAEa,SAAUC,gBAAV,CACZd,QADY,EAEZe,OAFY,EAEuB;AAEnC,EAAMd,IAAAA,gBAAgB,GAAkB,EAAxC,CAAA;AACAF,EAAAA,uBAAuB,CAACC,QAAD,EAAWC,gBAAX,CAAvB,CAAA;;AAEA,EAAA,IAAIA,gBAAgB,CAACe,MAAjB,GAA0B,CAA9B,EAAiC;AAC/BD,IAAAA,OAAO,CACL,IAAIE,SAAJ,CACEC,aAAa,CAACC,WADhB,EAAA,2JAAA,IAGIlB,gBAAgB,CAACe,MAAjB,KAA4B,CAA5B,GAAgC,KAAhC,GAAwC,MAH5C,CAIOf,GAAAA,IAAAA,GAAAA,gBAAgB,CAACY,IAAjB,CAAsB,IAAtB,CAJP,CADK,CAAP,CAAA;AAQD,GAAA;AACF;;;;"}
@@ -7,7 +7,7 @@ import { defaultOnError, defaultGetMessageFallback } from './use-intl.esm4.js';
7
7
 
8
8
  function resolvePath(messages, key, namespace) {
9
9
  if (!messages) {
10
- throw new Error(process.env.NODE_ENV !== 'production' ? "No messages available at `" + namespace + "`." : undefined);
10
+ throw new Error(process.env.NODE_ENV !== "production" ? "No messages available at `" + namespace + "`." : undefined);
11
11
  }
12
12
 
13
13
  var message = messages;
@@ -15,7 +15,7 @@ function resolvePath(messages, key, namespace) {
15
15
  var next = message[part];
16
16
 
17
17
  if (part == null || next == null) {
18
- throw new Error(process.env.NODE_ENV !== 'production' ? "Could not resolve `" + key + "` in " + (namespace ? "`" + namespace + "`" : 'messages') + "." : undefined);
18
+ throw new Error(process.env.NODE_ENV !== "production" ? "Could not resolve `" + key + "` in " + (namespace ? "`" + namespace + "`" : 'messages') + "." : undefined);
19
19
  }
20
20
 
21
21
  message = next;
@@ -56,13 +56,13 @@ function getMessagesOrError(_ref) {
56
56
 
57
57
  try {
58
58
  if (!messages) {
59
- throw new Error(process.env.NODE_ENV !== 'production' ? "No messages were configured on the provider." : undefined);
59
+ throw new Error(process.env.NODE_ENV !== "production" ? "No messages were configured on the provider." : undefined);
60
60
  }
61
61
 
62
62
  var retrievedMessages = namespace ? resolvePath(messages, namespace) : messages;
63
63
 
64
64
  if (!retrievedMessages) {
65
- throw new Error(process.env.NODE_ENV !== 'production' ? "No messages for namespace `" + namespace + "` found." : undefined);
65
+ throw new Error(process.env.NODE_ENV !== "production" ? "No messages for namespace `" + namespace + "` found." : undefined);
66
66
  }
67
67
 
68
68
  return retrievedMessages;
@@ -130,7 +130,7 @@ function createBaseTranslator(_ref2) {
130
130
  }
131
131
 
132
132
  if (typeof message === 'object') {
133
- return getFallbackFromErrorAndNotify(key, IntlErrorCode.INSUFFICIENT_PATH, process.env.NODE_ENV !== 'production' ? "Insufficient path specified for `" + key + "` in `" + (namespace ? "`" + namespace + "`" : 'messages') + "`." : undefined);
133
+ return getFallbackFromErrorAndNotify(key, IntlErrorCode.INSUFFICIENT_PATH, process.env.NODE_ENV !== "production" ? "Insufficient path specified for `" + key + "` in `" + (namespace ? "`" + namespace + "`" : 'messages') + "`." : undefined);
134
134
  }
135
135
 
136
136
  try {
@@ -156,7 +156,7 @@ function createBaseTranslator(_ref2) {
156
156
  prepareTranslationValues(_extends({}, defaultTranslationValues, values)));
157
157
 
158
158
  if (formattedMessage == null) {
159
- throw new Error(process.env.NODE_ENV !== 'production' ? "Unable to format `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') : undefined);
159
+ throw new Error(process.env.NODE_ENV !== "production" ? "Unable to format `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') : undefined);
160
160
  } // Limit the function signature to return strings or React elements
161
161
 
162
162
 
@@ -177,7 +177,7 @@ function createBaseTranslator(_ref2) {
177
177
  var result = translateBaseFn(key, values, formats);
178
178
 
179
179
  if (typeof result !== 'string') {
180
- return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, process.env.NODE_ENV !== 'production' ? "The message `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') + " didn't resolve to a string. If you want to format rich text, use `t.rich` instead." : undefined);
180
+ return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, process.env.NODE_ENV !== "production" ? "The message `" + key + "` in " + (namespace ? "namespace `" + namespace + "`" : 'messages') + " didn't resolve to a string. If you want to format rich text, use `t.rich` instead." : undefined);
181
181
  }
182
182
 
183
183
  return result;