use-intl 2.9.2 → 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 (128) hide show
  1. package/CHANGELOG.md +471 -0
  2. package/dist/core/use-intl.esm.js +38 -41
  3. package/dist/core/use-intl.esm.js.map +1 -1
  4. package/dist/core/use-intl.esm2.js +41 -38
  5. package/dist/core/use-intl.esm2.js.map +1 -1
  6. package/dist/core/use-intl.esm3.js +2 -2
  7. package/dist/core/use-intl.esm4.js +12 -56
  8. package/dist/core/use-intl.esm4.js.map +1 -1
  9. package/dist/core/use-intl.esm5.js +56 -12
  10. package/dist/core/use-intl.esm5.js.map +1 -1
  11. package/dist/core/use-intl.esm6.js +1 -1
  12. package/dist/core/use-intl.esm7.js +2 -2
  13. package/dist/react/use-intl.esm.js +2 -2
  14. package/dist/react/use-intl.esm2.js +41 -17
  15. package/dist/react/use-intl.esm2.js.map +1 -1
  16. package/dist/react/use-intl.esm3.js +21 -20
  17. package/dist/react/use-intl.esm3.js.map +1 -1
  18. package/dist/react/use-intl.esm4.js +21 -4
  19. package/dist/react/use-intl.esm4.js.map +1 -1
  20. package/dist/react/use-intl.esm5.js +4 -46
  21. package/dist/react/use-intl.esm5.js.map +1 -1
  22. package/dist/react/use-intl.esm6.js +4 -4
  23. package/dist/react/use-intl.esm6.js.map +1 -1
  24. package/dist/react/use-intl.esm7.js +12 -3
  25. package/dist/react/use-intl.esm7.js.map +1 -1
  26. package/dist/react/use-intl.esm8.js +38 -9
  27. package/dist/react/use-intl.esm8.js.map +1 -1
  28. package/dist/react/use-intl.esm9.js +3 -41
  29. package/dist/react/use-intl.esm9.js.map +1 -1
  30. package/dist/src/core/AbstractIntlMessages.d.ts +9 -0
  31. package/dist/src/core/AbstractIntlMessages.js +2 -0
  32. package/dist/src/core/AbstractIntlMessages.js.map +1 -0
  33. package/dist/src/core/DateTimeFormatOptions.d.ts +72 -0
  34. package/dist/src/core/DateTimeFormatOptions.js +3 -0
  35. package/dist/src/core/DateTimeFormatOptions.js.map +1 -0
  36. package/dist/src/core/Formats.d.ts +7 -0
  37. package/dist/src/core/Formats.js +2 -0
  38. package/dist/src/core/Formats.js.map +1 -0
  39. package/dist/src/core/IntlConfiguration.d.ts +46 -0
  40. package/dist/src/core/IntlConfiguration.js +2 -0
  41. package/dist/src/core/IntlConfiguration.js.map +1 -0
  42. package/dist/src/core/IntlError.d.ts +13 -0
  43. package/dist/src/core/IntlError.js +23 -0
  44. package/dist/src/core/IntlError.js.map +1 -0
  45. package/dist/src/core/IntlMessages.d.ts +2 -0
  46. package/dist/src/core/IntlMessages.js +4 -0
  47. package/dist/src/core/IntlMessages.js.map +1 -0
  48. package/dist/src/core/NumberFormatOptions.d.ts +2 -0
  49. package/dist/src/core/NumberFormatOptions.js +2 -0
  50. package/dist/src/core/NumberFormatOptions.js.map +1 -0
  51. package/dist/src/core/TranslationValues.d.ts +5 -0
  52. package/dist/src/core/TranslationValues.js +2 -0
  53. package/dist/src/core/TranslationValues.js.map +1 -0
  54. package/dist/src/core/convertFormatsToIntlMessageFormat.d.ts +10 -0
  55. package/dist/src/core/convertFormatsToIntlMessageFormat.js +31 -0
  56. package/dist/src/core/convertFormatsToIntlMessageFormat.js.map +1 -0
  57. package/dist/src/core/createBaseTranslator.d.ts +26 -0
  58. package/dist/src/core/createBaseTranslator.js +181 -0
  59. package/dist/src/core/createBaseTranslator.js.map +1 -0
  60. package/dist/src/core/createIntl.d.ts +17 -0
  61. package/dist/src/core/createIntl.js +126 -0
  62. package/dist/src/core/createIntl.js.map +1 -0
  63. package/dist/src/core/createTranslator.d.ts +48 -0
  64. package/dist/src/core/createTranslator.js +24 -0
  65. package/dist/src/core/createTranslator.js.map +1 -0
  66. package/dist/src/core/createTranslatorImpl.d.ts +14 -0
  67. package/dist/src/core/createTranslatorImpl.js +43 -0
  68. package/dist/src/core/createTranslatorImpl.js.map +1 -0
  69. package/dist/src/core/defaults.d.ts +11 -0
  70. package/dist/src/core/defaults.js +11 -0
  71. package/dist/src/core/defaults.js.map +1 -0
  72. package/dist/src/core/index.d.ts +8 -0
  73. package/dist/src/core/index.js +4 -0
  74. package/dist/src/core/index.js.map +1 -0
  75. package/dist/src/core/resolveNamespace.d.ts +5 -0
  76. package/dist/src/core/resolveNamespace.js +10 -0
  77. package/dist/src/core/resolveNamespace.js.map +1 -0
  78. package/dist/src/core/utils/MessageKeys.d.ts +5 -0
  79. package/dist/src/core/utils/MessageKeys.js +2 -0
  80. package/dist/src/core/utils/MessageKeys.js.map +1 -0
  81. package/dist/src/core/utils/NamespaceKeys.d.ts +5 -0
  82. package/dist/src/core/utils/NamespaceKeys.js +2 -0
  83. package/dist/src/core/utils/NamespaceKeys.js.map +1 -0
  84. package/dist/src/core/utils/NestedKeyOf.d.ts +4 -0
  85. package/dist/src/core/utils/NestedKeyOf.js +2 -0
  86. package/dist/src/core/utils/NestedKeyOf.js.map +1 -0
  87. package/dist/src/core/utils/NestedValueOf.d.ts +2 -0
  88. package/dist/src/core/utils/NestedValueOf.js +2 -0
  89. package/dist/src/core/utils/NestedValueOf.js.map +1 -0
  90. package/dist/src/core/validateMessages.d.ts +3 -0
  91. package/dist/src/core/validateMessages.js +22 -0
  92. package/dist/src/core/validateMessages.js.map +1 -0
  93. package/dist/src/index.d.ts +2 -0
  94. package/dist/src/index.js +3 -0
  95. package/dist/src/index.js.map +1 -0
  96. package/dist/src/react/IntlContext.d.ts +21 -0
  97. package/dist/src/react/IntlContext.js +4 -0
  98. package/dist/src/react/IntlContext.js.map +1 -0
  99. package/dist/src/react/IntlProvider.d.ts +16 -0
  100. package/dist/src/react/IntlProvider.js +16 -0
  101. package/dist/src/react/IntlProvider.js.map +1 -0
  102. package/dist/src/react/index.d.ts +6 -0
  103. package/dist/src/react/index.js +7 -0
  104. package/dist/src/react/index.js.map +1 -0
  105. package/dist/src/react/useIntl.d.ts +5 -0
  106. package/dist/src/react/useIntl.js +14 -0
  107. package/dist/src/react/useIntl.js.map +1 -0
  108. package/dist/src/react/useIntlContext.d.ts +1 -0
  109. package/dist/src/react/useIntlContext.js +12 -0
  110. package/dist/src/react/useIntlContext.js.map +1 -0
  111. package/dist/src/react/useLocale.d.ts +1 -0
  112. package/dist/src/react/useLocale.js +5 -0
  113. package/dist/src/react/useLocale.js.map +1 -0
  114. package/dist/src/react/useNow.d.ts +23 -0
  115. package/dist/src/react/useNow.js +40 -0
  116. package/dist/src/react/useNow.js.map +1 -0
  117. package/dist/src/react/useTimeZone.d.ts +1 -0
  118. package/dist/src/react/useTimeZone.js +5 -0
  119. package/dist/src/react/useTimeZone.js.map +1 -0
  120. package/dist/src/react/useTranslations.d.ts +44 -0
  121. package/dist/src/react/useTranslations.js +21 -0
  122. package/dist/src/react/useTranslations.js.map +1 -0
  123. package/dist/src/react/useTranslationsImpl.d.ts +8 -0
  124. package/dist/src/react/useTranslationsImpl.js +35 -0
  125. package/dist/src/react/useTranslationsImpl.js.map +1 -0
  126. package/dist/use-intl.esm.js +7 -7
  127. package/package.json +7 -6
  128. package/LICENSE +0 -21
@@ -0,0 +1,181 @@
1
+ import IntlMessageFormat from 'intl-messageformat';
2
+ import { cloneElement, isValidElement } from 'react';
3
+ import IntlError, { IntlErrorCode } from './IntlError';
4
+ import convertFormatsToIntlMessageFormat from './convertFormatsToIntlMessageFormat';
5
+ import { defaultGetMessageFallback, defaultOnError } from './defaults';
6
+ function resolvePath(messages, key, namespace) {
7
+ if (!messages) {
8
+ throw new Error(__DEV__ ? `No messages available at \`${namespace}\`.` : undefined);
9
+ }
10
+ let message = messages;
11
+ key.split('.').forEach((part) => {
12
+ const next = message[part];
13
+ if (part == null || next == null) {
14
+ throw new Error(__DEV__
15
+ ? `Could not resolve \`${key}\` in ${namespace ? `\`${namespace}\`` : 'messages'}.`
16
+ : undefined);
17
+ }
18
+ message = next;
19
+ });
20
+ return message;
21
+ }
22
+ function prepareTranslationValues(values) {
23
+ if (Object.keys(values).length === 0)
24
+ return undefined;
25
+ // Workaround for https://github.com/formatjs/formatjs/issues/1467
26
+ const transformedValues = {};
27
+ Object.keys(values).forEach((key) => {
28
+ let index = 0;
29
+ const value = values[key];
30
+ let transformed;
31
+ if (typeof value === 'function') {
32
+ transformed = (chunks) => {
33
+ const result = value(chunks);
34
+ return isValidElement(result)
35
+ ? cloneElement(result, { key: key + index++ })
36
+ : result;
37
+ };
38
+ }
39
+ else {
40
+ transformed = value;
41
+ }
42
+ transformedValues[key] = transformed;
43
+ });
44
+ return transformedValues;
45
+ }
46
+ export function getMessagesOrError({ messages, namespace, onError = defaultOnError }) {
47
+ try {
48
+ if (!messages) {
49
+ throw new Error(__DEV__ ? `No messages were configured on the provider.` : undefined);
50
+ }
51
+ const retrievedMessages = namespace
52
+ ? resolvePath(messages, namespace)
53
+ : messages;
54
+ if (!retrievedMessages) {
55
+ throw new Error(__DEV__
56
+ ? `No messages for namespace \`${namespace}\` found.`
57
+ : undefined);
58
+ }
59
+ return retrievedMessages;
60
+ }
61
+ catch (error) {
62
+ const intlError = new IntlError(IntlErrorCode.MISSING_MESSAGE, error.message);
63
+ onError(intlError);
64
+ return intlError;
65
+ }
66
+ }
67
+ export default function createBaseTranslator({ cachedFormatsByLocale, defaultTranslationValues, formats: globalFormats, getMessageFallback = defaultGetMessageFallback, locale, messagesOrError, namespace, onError, timeZone }) {
68
+ function getFallbackFromErrorAndNotify(key, code, message) {
69
+ const error = new IntlError(code, message);
70
+ onError(error);
71
+ return getMessageFallback({ error, key, namespace });
72
+ }
73
+ function translateBaseFn(
74
+ /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
75
+ key,
76
+ /** Key value pairs for values to interpolate into the message. */
77
+ values,
78
+ /** Provide custom formats for numbers, dates and times. */
79
+ formats) {
80
+ if (messagesOrError instanceof IntlError) {
81
+ // We have already warned about this during render
82
+ return getMessageFallback({
83
+ error: messagesOrError,
84
+ key,
85
+ namespace
86
+ });
87
+ }
88
+ const messages = messagesOrError;
89
+ const cacheKey = [namespace, key].filter((part) => part != null).join('.');
90
+ let messageFormat;
91
+ if (cachedFormatsByLocale?.[locale]?.[cacheKey]) {
92
+ messageFormat = cachedFormatsByLocale?.[locale][cacheKey];
93
+ }
94
+ else {
95
+ let message;
96
+ try {
97
+ message = resolvePath(messages, key, namespace);
98
+ }
99
+ catch (error) {
100
+ return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
101
+ }
102
+ if (typeof message === 'object') {
103
+ return getFallbackFromErrorAndNotify(key, IntlErrorCode.INSUFFICIENT_PATH, __DEV__
104
+ ? `Insufficient path specified for \`${key}\` in \`${namespace ? `\`${namespace}\`` : 'messages'}\`.`
105
+ : undefined);
106
+ }
107
+ try {
108
+ messageFormat = new IntlMessageFormat(message, locale, convertFormatsToIntlMessageFormat({ ...globalFormats, ...formats }, timeZone));
109
+ }
110
+ catch (error) {
111
+ return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, error.message);
112
+ }
113
+ if (cachedFormatsByLocale) {
114
+ if (!cachedFormatsByLocale[locale]) {
115
+ cachedFormatsByLocale[locale] = {};
116
+ }
117
+ cachedFormatsByLocale[locale][cacheKey] = messageFormat;
118
+ }
119
+ }
120
+ try {
121
+ const formattedMessage = messageFormat.format(
122
+ // @ts-ignore `intl-messageformat` expects a different format
123
+ // for rich text elements since a recent minor update. This
124
+ // needs to be evaluated in detail, possibly also in regards
125
+ // to be able to format to parts.
126
+ prepareTranslationValues({ ...defaultTranslationValues, ...values }));
127
+ if (formattedMessage == null) {
128
+ throw new Error(__DEV__
129
+ ? `Unable to format \`${key}\` in ${namespace ? `namespace \`${namespace}\`` : 'messages'}`
130
+ : undefined);
131
+ }
132
+ // Limit the function signature to return strings or React elements
133
+ return isValidElement(formattedMessage) ||
134
+ // Arrays of React elements
135
+ Array.isArray(formattedMessage) ||
136
+ typeof formattedMessage === 'string'
137
+ ? formattedMessage
138
+ : String(formattedMessage);
139
+ }
140
+ catch (error) {
141
+ return getFallbackFromErrorAndNotify(key, IntlErrorCode.FORMATTING_ERROR, error.message);
142
+ }
143
+ }
144
+ function translateFn(
145
+ /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
146
+ key,
147
+ /** Key value pairs for values to interpolate into the message. */
148
+ values,
149
+ /** Provide custom formats for numbers, dates and times. */
150
+ formats) {
151
+ const result = translateBaseFn(key, values, formats);
152
+ if (typeof result !== 'string') {
153
+ return getFallbackFromErrorAndNotify(key, IntlErrorCode.INVALID_MESSAGE, __DEV__
154
+ ? `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.`
155
+ : undefined);
156
+ }
157
+ return result;
158
+ }
159
+ translateFn.rich = translateBaseFn;
160
+ translateFn.raw = (
161
+ /** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
162
+ key) => {
163
+ if (messagesOrError instanceof IntlError) {
164
+ // We have already warned about this during render
165
+ return getMessageFallback({
166
+ error: messagesOrError,
167
+ key,
168
+ namespace
169
+ });
170
+ }
171
+ const messages = messagesOrError;
172
+ try {
173
+ return resolvePath(messages, key, namespace);
174
+ }
175
+ catch (error) {
176
+ return getFallbackFromErrorAndNotify(key, IntlErrorCode.MISSING_MESSAGE, error.message);
177
+ }
178
+ };
179
+ return translateFn;
180
+ }
181
+ //# sourceMappingURL=createBaseTranslator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createBaseTranslator.js","sourceRoot":"","sources":["../../../src/core/createBaseTranslator.tsx"],"names":[],"mappings":"AAAA,OAAO,iBAAiB,MAAM,oBAAoB,CAAC;AACnD,OAAO,EACL,YAAY,EACZ,cAAc,EAIf,MAAM,OAAO,CAAC;AAIf,OAAO,SAAS,EAAE,EAAC,aAAa,EAAC,MAAM,aAAa,CAAC;AAErD,OAAO,iCAAiC,MAAM,qCAAqC,CAAC;AACpF,OAAO,EAAC,yBAAyB,EAAE,cAAc,EAAC,MAAM,YAAY,CAAC;AAKrE,SAAS,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AAO,CACzB,CAAC;SACH;IACH,CAAC,CAAC;IAEF,OAAO,WAAW,CAAC;AACrB,CAAC"}
@@ -0,0 +1,17 @@
1
+ import DateTimeFormatOptions from './DateTimeFormatOptions';
2
+ import Formats from './Formats';
3
+ import IntlError from './IntlError';
4
+ import NumberFormatOptions from './NumberFormatOptions';
5
+ declare type Props = {
6
+ locale: string;
7
+ timeZone?: string;
8
+ onError?(error: IntlError): void;
9
+ formats?: Partial<Formats>;
10
+ now?: Date;
11
+ };
12
+ export default function createIntl({ formats, locale, now: globalNow, onError, timeZone }: Props): {
13
+ formatDateTime: (value: Date | number, formatOrOptions?: string | DateTimeFormatOptions | undefined) => string;
14
+ formatNumber: (value: number, formatOrOptions?: string | NumberFormatOptions | undefined) => string;
15
+ formatRelativeTime: (date: number | Date, now?: number | Date | undefined) => string;
16
+ };
17
+ export {};
@@ -0,0 +1,126 @@
1
+ import IntlError, { IntlErrorCode } from './IntlError';
2
+ import { defaultOnError } from './defaults';
3
+ const MINUTE = 60;
4
+ const HOUR = MINUTE * 60;
5
+ const DAY = HOUR * 24;
6
+ const WEEK = DAY * 7;
7
+ const MONTH = DAY * (365 / 12); // Approximation
8
+ const YEAR = DAY * 365;
9
+ function getRelativeTimeFormatConfig(seconds) {
10
+ const absValue = Math.abs(seconds);
11
+ let value, unit;
12
+ // We have to round the resulting values, as `Intl.RelativeTimeFormat`
13
+ // will include fractions like '2.1 hours ago'.
14
+ if (absValue < MINUTE) {
15
+ unit = 'second';
16
+ value = Math.round(seconds);
17
+ }
18
+ else if (absValue < HOUR) {
19
+ unit = 'minute';
20
+ value = Math.round(seconds / MINUTE);
21
+ }
22
+ else if (absValue < DAY) {
23
+ unit = 'hour';
24
+ value = Math.round(seconds / HOUR);
25
+ }
26
+ else if (absValue < WEEK) {
27
+ unit = 'day';
28
+ value = Math.round(seconds / DAY);
29
+ }
30
+ else if (absValue < MONTH) {
31
+ unit = 'week';
32
+ value = Math.round(seconds / WEEK);
33
+ }
34
+ else if (absValue < YEAR) {
35
+ unit = 'month';
36
+ value = Math.round(seconds / MONTH);
37
+ }
38
+ else {
39
+ unit = 'year';
40
+ value = Math.round(seconds / YEAR);
41
+ }
42
+ return { value, unit };
43
+ }
44
+ export default function createIntl({ formats, locale, now: globalNow, onError = defaultOnError, timeZone }) {
45
+ function resolveFormatOrOptions(typeFormats, formatOrOptions) {
46
+ let options;
47
+ if (typeof formatOrOptions === 'string') {
48
+ const formatName = formatOrOptions;
49
+ options = typeFormats?.[formatName];
50
+ if (!options) {
51
+ const error = new IntlError(IntlErrorCode.MISSING_FORMAT, __DEV__
52
+ ? `Format \`${formatName}\` is not available. You can configure it on the provider or provide custom options.`
53
+ : undefined);
54
+ onError(error);
55
+ throw error;
56
+ }
57
+ }
58
+ else {
59
+ options = formatOrOptions;
60
+ }
61
+ return options;
62
+ }
63
+ function getFormattedValue(value, formatOrOptions, typeFormats, formatter) {
64
+ let options;
65
+ try {
66
+ options = resolveFormatOrOptions(typeFormats, formatOrOptions);
67
+ }
68
+ catch (error) {
69
+ return String(value);
70
+ }
71
+ try {
72
+ return formatter(options);
73
+ }
74
+ catch (error) {
75
+ onError(new IntlError(IntlErrorCode.FORMATTING_ERROR, error.message));
76
+ return String(value);
77
+ }
78
+ }
79
+ function formatDateTime(
80
+ /** If a number is supplied, this is interpreted as a UTC timestamp. */
81
+ value,
82
+ /** If a time zone is supplied, the `value` is converted to that time zone.
83
+ * Otherwise the user time zone will be used. */
84
+ formatOrOptions) {
85
+ return getFormattedValue(value, formatOrOptions, formats?.dateTime, (options) => {
86
+ if (timeZone && !options?.timeZone) {
87
+ options = { ...options, timeZone };
88
+ }
89
+ return new Intl.DateTimeFormat(locale, options).format(value);
90
+ });
91
+ }
92
+ function formatNumber(value, formatOrOptions) {
93
+ return getFormattedValue(value, formatOrOptions, formats?.number, (options) => new Intl.NumberFormat(locale, options).format(value));
94
+ }
95
+ function formatRelativeTime(
96
+ /** The date time that needs to be formatted. */
97
+ date,
98
+ /** The reference point in time to which `date` will be formatted in relation to. */
99
+ now) {
100
+ try {
101
+ if (!now) {
102
+ if (globalNow) {
103
+ now = globalNow;
104
+ }
105
+ else {
106
+ throw new Error(__DEV__
107
+ ? `The \`now\` parameter wasn't provided to \`formatRelativeTime\` and there was no global fallback configured on the provider.`
108
+ : undefined);
109
+ }
110
+ }
111
+ const dateDate = date instanceof Date ? date : new Date(date);
112
+ const nowDate = now instanceof Date ? now : new Date(now);
113
+ const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
114
+ const { unit, value } = getRelativeTimeFormatConfig(seconds);
115
+ return new Intl.RelativeTimeFormat(locale, {
116
+ numeric: 'auto'
117
+ }).format(value, unit);
118
+ }
119
+ catch (error) {
120
+ onError(new IntlError(IntlErrorCode.FORMATTING_ERROR, error.message));
121
+ return String(date);
122
+ }
123
+ }
124
+ return { formatDateTime, formatNumber, formatRelativeTime };
125
+ }
126
+ //# sourceMappingURL=createIntl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createIntl.js","sourceRoot":"","sources":["../../../src/core/createIntl.tsx"],"names":[],"mappings":"AAEA,OAAO,SAAS,EAAE,EAAC,aAAa,EAAC,MAAM,aAAa,CAAC;AAErD,OAAO,EAAC,cAAc,EAAC,MAAM,YAAY,CAAC;AAE1C,MAAM,MAAM,GAAG,EAAE,CAAC;AAClB,MAAM,IAAI,GAAG,MAAM,GAAG,EAAE,CAAC;AACzB,MAAM,GAAG,GAAG,IAAI,GAAG,EAAE,CAAC;AACtB,MAAM,IAAI,GAAG,GAAG,GAAG,CAAC,CAAC;AACrB,MAAM,KAAK,GAAG,GAAG,GAAG,CAAC,GAAG,GAAG,EAAE,CAAC,CAAC,CAAC,gBAAgB;AAChD,MAAM,IAAI,GAAG,GAAG,GAAG,GAAG,CAAC;AAEvB,SAAS,2BAA2B,CAAC,OAAe;IAClD,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACnC,IAAI,KAAK,EAAE,IAAiC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O,EAAE,MAAM;aAChB,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;SACxB;QAAC,OAAO,KAAK,EAAE;YACd,OAAO,CACL,IAAI,SAAS,CAAC,aAAa,CAAC,gBAAgB,EAAG,KAAe,CAAC,OAAO,CAAC,CACxE,CAAC;YACF,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC;SACrB;IACH,CAAC;IAED,OAAO,EAAC,cAAc,EAAE,YAAY,EAAE,kBAAkB,EAAC,CAAC;AAC5D,CAAC"}
@@ -0,0 +1,48 @@
1
+ import Formats from './Formats';
2
+ import IntlConfiguration from './IntlConfiguration';
3
+ import TranslationValues from './TranslationValues';
4
+ import { CoreRichTranslationValues } from './createTranslatorImpl';
5
+ import MessageKeys from './utils/MessageKeys';
6
+ import NamespaceKeys from './utils/NamespaceKeys';
7
+ import NestedKeyOf from './utils/NestedKeyOf';
8
+ import NestedValueOf from './utils/NestedValueOf';
9
+ /**
10
+ * Translates messages from the given namespace by using the ICU syntax.
11
+ * See https://formatjs.io/docs/core-concepts/icu-syntax.
12
+ *
13
+ * If no namespace is provided, all available messages are returned.
14
+ * The namespace can also indicate nesting by using a dot
15
+ * (e.g. `namespace.Component`).
16
+ */
17
+ export default function createTranslator<NestedKey extends NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>> = never>({ onError, getMessageFallback, messages, namespace, ...rest }: IntlConfiguration & {
18
+ messages: IntlMessages;
19
+ namespace?: NestedKey;
20
+ }): {
21
+ <TargetKey extends MessageKeys<NestedValueOf<{
22
+ '!': IntlMessages;
23
+ }, [
24
+ NestedKey
25
+ ] extends [never] ? '!' : `!.${NestedKey}`>, NestedKeyOf<NestedValueOf<{
26
+ '!': IntlMessages;
27
+ }, [
28
+ NestedKey
29
+ ] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?: TranslationValues, formats?: Partial<Formats>): string;
30
+ rich<TargetKey extends MessageKeys<NestedValueOf<{
31
+ '!': IntlMessages;
32
+ }, [
33
+ NestedKey
34
+ ] extends [never] ? '!' : `!.${NestedKey}`>, NestedKeyOf<NestedValueOf<{
35
+ '!': IntlMessages;
36
+ }, [
37
+ NestedKey
38
+ ] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?: CoreRichTranslationValues, formats?: Partial<Formats>): string;
39
+ raw<TargetKey extends MessageKeys<NestedValueOf<{
40
+ '!': IntlMessages;
41
+ }, [
42
+ NestedKey
43
+ ] extends [never] ? '!' : `!.${NestedKey}`>, NestedKeyOf<NestedValueOf<{
44
+ '!': IntlMessages;
45
+ }, [
46
+ NestedKey
47
+ ] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey): any;
48
+ };
@@ -0,0 +1,24 @@
1
+ import createTranslatorImpl from './createTranslatorImpl';
2
+ import { defaultGetMessageFallback, defaultOnError } from './defaults';
3
+ /**
4
+ * Translates messages from the given namespace by using the ICU syntax.
5
+ * See https://formatjs.io/docs/core-concepts/icu-syntax.
6
+ *
7
+ * If no namespace is provided, all available messages are returned.
8
+ * The namespace can also indicate nesting by using a dot
9
+ * (e.g. `namespace.Component`).
10
+ */
11
+ export default function createTranslator({ onError = defaultOnError, getMessageFallback = defaultGetMessageFallback, messages, namespace, ...rest }) {
12
+ // We have to wrap the actual function so the type inference for the optional
13
+ // namespace works correctly. See https://stackoverflow.com/a/71529575/343045
14
+ // The prefix ("!") is arbitrary.
15
+ return createTranslatorImpl({
16
+ ...rest,
17
+ onError,
18
+ getMessageFallback,
19
+ messages: { '!': messages },
20
+ // @ts-ignore
21
+ namespace: namespace ? `!.${namespace}` : '!'
22
+ }, '!');
23
+ }
24
+ //# sourceMappingURL=createTranslator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createTranslator.js","sourceRoot":"","sources":["../../../src/core/createTranslator.tsx"],"names":[],"mappings":"AAGA,OAAO,oBAEN,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAC,yBAAyB,EAAE,cAAc,EAAC,MAAM,YAAY,CAAC;AAMrE;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAKtC,EACA,OAAO,GAAG,cAAc,EACxB,kBAAkB,GAAG,yBAAyB,EAC9C,QAAQ,EACR,SAAS,EACT,GAAG,IAAI,EAIR;IA4DC,6EAA6E;IAC7E,6EAA6E;IAC7E,iCAAiC;IACjC,OAAO,oBAAoB,CAIzB;QACE,GAAG,IAAI;QACP,OAAO;QACP,kBAAkB;QAClB,QAAQ,EAAE,EAAC,GAAG,EAAE,QAAQ,EAAC;QACzB,aAAa;QACb,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE,CAAC,CAAC,CAAC,GAAG;KAC9C,EACD,GAAG,CACJ,CAAC;AACJ,CAAC"}
@@ -0,0 +1,14 @@
1
+ import AbstractIntlMessages from './AbstractIntlMessages';
2
+ import { InitializedIntlConfiguration } from './IntlConfiguration';
3
+ import { TranslationValue } from './TranslationValues';
4
+ import NestedKeyOf from './utils/NestedKeyOf';
5
+ export declare type CoreRichTranslationValues = Record<string, TranslationValue | ((chunks: string) => string)>;
6
+ export declare type CreateTranslatorImplProps<Messages> = InitializedIntlConfiguration & {
7
+ namespace: string;
8
+ messages: Messages;
9
+ };
10
+ export default function createTranslatorImpl<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>({ getMessageFallback, messages, namespace, onError, ...rest }: CreateTranslatorImplProps<Messages>, namespacePrefix: string): {
11
+ (key: import("./utils/MessageKeys").default<import("./utils/NestedValueOf").default<Messages, NestedKey>, NestedKeyOf<import("./utils/NestedValueOf").default<Messages, NestedKey>>>, values?: import("./TranslationValues").default | undefined, formats?: Partial<import("./Formats").default> | undefined): string;
12
+ rich(key: string, values: CoreRichTranslationValues, formats?: Partial<import("./Formats").default> | undefined): string;
13
+ raw: (key: string) => any;
14
+ };
@@ -0,0 +1,43 @@
1
+ import IntlError, { IntlErrorCode } from './IntlError';
2
+ import createBaseTranslator, { getMessagesOrError } from './createBaseTranslator';
3
+ import resolveNamespace from './resolveNamespace';
4
+ export default function createTranslatorImpl({ getMessageFallback, messages, namespace, onError, ...rest }, namespacePrefix) {
5
+ // The `namespacePrefix` is part of the type system.
6
+ // See the comment in the function invocation.
7
+ messages = messages[namespacePrefix];
8
+ namespace = resolveNamespace(namespace, namespacePrefix);
9
+ const translator = createBaseTranslator({
10
+ ...rest,
11
+ onError,
12
+ getMessageFallback,
13
+ messagesOrError: getMessagesOrError({
14
+ messages,
15
+ namespace,
16
+ onError
17
+ })
18
+ });
19
+ const originalRich = translator.rich;
20
+ function base(...args) {
21
+ return translator(...args);
22
+ }
23
+ // Augment `t.rich` to return plain strings
24
+ base.rich = (key,
25
+ /** Key value pairs for values to interpolate into the message. */
26
+ values, formats) => {
27
+ // `chunks` is returned as a string when no React element
28
+ // is used, therefore it's safe to cast this type.
29
+ const result = originalRich(key, values, formats);
30
+ // When only string chunks are provided to the parser, only strings should be returned here.
31
+ if (typeof result !== 'string') {
32
+ const error = new IntlError(IntlErrorCode.FORMATTING_ERROR, __DEV__
33
+ ? "`createTranslator` only accepts functions for rich text formatting that receive and return strings.\n\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})"
34
+ : undefined);
35
+ onError(error);
36
+ return getMessageFallback({ error, key, namespace });
37
+ }
38
+ return result;
39
+ };
40
+ base.raw = translator.raw;
41
+ return base;
42
+ }
43
+ //# sourceMappingURL=createTranslatorImpl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createTranslatorImpl.js","sourceRoot":"","sources":["../../../src/core/createTranslatorImpl.tsx"],"names":[],"mappings":"AAEA,OAAO,SAAS,EAAE,EAAC,aAAa,EAAC,MAAM,aAAa,CAAC;AAErD,OAAO,oBAAoB,EAAE,EAAC,kBAAkB,EAAC,MAAM,wBAAwB,CAAC;AAChF,OAAO,gBAAgB,MAAM,oBAAoB,CAAC;AAclD,MAAM,CAAC,OAAO,UAAU,oBAAoB,CAI1C,EACE,kBAAkB,E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@@
1
+ import IntlError from './IntlError';
2
+ /**
3
+ * Contains defaults that are used for all entry points into the core.
4
+ * See also `InitializedIntlConfiguration`.
5
+ */
6
+ export declare function defaultGetMessageFallback(props: {
7
+ error: IntlError;
8
+ key: string;
9
+ namespace?: string;
10
+ }): string;
11
+ export declare function defaultOnError(error: IntlError): void;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Contains defaults that are used for all entry points into the core.
3
+ * See also `InitializedIntlConfiguration`.
4
+ */
5
+ export function defaultGetMessageFallback(props) {
6
+ return [props.namespace, props.key].filter((part) => part != null).join('.');
7
+ }
8
+ export function defaultOnError(error) {
9
+ console.error(error);
10
+ }
11
+ //# sourceMappingURL=defaults.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defaults.js","sourceRoot":"","sources":["../../../src/core/defaults.tsx"],"names":[],"mappings":"AAEA;;;GAGG;AAEH,MAAM,UAAU,yBAAyB,CAAC,KAIzC;IACC,OAAO,CAAC,KAAK,CAAC,SAAS,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC/E,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,KAAgB;IAC7C,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC"}
@@ -0,0 +1,8 @@
1
+ export { default as AbstractIntlMessages } from './AbstractIntlMessages';
2
+ export { default as TranslationValues, RichTranslationValues } from './TranslationValues';
3
+ export { default as Formats } from './Formats';
4
+ export { default as DateTimeFormatOptions } from './DateTimeFormatOptions';
5
+ export { default as NumberFormatOptions } from './NumberFormatOptions';
6
+ export { default as IntlError, IntlErrorCode } from './IntlError';
7
+ export { default as createTranslator } from './createTranslator';
8
+ export { default as createIntl } from './createIntl';
@@ -0,0 +1,4 @@
1
+ export { default as IntlError, IntlErrorCode } from './IntlError';
2
+ export { default as createTranslator } from './createTranslator';
3
+ export { default as createIntl } from './createIntl';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/index.tsx"],"names":[],"mappings":"AAQA,OAAO,EAAC,OAAO,IAAI,SAAS,EAAE,aAAa,EAAC,MAAM,aAAa,CAAC;AAChE,OAAO,EAAC,OAAO,IAAI,gBAAgB,EAAC,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAAC,OAAO,IAAI,UAAU,EAAC,MAAM,cAAc,CAAC"}
@@ -0,0 +1,5 @@
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
+ export default function resolveNamespace(namespace: string, namespacePrefix: string): string | undefined;
@@ -0,0 +1,10 @@
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
+ export default function resolveNamespace(namespace, namespacePrefix) {
6
+ return namespace === namespacePrefix
7
+ ? undefined
8
+ : namespace.slice((namespacePrefix + '.').length);
9
+ }
10
+ //# sourceMappingURL=resolveNamespace.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolveNamespace.js","sourceRoot":"","sources":["../../../src/core/resolveNamespace.tsx"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,OAAO,UAAU,gBAAgB,CACtC,SAAiB,EACjB,eAAuB;IAEvB,OAAO,SAAS,KAAK,eAAe;QAClC,CAAC,CAAC,SAAS;QACX,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,eAAe,GAAG,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;AACtD,CAAC"}
@@ -0,0 +1,5 @@
1
+ import NestedValueOf from './NestedValueOf';
2
+ declare type MessageKeys<ObjectType, Keys extends string> = {
3
+ [Property in Keys]: NestedValueOf<ObjectType, Property> extends string ? Property : never;
4
+ }[Keys];
5
+ export default MessageKeys;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=MessageKeys.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MessageKeys.js","sourceRoot":"","sources":["../../../../src/core/utils/MessageKeys.tsx"],"names":[],"mappings":""}
@@ -0,0 +1,5 @@
1
+ import NestedValueOf from './NestedValueOf';
2
+ declare type NamespaceKeys<ObjectType, Keys extends string> = {
3
+ [Property in Keys]: NestedValueOf<ObjectType, Property> extends string ? never : Property;
4
+ }[Keys];
5
+ export default NamespaceKeys;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=NamespaceKeys.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NamespaceKeys.js","sourceRoot":"","sources":["../../../../src/core/utils/NamespaceKeys.tsx"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ declare type NestedKeyOf<ObjectType> = ObjectType extends object ? {
2
+ [Key in keyof ObjectType]: `${Key & string}` | `${Key & string}.${NestedKeyOf<ObjectType[Key]>}`;
3
+ }[keyof ObjectType] : never;
4
+ export default NestedKeyOf;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=NestedKeyOf.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NestedKeyOf.js","sourceRoot":"","sources":["../../../../src/core/utils/NestedKeyOf.tsx"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ declare type NestedValueOf<ObjectType, Property extends string> = Property extends `${infer Key}.${infer Rest}` ? Key extends keyof ObjectType ? NestedValueOf<ObjectType[Key], Rest> : never : Property extends keyof ObjectType ? ObjectType[Property] : never;
2
+ export default NestedValueOf;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=NestedValueOf.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NestedValueOf.js","sourceRoot":"","sources":["../../../../src/core/utils/NestedValueOf.tsx"],"names":[],"mappings":""}
@@ -0,0 +1,3 @@
1
+ import AbstractIntlMessages from './AbstractIntlMessages';
2
+ import IntlError from './IntlError';
3
+ export default function validateMessages(messages: AbstractIntlMessages, onError: (error: IntlError) => void): void;