use-intl 0.0.0-canary-0303294 → 0.0.0-canary-dc800af
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/esm/development/core.js +4 -2
- package/dist/esm/development/format-message/format-only.js +145 -0
- package/dist/esm/development/format-message/index.js +138 -0
- package/dist/esm/development/formatters-r4aAmsMP.js +79 -0
- package/dist/esm/development/index.js +2 -1
- package/dist/esm/development/{initializeConfig-CIDVMS2E.js → initializeConfig-DzxNJxi9.js} +44 -219
- package/dist/esm/development/react.js +2 -1
- package/dist/esm/production/core.js +1 -1
- package/dist/esm/production/format-message/format-only.js +1 -0
- package/dist/esm/production/format-message/index.js +1 -0
- package/dist/esm/production/formatters-CJcico0N.js +1 -0
- package/dist/esm/production/index.js +1 -1
- package/dist/esm/production/initializeConfig-Brh10fyG.js +1 -0
- package/dist/esm/production/react.js +1 -1
- package/dist/types/core/createBaseTranslator.d.ts +1 -1
- package/dist/types/core/format-message/compile-format.d.ts +12 -0
- package/dist/types/core/format-message/format-only.d.ts +13 -0
- package/dist/types/core/format-message/index.d.ts +5 -0
- package/dist/types/core/format-message/types.d.ts +15 -0
- package/format-message/format-only.d.ts +2 -0
- package/format-message.d.ts +2 -0
- package/package.json +24 -5
- package/dist/esm/production/initializeConfig-CzP0yD8_.js +0 -1
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { I as IntlError, a as IntlErrorCode
|
|
1
|
+
import { c as createIntlFormatters, b as createCache } from './formatters-r4aAmsMP.js';
|
|
2
|
+
export { I as IntlError, a as IntlErrorCode } from './formatters-r4aAmsMP.js';
|
|
3
|
+
import { r as resolveNamespace, a as createBaseTranslator, d as defaultGetMessageFallback, b as defaultOnError } from './initializeConfig-DzxNJxi9.js';
|
|
4
|
+
export { c as createFormatter, i as initializeConfig } from './initializeConfig-DzxNJxi9.js';
|
|
3
5
|
|
|
4
6
|
|
|
5
7
|
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
const a$1 = 0,
|
|
2
|
+
s = 1,
|
|
3
|
+
c$1 = 2,
|
|
4
|
+
e = 3,
|
|
5
|
+
o = 4,
|
|
6
|
+
t = 5,
|
|
7
|
+
b = 6;
|
|
8
|
+
|
|
9
|
+
function f(t, r, n = {}, e) {
|
|
10
|
+
if ("string" == typeof t) return t;
|
|
11
|
+
return g(i(t, r, n, e, void 0));
|
|
12
|
+
}
|
|
13
|
+
function i(t, r, n, e, o) {
|
|
14
|
+
const u = [];
|
|
15
|
+
for (const s of t) {
|
|
16
|
+
const t = a(s, r, n, e, o);
|
|
17
|
+
Array.isArray(t) ? u.push(...t) : u.push(t);
|
|
18
|
+
}
|
|
19
|
+
return u;
|
|
20
|
+
}
|
|
21
|
+
function a(f, a, y, d, h) {
|
|
22
|
+
if ("string" == typeof f) return f;
|
|
23
|
+
if (f === a$1) {
|
|
24
|
+
const t = h;
|
|
25
|
+
return d.formatters.getNumberFormat(t.locale).format(t.value);
|
|
26
|
+
}
|
|
27
|
+
const [b$1, v, ...A] = f;
|
|
28
|
+
if (void 0 === v) {
|
|
29
|
+
const t = c(y, b$1);
|
|
30
|
+
return String(t);
|
|
31
|
+
}
|
|
32
|
+
if ("number" != typeof v || v === a$1) return function (t, r, n, e, o, u) {
|
|
33
|
+
const s = c(e, t),
|
|
34
|
+
f = s,
|
|
35
|
+
a = i(r, n, e, o, u),
|
|
36
|
+
m = g(a),
|
|
37
|
+
l = Array.isArray(m) ? m : [m];
|
|
38
|
+
return f(l);
|
|
39
|
+
}(b$1, [v, ...A], a, y, d, h);
|
|
40
|
+
switch (v) {
|
|
41
|
+
case s:
|
|
42
|
+
return function (t, r, n, e, o, u) {
|
|
43
|
+
const s = String(c(e, t)),
|
|
44
|
+
f = r[s] ?? r.other;
|
|
45
|
+
return l(f, n, e, o, u);
|
|
46
|
+
}(b$1, A[0], a, y, d, h);
|
|
47
|
+
case c$1:
|
|
48
|
+
return m(b$1, A[0], a, y, d, "cardinal");
|
|
49
|
+
case e:
|
|
50
|
+
return m(b$1, A[0], a, y, d, "ordinal");
|
|
51
|
+
case o:
|
|
52
|
+
return function (t, r, n, e, o) {
|
|
53
|
+
const u = c(e, t),
|
|
54
|
+
s = u,
|
|
55
|
+
f = function (t, r) {
|
|
56
|
+
if (!t) return;
|
|
57
|
+
if ("string" == typeof t) return r.formats?.number?.[t] ? r.formats.number[t] : void 0;
|
|
58
|
+
return t;
|
|
59
|
+
}(r, o);
|
|
60
|
+
return o.formatters.getNumberFormat(n, f).format(s);
|
|
61
|
+
}(b$1, A[0], a, y, d);
|
|
62
|
+
case t:
|
|
63
|
+
return p(b$1, A[0], a, y, d);
|
|
64
|
+
case b:
|
|
65
|
+
return p(b$1, A[0], a, y, d);
|
|
66
|
+
default:
|
|
67
|
+
return "";
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function c(t, r) {
|
|
71
|
+
return t[r];
|
|
72
|
+
}
|
|
73
|
+
function m(t, r, n, e, o, u) {
|
|
74
|
+
const s = c(e, t),
|
|
75
|
+
f = `=${s}`;
|
|
76
|
+
if (f in r) return l(r[f], n, e, o, {
|
|
77
|
+
value: s,
|
|
78
|
+
locale: n
|
|
79
|
+
});
|
|
80
|
+
return l(r[o.formatters.getPluralRules(n, {
|
|
81
|
+
type: u
|
|
82
|
+
}).select(s)] ?? r.other, n, e, o, {
|
|
83
|
+
value: s,
|
|
84
|
+
locale: n
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
function l(r, n, e, o, u) {
|
|
88
|
+
return "string" == typeof r ? r : r === a$1 ? a(r, n, e, o, u) : i(r, n, e, o, u);
|
|
89
|
+
}
|
|
90
|
+
function p(t, r, n, e, o, u) {
|
|
91
|
+
const s = c(e, t),
|
|
92
|
+
f = function (t, r, n) {
|
|
93
|
+
if (!t) return;
|
|
94
|
+
if ("string" == typeof t) {
|
|
95
|
+
const r = n.formats?.dateTime?.[t];
|
|
96
|
+
return r;
|
|
97
|
+
}
|
|
98
|
+
return t;
|
|
99
|
+
}(r, 0, o),
|
|
100
|
+
i = {
|
|
101
|
+
...f,
|
|
102
|
+
timeZone: f?.timeZone ?? o.timeZone
|
|
103
|
+
};
|
|
104
|
+
return o.formatters.getDateTimeFormat(n, i).format(s);
|
|
105
|
+
}
|
|
106
|
+
function g(t) {
|
|
107
|
+
if (0 === t.length) return "";
|
|
108
|
+
const r = [];
|
|
109
|
+
let n = "";
|
|
110
|
+
for (const e of t) "string" == typeof e ? n += e : (n && (r.push(n), n = ""), r.push(e));
|
|
111
|
+
return n && r.push(n), 1 === r.length ? r[0] : r;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Formats a precompiled ICU message using icu-minify/format.
|
|
116
|
+
* This implementation requires messages to be precompiled at build time.
|
|
117
|
+
*/
|
|
118
|
+
function formatMessage(/** The precompiled ICU message (CompiledMessage from icu-minify) */
|
|
119
|
+
...[, message, values, options]) {
|
|
120
|
+
const {
|
|
121
|
+
formats,
|
|
122
|
+
globalFormats,
|
|
123
|
+
locale,
|
|
124
|
+
...rest
|
|
125
|
+
} = options;
|
|
126
|
+
const result = f(message, locale, values, {
|
|
127
|
+
formats: {
|
|
128
|
+
dateTime: {
|
|
129
|
+
...globalFormats?.dateTime,
|
|
130
|
+
...formats?.dateTime
|
|
131
|
+
},
|
|
132
|
+
number: {
|
|
133
|
+
...globalFormats?.number,
|
|
134
|
+
...formats?.number
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
...rest
|
|
138
|
+
});
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// `t.raw` is not supported
|
|
143
|
+
formatMessage.raw = false;
|
|
144
|
+
|
|
145
|
+
export { formatMessage as default };
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { IntlMessageFormat } from 'intl-messageformat';
|
|
2
|
+
import { isValidElement } from 'react';
|
|
3
|
+
import { I as IntlError, a as IntlErrorCode, m as memoFn } from '../formatters-r4aAmsMP.js';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `intl-messageformat` uses separate keys for `date` and `time`, but there's
|
|
8
|
+
* only one native API: `Intl.DateTimeFormat`. Additionally you might want to
|
|
9
|
+
* include both a time and a date in a value, therefore the separation doesn't
|
|
10
|
+
* seem so useful. We offer a single `dateTime` namespace instead, but we have
|
|
11
|
+
* to convert the format before `intl-messageformat` can be used.
|
|
12
|
+
*/
|
|
13
|
+
function convertFormatsToIntlMessageFormat(globalFormats, inlineFormats, timeZone) {
|
|
14
|
+
const mfDateDefaults = IntlMessageFormat.formats.date;
|
|
15
|
+
const mfTimeDefaults = IntlMessageFormat.formats.time;
|
|
16
|
+
const dateTimeFormats = {
|
|
17
|
+
...globalFormats?.dateTime,
|
|
18
|
+
...inlineFormats?.dateTime
|
|
19
|
+
};
|
|
20
|
+
const allFormats = {
|
|
21
|
+
date: {
|
|
22
|
+
...mfDateDefaults,
|
|
23
|
+
...dateTimeFormats
|
|
24
|
+
},
|
|
25
|
+
time: {
|
|
26
|
+
...mfTimeDefaults,
|
|
27
|
+
...dateTimeFormats
|
|
28
|
+
},
|
|
29
|
+
number: {
|
|
30
|
+
...globalFormats?.number,
|
|
31
|
+
...inlineFormats?.number
|
|
32
|
+
}
|
|
33
|
+
// (list is not supported in ICU messages)
|
|
34
|
+
};
|
|
35
|
+
if (timeZone) {
|
|
36
|
+
// The only way to set a time zone with `intl-messageformat` is to merge it into the formats
|
|
37
|
+
// https://github.com/formatjs/formatjs/blob/8256c5271505cf2606e48e3c97ecdd16ede4f1b5/packages/intl/src/message.ts#L15
|
|
38
|
+
['date', 'time'].forEach(property => {
|
|
39
|
+
const formats = allFormats[property];
|
|
40
|
+
for (const [key, value] of Object.entries(formats)) {
|
|
41
|
+
formats[key] = {
|
|
42
|
+
timeZone,
|
|
43
|
+
...value
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return allFormats;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Placed here for improved tree shaking. Somehow when this is placed in
|
|
52
|
+
// `formatters.tsx`, then it can't be shaken off from `next-intl`.
|
|
53
|
+
function createMessageFormatter(cache, intlFormatters) {
|
|
54
|
+
const getMessageFormat = memoFn((...args) => new IntlMessageFormat(args[0], args[1], args[2], {
|
|
55
|
+
formatters: intlFormatters,
|
|
56
|
+
...args[3]
|
|
57
|
+
}), cache.message);
|
|
58
|
+
return getMessageFormat;
|
|
59
|
+
}
|
|
60
|
+
function getPlainMessage(candidate, values) {
|
|
61
|
+
// To improve runtime performance, only compile message if:
|
|
62
|
+
return (
|
|
63
|
+
// 1. Values are provided
|
|
64
|
+
values ||
|
|
65
|
+
// 2. There are escaped braces (e.g. "'{name'}")
|
|
66
|
+
/'[{}]/.test(candidate) ||
|
|
67
|
+
// 3. There are missing arguments or tags (dev-only error handling)
|
|
68
|
+
/<|{/.test(candidate) ? undefined // Compile
|
|
69
|
+
: candidate // Don't compile
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Compiles and formats an ICU message at runtime using intl-messageformat.
|
|
75
|
+
* This is the default implementation used when messages are not precompiled.
|
|
76
|
+
*/
|
|
77
|
+
function formatMessage(/** The raw ICU message string (or precompiled message, though this implementation ignores precompilation) */
|
|
78
|
+
...[key, message, values, options]) {
|
|
79
|
+
if (Array.isArray(message)) {
|
|
80
|
+
throw new IntlError(IntlErrorCode.INVALID_MESSAGE, `Message at \`${key}\` resolved to an array, but only strings are supported. See https://next-intl.dev/docs/usage/translations#arrays-of-messages` );
|
|
81
|
+
}
|
|
82
|
+
if (typeof message === 'object') {
|
|
83
|
+
throw new IntlError(IntlErrorCode.INSUFFICIENT_PATH, `Message at \`${key}\` resolved to \`${typeof message}\`, but only strings are supported. Use a \`.\` to retrieve nested messages. See https://next-intl.dev/docs/usage/translations#structuring-messages` );
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Hot path that avoids creating an `IntlMessageFormat` instance
|
|
87
|
+
if (typeof message === 'string') {
|
|
88
|
+
const plainMessage = getPlainMessage(message, values);
|
|
89
|
+
if (plainMessage) return plainMessage;
|
|
90
|
+
}
|
|
91
|
+
const {
|
|
92
|
+
cache,
|
|
93
|
+
formats,
|
|
94
|
+
formatters,
|
|
95
|
+
globalFormats,
|
|
96
|
+
locale,
|
|
97
|
+
timeZone
|
|
98
|
+
} = options;
|
|
99
|
+
|
|
100
|
+
// Lazy init the message formatter for better tree
|
|
101
|
+
// shaking in case message formatting is not used.
|
|
102
|
+
if (!formatters.getMessageFormat) {
|
|
103
|
+
formatters.getMessageFormat = createMessageFormatter(cache, formatters);
|
|
104
|
+
}
|
|
105
|
+
let messageFormat;
|
|
106
|
+
try {
|
|
107
|
+
messageFormat = formatters.getMessageFormat(message, locale, convertFormatsToIntlMessageFormat(globalFormats, formats, timeZone), {
|
|
108
|
+
formatters: {
|
|
109
|
+
...formatters,
|
|
110
|
+
getDateTimeFormat(locales, dateTimeOptions) {
|
|
111
|
+
// Workaround for https://github.com/formatjs/formatjs/issues/4279
|
|
112
|
+
return formatters.getDateTimeFormat(locales, {
|
|
113
|
+
...dateTimeOptions,
|
|
114
|
+
timeZone: dateTimeOptions?.timeZone ?? timeZone
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
} catch (error) {
|
|
120
|
+
throw new IntlError(IntlErrorCode.INVALID_MESSAGE, `${error.message} (${error.originalMessage})` );
|
|
121
|
+
}
|
|
122
|
+
const formattedMessage = messageFormat.format(
|
|
123
|
+
// @ts-expect-error `intl-messageformat` expects a different format
|
|
124
|
+
// for rich text elements since a recent minor update. This
|
|
125
|
+
// needs to be evaluated in detail, possibly also in regard
|
|
126
|
+
// to be able to format to parts.
|
|
127
|
+
values);
|
|
128
|
+
|
|
129
|
+
// Limit the function signature to return strings or React elements
|
|
130
|
+
return /*#__PURE__*/isValidElement(formattedMessage) ||
|
|
131
|
+
// Arrays of React elements
|
|
132
|
+
Array.isArray(formattedMessage) || typeof formattedMessage === 'string' ? formattedMessage : String(formattedMessage);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// `t.raw` is supported
|
|
136
|
+
formatMessage.raw = true;
|
|
137
|
+
|
|
138
|
+
export { formatMessage as default };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { memoize, strategies } from '@formatjs/fast-memoize';
|
|
2
|
+
|
|
3
|
+
class IntlError extends Error {
|
|
4
|
+
constructor(code, originalMessage) {
|
|
5
|
+
let message = code;
|
|
6
|
+
if (originalMessage) {
|
|
7
|
+
message += ': ' + originalMessage;
|
|
8
|
+
}
|
|
9
|
+
super(message);
|
|
10
|
+
this.code = code;
|
|
11
|
+
if (originalMessage) {
|
|
12
|
+
this.originalMessage = originalMessage;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
var IntlErrorCode = /*#__PURE__*/function (IntlErrorCode) {
|
|
18
|
+
IntlErrorCode["MISSING_MESSAGE"] = "MISSING_MESSAGE";
|
|
19
|
+
IntlErrorCode["MISSING_FORMAT"] = "MISSING_FORMAT";
|
|
20
|
+
IntlErrorCode["ENVIRONMENT_FALLBACK"] = "ENVIRONMENT_FALLBACK";
|
|
21
|
+
IntlErrorCode["INSUFFICIENT_PATH"] = "INSUFFICIENT_PATH";
|
|
22
|
+
IntlErrorCode["INVALID_MESSAGE"] = "INVALID_MESSAGE";
|
|
23
|
+
IntlErrorCode["INVALID_KEY"] = "INVALID_KEY";
|
|
24
|
+
IntlErrorCode["FORMATTING_ERROR"] = "FORMATTING_ERROR";
|
|
25
|
+
return IntlErrorCode;
|
|
26
|
+
}(IntlErrorCode || {});
|
|
27
|
+
|
|
28
|
+
function createCache() {
|
|
29
|
+
return {
|
|
30
|
+
dateTime: {},
|
|
31
|
+
number: {},
|
|
32
|
+
message: {},
|
|
33
|
+
relativeTime: {},
|
|
34
|
+
pluralRules: {},
|
|
35
|
+
list: {},
|
|
36
|
+
displayNames: {}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function createMemoCache(store) {
|
|
40
|
+
return {
|
|
41
|
+
create() {
|
|
42
|
+
return {
|
|
43
|
+
get(key) {
|
|
44
|
+
return store[key];
|
|
45
|
+
},
|
|
46
|
+
set(key, value) {
|
|
47
|
+
store[key] = value;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function memoFn(fn, cache) {
|
|
54
|
+
return memoize(fn, {
|
|
55
|
+
cache: createMemoCache(cache),
|
|
56
|
+
strategy: strategies.variadic
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
function memoConstructor(ConstructorFn, cache) {
|
|
60
|
+
return memoFn((...args) => new ConstructorFn(...args), cache);
|
|
61
|
+
}
|
|
62
|
+
function createIntlFormatters(cache) {
|
|
63
|
+
const getDateTimeFormat = memoConstructor(Intl.DateTimeFormat, cache.dateTime);
|
|
64
|
+
const getNumberFormat = memoConstructor(Intl.NumberFormat, cache.number);
|
|
65
|
+
const getPluralRules = memoConstructor(Intl.PluralRules, cache.pluralRules);
|
|
66
|
+
const getRelativeTimeFormat = memoConstructor(Intl.RelativeTimeFormat, cache.relativeTime);
|
|
67
|
+
const getListFormat = memoConstructor(Intl.ListFormat, cache.list);
|
|
68
|
+
const getDisplayNames = memoConstructor(Intl.DisplayNames, cache.displayNames);
|
|
69
|
+
return {
|
|
70
|
+
getDateTimeFormat,
|
|
71
|
+
getNumberFormat,
|
|
72
|
+
getPluralRules,
|
|
73
|
+
getRelativeTimeFormat,
|
|
74
|
+
getListFormat,
|
|
75
|
+
getDisplayNames
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export { IntlError as I, IntlErrorCode as a, createCache as b, createIntlFormatters as c, memoFn as m };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export { I as IntlError, a as IntlErrorCode,
|
|
1
|
+
export { I as IntlError, a as IntlErrorCode, b as _createCache, c as _createIntlFormatters } from './formatters-r4aAmsMP.js';
|
|
2
2
|
export { createTranslator, hasLocale } from './core.js';
|
|
3
|
+
export { c as createFormatter, i as initializeConfig } from './initializeConfig-DzxNJxi9.js';
|
|
3
4
|
export { IntlProvider, _useExtracted, useFormatter, useLocale, useMessages, useNow, useTimeZone, useTranslations } from './react.js';
|
|
4
5
|
|
|
5
6
|
|
|
@@ -1,76 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
class IntlError extends Error {
|
|
6
|
-
constructor(code, originalMessage) {
|
|
7
|
-
let message = code;
|
|
8
|
-
if (originalMessage) {
|
|
9
|
-
message += ': ' + originalMessage;
|
|
10
|
-
}
|
|
11
|
-
super(message);
|
|
12
|
-
this.code = code;
|
|
13
|
-
if (originalMessage) {
|
|
14
|
-
this.originalMessage = originalMessage;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
var IntlErrorCode = /*#__PURE__*/function (IntlErrorCode) {
|
|
20
|
-
IntlErrorCode["MISSING_MESSAGE"] = "MISSING_MESSAGE";
|
|
21
|
-
IntlErrorCode["MISSING_FORMAT"] = "MISSING_FORMAT";
|
|
22
|
-
IntlErrorCode["ENVIRONMENT_FALLBACK"] = "ENVIRONMENT_FALLBACK";
|
|
23
|
-
IntlErrorCode["INSUFFICIENT_PATH"] = "INSUFFICIENT_PATH";
|
|
24
|
-
IntlErrorCode["INVALID_MESSAGE"] = "INVALID_MESSAGE";
|
|
25
|
-
IntlErrorCode["INVALID_KEY"] = "INVALID_KEY";
|
|
26
|
-
IntlErrorCode["FORMATTING_ERROR"] = "FORMATTING_ERROR";
|
|
27
|
-
return IntlErrorCode;
|
|
28
|
-
}(IntlErrorCode || {});
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* `intl-messageformat` uses separate keys for `date` and `time`, but there's
|
|
32
|
-
* only one native API: `Intl.DateTimeFormat`. Additionally you might want to
|
|
33
|
-
* include both a time and a date in a value, therefore the separation doesn't
|
|
34
|
-
* seem so useful. We offer a single `dateTime` namespace instead, but we have
|
|
35
|
-
* to convert the format before `intl-messageformat` can be used.
|
|
36
|
-
*/
|
|
37
|
-
function convertFormatsToIntlMessageFormat(globalFormats, inlineFormats, timeZone) {
|
|
38
|
-
const mfDateDefaults = IntlMessageFormat.formats.date;
|
|
39
|
-
const mfTimeDefaults = IntlMessageFormat.formats.time;
|
|
40
|
-
const dateTimeFormats = {
|
|
41
|
-
...globalFormats?.dateTime,
|
|
42
|
-
...inlineFormats?.dateTime
|
|
43
|
-
};
|
|
44
|
-
const allFormats = {
|
|
45
|
-
date: {
|
|
46
|
-
...mfDateDefaults,
|
|
47
|
-
...dateTimeFormats
|
|
48
|
-
},
|
|
49
|
-
time: {
|
|
50
|
-
...mfTimeDefaults,
|
|
51
|
-
...dateTimeFormats
|
|
52
|
-
},
|
|
53
|
-
number: {
|
|
54
|
-
...globalFormats?.number,
|
|
55
|
-
...inlineFormats?.number
|
|
56
|
-
}
|
|
57
|
-
// (list is not supported in ICU messages)
|
|
58
|
-
};
|
|
59
|
-
if (timeZone) {
|
|
60
|
-
// The only way to set a time zone with `intl-messageformat` is to merge it into the formats
|
|
61
|
-
// https://github.com/formatjs/formatjs/blob/8256c5271505cf2606e48e3c97ecdd16ede4f1b5/packages/intl/src/message.ts#L15
|
|
62
|
-
['date', 'time'].forEach(property => {
|
|
63
|
-
const formats = allFormats[property];
|
|
64
|
-
for (const [key, value] of Object.entries(formats)) {
|
|
65
|
-
formats[key] = {
|
|
66
|
-
timeZone,
|
|
67
|
-
...value
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
return allFormats;
|
|
73
|
-
}
|
|
1
|
+
import { cloneElement, isValidElement } from 'react';
|
|
2
|
+
import formatMessage from 'use-intl/format-message';
|
|
3
|
+
import { I as IntlError, a as IntlErrorCode, c as createIntlFormatters, b as createCache } from './formatters-r4aAmsMP.js';
|
|
74
4
|
|
|
75
5
|
function joinPath(...parts) {
|
|
76
6
|
return parts.filter(Boolean).join('.');
|
|
@@ -88,65 +18,26 @@ function defaultOnError(error) {
|
|
|
88
18
|
console.error(error);
|
|
89
19
|
}
|
|
90
20
|
|
|
91
|
-
function
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
create() {
|
|
105
|
-
return {
|
|
106
|
-
get(key) {
|
|
107
|
-
return store[key];
|
|
108
|
-
},
|
|
109
|
-
set(key, value) {
|
|
110
|
-
store[key] = value;
|
|
111
|
-
}
|
|
21
|
+
function prepareTranslationValues(values) {
|
|
22
|
+
// Related to https://github.com/formatjs/formatjs/issues/1467
|
|
23
|
+
const transformedValues = {};
|
|
24
|
+
Object.keys(values).forEach(key => {
|
|
25
|
+
let index = 0;
|
|
26
|
+
const value = values[key];
|
|
27
|
+
let transformed;
|
|
28
|
+
if (typeof value === 'function') {
|
|
29
|
+
transformed = chunks => {
|
|
30
|
+
const result = value(chunks);
|
|
31
|
+
return /*#__PURE__*/isValidElement(result) ? /*#__PURE__*/cloneElement(result, {
|
|
32
|
+
key: key + index++
|
|
33
|
+
}) : result;
|
|
112
34
|
};
|
|
35
|
+
} else {
|
|
36
|
+
transformed = value;
|
|
113
37
|
}
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
function memoFn(fn, cache) {
|
|
117
|
-
return memoize(fn, {
|
|
118
|
-
cache: createMemoCache(cache),
|
|
119
|
-
strategy: strategies.variadic
|
|
38
|
+
transformedValues[key] = transformed;
|
|
120
39
|
});
|
|
121
|
-
|
|
122
|
-
function memoConstructor(ConstructorFn, cache) {
|
|
123
|
-
return memoFn((...args) => new ConstructorFn(...args), cache);
|
|
124
|
-
}
|
|
125
|
-
function createIntlFormatters(cache) {
|
|
126
|
-
const getDateTimeFormat = memoConstructor(Intl.DateTimeFormat, cache.dateTime);
|
|
127
|
-
const getNumberFormat = memoConstructor(Intl.NumberFormat, cache.number);
|
|
128
|
-
const getPluralRules = memoConstructor(Intl.PluralRules, cache.pluralRules);
|
|
129
|
-
const getRelativeTimeFormat = memoConstructor(Intl.RelativeTimeFormat, cache.relativeTime);
|
|
130
|
-
const getListFormat = memoConstructor(Intl.ListFormat, cache.list);
|
|
131
|
-
const getDisplayNames = memoConstructor(Intl.DisplayNames, cache.displayNames);
|
|
132
|
-
return {
|
|
133
|
-
getDateTimeFormat,
|
|
134
|
-
getNumberFormat,
|
|
135
|
-
getPluralRules,
|
|
136
|
-
getRelativeTimeFormat,
|
|
137
|
-
getListFormat,
|
|
138
|
-
getDisplayNames
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// Placed here for improved tree shaking. Somehow when this is placed in
|
|
143
|
-
// `formatters.tsx`, then it can't be shaken off from `next-intl`.
|
|
144
|
-
function createMessageFormatter(cache, intlFormatters) {
|
|
145
|
-
const getMessageFormat = memoFn((...args) => new IntlMessageFormat(args[0], args[1], args[2], {
|
|
146
|
-
formatters: intlFormatters,
|
|
147
|
-
...args[3]
|
|
148
|
-
}), cache.message);
|
|
149
|
-
return getMessageFormat;
|
|
40
|
+
return transformedValues;
|
|
150
41
|
}
|
|
151
42
|
function resolvePath(locale, messages, key, namespace) {
|
|
152
43
|
const fullKey = joinPath(namespace, key);
|
|
@@ -165,27 +56,6 @@ function resolvePath(locale, messages, key, namespace) {
|
|
|
165
56
|
});
|
|
166
57
|
return message;
|
|
167
58
|
}
|
|
168
|
-
function prepareTranslationValues(values) {
|
|
169
|
-
// Workaround for https://github.com/formatjs/formatjs/issues/1467
|
|
170
|
-
const transformedValues = {};
|
|
171
|
-
Object.keys(values).forEach(key => {
|
|
172
|
-
let index = 0;
|
|
173
|
-
const value = values[key];
|
|
174
|
-
let transformed;
|
|
175
|
-
if (typeof value === 'function') {
|
|
176
|
-
transformed = chunks => {
|
|
177
|
-
const result = value(chunks);
|
|
178
|
-
return /*#__PURE__*/isValidElement(result) ? /*#__PURE__*/cloneElement(result, {
|
|
179
|
-
key: key + index++
|
|
180
|
-
}) : result;
|
|
181
|
-
};
|
|
182
|
-
} else {
|
|
183
|
-
transformed = value;
|
|
184
|
-
}
|
|
185
|
-
transformedValues[key] = transformed;
|
|
186
|
-
});
|
|
187
|
-
return transformedValues;
|
|
188
|
-
}
|
|
189
59
|
function getMessagesOrError(locale, messages, namespace) {
|
|
190
60
|
try {
|
|
191
61
|
if (!messages) {
|
|
@@ -203,18 +73,6 @@ function getMessagesOrError(locale, messages, namespace) {
|
|
|
203
73
|
return intlError;
|
|
204
74
|
}
|
|
205
75
|
}
|
|
206
|
-
function getPlainMessage(candidate, values) {
|
|
207
|
-
// To improve runtime performance, only compile message if:
|
|
208
|
-
return (
|
|
209
|
-
// 1. Values are provided
|
|
210
|
-
values ||
|
|
211
|
-
// 2. There are escaped braces (e.g. "'{name'}")
|
|
212
|
-
/'[{}]/.test(candidate) ||
|
|
213
|
-
// 3. There are missing arguments or tags (dev-only error handling)
|
|
214
|
-
/<|{/.test(candidate) ? undefined // Compile
|
|
215
|
-
: candidate // Don't compile
|
|
216
|
-
);
|
|
217
|
-
}
|
|
218
76
|
function createBaseTranslator(config) {
|
|
219
77
|
const messagesOrError = getMessagesOrError(config.locale, config.messages, config.namespace);
|
|
220
78
|
return createBaseTranslatorImpl({
|
|
@@ -272,66 +130,28 @@ function createBaseTranslatorImpl({
|
|
|
272
130
|
}
|
|
273
131
|
}
|
|
274
132
|
}
|
|
275
|
-
if (typeof message === 'object') {
|
|
276
|
-
let code, errorMessage;
|
|
277
|
-
if (Array.isArray(message)) {
|
|
278
|
-
code = IntlErrorCode.INVALID_MESSAGE;
|
|
279
|
-
{
|
|
280
|
-
errorMessage = `Message at \`${joinPath(namespace, key)}\` resolved to an array, but only strings are supported. See https://next-intl.dev/docs/usage/translations#arrays-of-messages`;
|
|
281
|
-
}
|
|
282
|
-
} else {
|
|
283
|
-
code = IntlErrorCode.INSUFFICIENT_PATH;
|
|
284
|
-
{
|
|
285
|
-
errorMessage = `Message at \`${joinPath(namespace, key)}\` resolved to an object, but only strings are supported. Use a \`.\` to retrieve nested messages. See https://next-intl.dev/docs/usage/translations#structuring-messages`;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
return getFallbackFromErrorAndNotify(key, code, errorMessage);
|
|
289
|
-
}
|
|
290
|
-
let messageFormat;
|
|
291
|
-
|
|
292
|
-
// Hot path that avoids creating an `IntlMessageFormat` instance
|
|
293
|
-
const plainMessage = getPlainMessage(message, values);
|
|
294
|
-
if (plainMessage) return plainMessage;
|
|
295
|
-
|
|
296
|
-
// Lazy init the message formatter for better tree
|
|
297
|
-
// shaking in case message formatting is not used.
|
|
298
|
-
if (!formatters.getMessageFormat) {
|
|
299
|
-
formatters.getMessageFormat = createMessageFormatter(cache, formatters);
|
|
300
|
-
}
|
|
301
133
|
try {
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
}
|
|
134
|
+
const messagePath = joinPath(namespace, key);
|
|
135
|
+
return formatMessage(messagePath,
|
|
136
|
+
// @ts-expect-error -- We have additional validation either in `compile-format.tsx` or in case of `format-only.tsx` in the loader
|
|
137
|
+
message, values ? prepareTranslationValues(values) : values, {
|
|
138
|
+
cache,
|
|
139
|
+
formatters,
|
|
140
|
+
globalFormats,
|
|
141
|
+
formats,
|
|
142
|
+
locale,
|
|
143
|
+
timeZone
|
|
313
144
|
});
|
|
314
145
|
} catch (error) {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
// needs to be evaluated in detail, possibly also in regards
|
|
323
|
-
// to be able to format to parts.
|
|
324
|
-
values ? prepareTranslationValues(values) : values);
|
|
325
|
-
if (formattedMessage == null) {
|
|
326
|
-
throw new Error(`Unable to format \`${key}\` in ${namespace ? `namespace \`${namespace}\`` : 'messages'}` );
|
|
146
|
+
let errorCode, errorMessage;
|
|
147
|
+
if (error instanceof IntlError) {
|
|
148
|
+
errorCode = error.code;
|
|
149
|
+
errorMessage = error.originalMessage;
|
|
150
|
+
} else {
|
|
151
|
+
errorCode = IntlErrorCode.FORMATTING_ERROR;
|
|
152
|
+
errorMessage = error.message;
|
|
327
153
|
}
|
|
328
|
-
|
|
329
|
-
// Limit the function signature to return strings or React elements
|
|
330
|
-
return /*#__PURE__*/isValidElement(formattedMessage) ||
|
|
331
|
-
// Arrays of React elements
|
|
332
|
-
Array.isArray(formattedMessage) || typeof formattedMessage === 'string' ? formattedMessage : String(formattedMessage);
|
|
333
|
-
} catch (error) {
|
|
334
|
-
return getFallbackFromErrorAndNotify(key, IntlErrorCode.FORMATTING_ERROR, error.message, fallback);
|
|
154
|
+
return getFallbackFromErrorAndNotify(key, errorCode, errorMessage, fallback);
|
|
335
155
|
}
|
|
336
156
|
}
|
|
337
157
|
function translateFn(/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */
|
|
@@ -364,6 +184,11 @@ function createBaseTranslatorImpl({
|
|
|
364
184
|
return result;
|
|
365
185
|
};
|
|
366
186
|
translateFn.raw = key => {
|
|
187
|
+
{
|
|
188
|
+
if (!formatMessage.raw) {
|
|
189
|
+
throw new Error('`t.raw` is not supported when messages are precompiled.');
|
|
190
|
+
}
|
|
191
|
+
}
|
|
367
192
|
if (hasMessagesError) {
|
|
368
193
|
onError(messagesOrError);
|
|
369
194
|
return getMessageFallback({
|
|
@@ -692,4 +517,4 @@ function initializeConfig({
|
|
|
692
517
|
};
|
|
693
518
|
}
|
|
694
519
|
|
|
695
|
-
export {
|
|
520
|
+
export { createBaseTranslator as a, defaultOnError as b, createFormatter as c, defaultGetMessageFallback as d, initializeConfig as i, resolveNamespace as r };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createContext, useContext, useMemo, useState, useEffect } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { b as createCache, c as createIntlFormatters, I as IntlError, a as IntlErrorCode } from './formatters-r4aAmsMP.js';
|
|
3
|
+
import { i as initializeConfig, r as resolveNamespace, a as createBaseTranslator, c as createFormatter } from './initializeConfig-DzxNJxi9.js';
|
|
3
4
|
import { jsx } from 'react/jsx-runtime';
|
|
4
5
|
|
|
5
6
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{c as e,b as r}from"./formatters-CJcico0N.js";export{I as IntlError,a as IntlErrorCode}from"./formatters-CJcico0N.js";import{r as s,a as t,d as o,b as m}from"./initializeConfig-Brh10fyG.js";export{c as createFormatter,i as initializeConfig}from"./initializeConfig-Brh10fyG.js";function n({_cache:a=r(),_formatters:i=e(a),getMessageFallback:n=o,messages:c,namespace:f,onError:g=m,...l}){return function({messages:e,namespace:a,...r},o){return e=e[o],a=s(a,o),t({...r,messages:e,namespace:a})}({...l,onError:g,cache:a,formatters:i,getMessageFallback:n,messages:{"!":c},namespace:f?`!.${f}`:"!"},"!")}function f(e,a){return e.includes(a)}export{r as _createCache,e as _createIntlFormatters,n as createTranslator,f as hasLocale};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function t(t,n,e,o,u){const f=[];for(const i of t){const t=r(i,n,e,o,u);Array.isArray(t)?f.push(...t):f.push(t)}return f}function r(r,i,s,a,c){if("string"==typeof r)return r;if(0===r){const t=c;return a.formatters.getNumberFormat(t.locale).format(t.value)}const[m,l,...g]=r;if(void 0===l){const t=n(s,m);return String(t)}if("number"!=typeof l||0===l)return function(r,e,o,u,i,s){const a=n(u,r),c=f(t(e,o,u,i,s));return a(Array.isArray(c)?c:[c])}(m,[l,...g],i,s,a,c);switch(l){case 1:return function(t,r,e,u,f,i){return o(r[String(n(u,t))]??r.other,e,u,f,i)}(m,g[0],i,s,a,c);case 2:return e(m,g[0],i,s,a,"cardinal");case 3:return e(m,g[0],i,s,a,"ordinal");case 4:return function(t,r,e,o,u){const f=n(o,t),i=function(t,r){if(t)return"string"==typeof t?r.formats?.number?.[t]?r.formats.number[t]:void 0:t}(r,u);return u.formatters.getNumberFormat(e,i).format(f)}(m,g[0],i,s,a);case 5:case 6:return u(m,g[0],i,s,a);default:return""}}function n(t,r){return t[r]}function e(t,r,e,u,f,i){const s=n(u,t),a=`=${s}`;return o(a in r?r[a]:r[f.formatters.getPluralRules(e,{type:i}).select(s)]??r.other,e,u,f,{value:s,locale:e})}function o(n,e,o,u,f){return"string"==typeof n?n:0===n?r(n,e,o,u,f):t(n,e,o,u,f)}function u(t,r,e,o,u,f){const i=n(o,t),s=function(t,r,n){if(t){if("string"==typeof t){const r=n.formats?.dateTime?.[t];return r}return t}}(r,0,u),a={...s,timeZone:s?.timeZone??u.timeZone};return u.formatters.getDateTimeFormat(e,a).format(i)}function f(t){if(0===t.length)return"";const r=[];let n="";for(const e of t)"string"==typeof e?n+=e:(n&&(r.push(n),n=""),r.push(e));return n&&r.push(n),1===r.length?r[0]:r}function i(...[,r,n,e]){const{formats:o,globalFormats:u,locale:i,...s}=e,a=function(r,n,e={},o){return"string"==typeof r?r:f(t(r,n,e,o,void 0))}(r,i,n,{formats:{dateTime:{...u?.dateTime,...o?.dateTime},number:{...u?.number,...o?.number}},...s});return a}i.raw=!1;export{i as default};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{IntlMessageFormat as t}from"intl-messageformat";import{isValidElement as e}from"react";import{I as r,a as o,m as a}from"../formatters-CJcico0N.js";function m(...[m,s,i,n]){if(Array.isArray(s))throw new r(o.INVALID_MESSAGE,void 0);if("object"==typeof s)throw new r(o.INSUFFICIENT_PATH,void 0);if("string"==typeof s){const t=function(t,e){return e||/'[{}]/.test(t)?void 0:t}(s,i);if(t)return t}const{cache:f,formats:c,formatters:g,globalFormats:u,locale:d,timeZone:A}=n;let p;g.getMessageFormat||(g.getMessageFormat=function(e,r){return a(((...e)=>new t(e[0],e[1],e[2],{formatters:r,...e[3]})),e.message)}(f,g));try{p=g.getMessageFormat(s,d,function(e,r,o){const a=t.formats.date,m=t.formats.time,s={...e?.dateTime,...r?.dateTime},i={date:{...a,...s},time:{...m,...s},number:{...e?.number,...r?.number}};return o&&["date","time"].forEach((t=>{const e=i[t];for(const[t,r]of Object.entries(e))e[t]={timeZone:o,...r}})),i}(u,c,A),{formatters:{...g,getDateTimeFormat:(t,e)=>g.getDateTimeFormat(t,{...e,timeZone:e?.timeZone??A})}})}catch(t){throw new r(o.INVALID_MESSAGE,void 0)}const w=p.format(i);return e(w)||Array.isArray(w)||"string"==typeof w?w:String(w)}m.raw=!0;export{m as default};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{memoize as e,strategies as t}from"@formatjs/fast-memoize";class a extends Error{constructor(e,t){let a=e;t&&(a+=": "+t),super(a),this.code=e,t&&(this.originalMessage=t)}}var r=function(e){return e.MISSING_MESSAGE="MISSING_MESSAGE",e.MISSING_FORMAT="MISSING_FORMAT",e.ENVIRONMENT_FALLBACK="ENVIRONMENT_FALLBACK",e.INSUFFICIENT_PATH="INSUFFICIENT_PATH",e.INVALID_MESSAGE="INVALID_MESSAGE",e.INVALID_KEY="INVALID_KEY",e.FORMATTING_ERROR="FORMATTING_ERROR",e}(r||{});function s(){return{dateTime:{},number:{},message:{},relativeTime:{},pluralRules:{},list:{},displayNames:{}}}function i(a,r){return e(a,{cache:(s=r,{create:()=>({get:e=>s[e],set(e,t){s[e]=t}})}),strategy:t.variadic});var s}function I(e,t){return i(((...t)=>new e(...t)),t)}function l(e){return{getDateTimeFormat:I(Intl.DateTimeFormat,e.dateTime),getNumberFormat:I(Intl.NumberFormat,e.number),getPluralRules:I(Intl.PluralRules,e.pluralRules),getRelativeTimeFormat:I(Intl.RelativeTimeFormat,e.relativeTime),getListFormat:I(Intl.ListFormat,e.list),getDisplayNames:I(Intl.DisplayNames,e.displayNames)}}export{a as I,r as a,s as b,l as c,i as m};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export{I as IntlError,a as IntlErrorCode,
|
|
1
|
+
export{I as IntlError,a as IntlErrorCode,b as _createCache,c as _createIntlFormatters}from"./formatters-CJcico0N.js";export{createTranslator,hasLocale}from"./core.js";export{c as createFormatter,i as initializeConfig}from"./initializeConfig-Brh10fyG.js";export{IntlProvider,_useExtracted,useFormatter,useLocale,useMessages,useNow,useTimeZone,useTranslations}from"./react.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{cloneElement as e,isValidElement as t}from"react";import r from"use-intl/format-message";import{I as n,a as o,c as a,b as s}from"./formatters-CJcico0N.js";function c(...e){return e.filter(Boolean).join(".")}function i(e){return c(e.namespace,e.key)}function u(e){console.error(e)}function m(e,t,r,n){const o=c(n,r);if(!t)throw new Error(o);let a=t;return r.split(".").forEach((t=>{const r=a[t];if(null==t||null==r)throw new Error(o+` (${e})`);a=r})),a}function f(a){const s=function(e,t,r){try{if(!t)throw new Error(void 0);const n=r?m(e,t,r):t;if(!n)throw new Error(r);return n}catch(e){return new n(o.MISSING_MESSAGE,e.message)}}(a.locale,a.messages,a.namespace);return function({cache:a,formats:s,formatters:u,getMessageFallback:f=i,locale:l,messagesOrError:g,namespace:y,onError:h,timeZone:w}){const E=g instanceof n;function S(e,t,r,o){const a=new n(t,r);return h(a),o??f({error:a,key:e,namespace:y})}function d(i,d,p,M){const T=M;let R;if(E){if(!T)return h(g),f({error:g,key:i,namespace:y});R=T}else{const e=g;try{R=m(l,e,i,y)}catch(e){if(!T)return S(i,o.MISSING_MESSAGE,e.message,T);R=T}}try{const n=c(y,i);return r(n,R,d?function(r){const n={};return Object.keys(r).forEach((o=>{let a=0;const s=r[o];let c;c="function"==typeof s?r=>{const n=s(r);return t(n)?e(n,{key:o+a++}):n}:s,n[o]=c})),n}(d):d,{cache:a,formatters:u,globalFormats:s,formats:p,locale:l,timeZone:w})}catch(e){let t,r;return e instanceof n?(t=e.code,r=e.originalMessage):(t=o.FORMATTING_ERROR,r=e.message),S(i,t,r,T)}}function p(e,t,r,n){const a=d(e,t,r,n);return"string"!=typeof a?S(e,o.INVALID_MESSAGE,void 0):a}return p.rich=d,p.markup=(e,t,r,n)=>d(e,t,r,n),p.raw=e=>{if(E)return h(g),f({error:g,key:e,namespace:y});const t=g;try{return m(l,t,e,y)}catch(t){return S(e,o.MISSING_MESSAGE,t.message)}},p.has=e=>{if(E)return!1;try{return m(l,g,e,y),!0}catch{return!1}},p}({...a,messagesOrError:s})}function l(e,t){return e===t?void 0:e.slice((t+".").length)}const g=3600,y=86400,h=7*y,w=2628e3,E=7884e3,S=365*y,d={second:1,seconds:1,minute:60,minutes:60,hour:g,hours:g,day:y,days:y,week:h,weeks:h,month:w,months:w,quarter:E,quarters:E,year:S,years:S};function p(e){const{_cache:t=s(),_formatters:r=a(t),formats:c,locale:i,onError:m=u,timeZone:f}=e;function l(e){return e?.timeZone||(f?e={...e,timeZone:f}:m(new n(o.ENVIRONMENT_FALLBACK,void 0))),e}function E(e,t,r,a,s){let c;try{c=function(e,t,r){let a;if("string"==typeof t){const r=t;if(a=e?.[r],!a){const e=new n(o.MISSING_FORMAT,void 0);throw m(e),e}}else a=t;return r&&(a={...a,...r}),a}(r,e,t)}catch{return s()}try{return a(c)}catch(e){return m(new n(o.FORMATTING_ERROR,e.message)),s()}}function p(e,t,n){return E(t,n,c?.dateTime,(t=>(t=l(t),r.getDateTimeFormat(i,t).format(e))),(()=>String(e)))}function M(){return e.now?e.now:(m(new n(o.ENVIRONMENT_FALLBACK,void 0)),new Date)}return{dateTime:p,number:function(e,t,n){return E(t,n,c?.number,(t=>r.getNumberFormat(i,t).format(e)),(()=>String(e)))},relativeTime:function(e,t){try{let n,o;const a={};t instanceof Date||"number"==typeof t?n=new Date(t):t&&(n=null!=t.now?new Date(t.now):M(),o=t.unit,a.style=t.style,a.numberingSystem=t.numberingSystem),n||(n=M());const s=(new Date(e).getTime()-n.getTime())/1e3;o||(o=function(e){const t=Math.abs(e);return t<60?"second":t<g?"minute":t<y?"hour":t<h?"day":t<w?"week":t<S?"month":"year"}(s)),a.numeric="second"===o?"auto":"always";const c=function(e,t){return Math.round(e/d[t])}(s,o);return r.getRelativeTimeFormat(i,a).format(c,o)}catch(t){return m(new n(o.FORMATTING_ERROR,t.message)),String(e)}},list:function(e,t,n){const o=[],a=new Map;let s=0;for(const t of e){let e;"object"==typeof t?(e=String(s),a.set(e,t)):e=String(t),o.push(e),s++}return E(t,n,c?.list,(e=>{const t=r.getListFormat(i,e).formatToParts(o).map((e=>"literal"===e.type?e.value:a.get(e.value)||e.value));return a.size>0?t:t.join("")}),(()=>String(e)))},dateTimeRange:function(e,t,n,o){return E(n,o,c?.dateTime,(n=>(n=l(n),r.getDateTimeFormat(i,n).formatRange(e,t))),(()=>[p(e),p(t)].join(" – ")))}}}function M({formats:e,getMessageFallback:t,messages:r,onError:n,...o}){return{...o,formats:e||void 0,messages:r||void 0,onError:n||u,getMessageFallback:t||i}}export{f as a,u as b,p as c,i as d,M as i,l as r};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createContext as e,useContext as r,useMemo as t,useState as o,useEffect as n}from"react";import{
|
|
1
|
+
import{createContext as e,useContext as r,useMemo as t,useState as o,useEffect as n}from"react";import{b as a,c as s,I as c,a as i}from"./formatters-CJcico0N.js";import{i as m,r as f,a as u,c as l}from"./initializeConfig-Brh10fyG.js";import{jsx as g}from"react/jsx-runtime";const d=e(void 0);function v({children:e,formats:o,getMessageFallback:n,locale:c,messages:i,now:f,onError:u,timeZone:l}){const v=r(d),w=t((()=>v?.cache||a()),[c,v?.cache]),p=t((()=>v?.formatters||s(w)),[w,v?.formatters]),h=t((()=>({...m({locale:c,formats:void 0===o?v?.formats:o,getMessageFallback:n||v?.getMessageFallback,messages:void 0===i?v?.messages:i,now:f||v?.now,onError:u||v?.onError,timeZone:l||v?.timeZone}),formatters:p,cache:w})),[w,o,p,n,c,i,f,u,v,l]);return g(d.Provider,{value:h,children:e})}function w(){const e=r(d);if(!e)throw new Error(void 0);return e}let p=!1;const h="undefined"==typeof window;function E(e){return function(e,r,o){const{cache:n,formats:a,formatters:s,getMessageFallback:m,locale:l,onError:g,timeZone:d}=w(),v=e[o],E=f(r,o);return d||p||!h||(p=!0,g(new c(i.ENVIRONMENT_FALLBACK,void 0))),t((()=>u({cache:n,formatters:s,getMessageFallback:m,messages:v,namespace:E,onError:g,formats:a,locale:l,timeZone:d})),[n,s,m,v,E,g,a,l,d])}({"!":w().messages},e?`!.${e}`:"!","!")}function Z(){return w().locale}function k(){return new Date}function b(e){const r=e?.updateInterval,{now:t}=w(),[a,s]=o(t||k());return n((()=>{if(!r)return;const e=setInterval((()=>{s(k())}),r);return()=>{clearInterval(e)}}),[t,r]),null==r&&t?t:a}function F(){return w().timeZone}function M(){const e=w();if(!e.messages)throw new Error(void 0);return e.messages}function I(){const{formats:e,formatters:r,locale:o,now:n,onError:a,timeZone:s}=w();return t((()=>l({formats:e,locale:o,now:n,onError:a,timeZone:s,_formatters:r})),[e,r,n,o,a,s])}function j(e,...r){let t,o;return"string"==typeof e?(t=r[0],o=r[1]):(t=e.values,o=e.formats),[void 0,t,o,void 0]}function x(e){const r=E(e);function t(...e){return r(...j(...e))}return t.rich=(...e)=>r.rich(...j(...e)),t.markup=(...e)=>r.markup(...j(...e)),t.has=function(e){return!0},t}export{v as IntlProvider,x as _useExtracted,I as useFormatter,Z as useLocale,M as useMessages,b as useNow,F as useTimeZone,E as useTranslations};
|
|
@@ -5,7 +5,7 @@ import type { InitializedIntlConfig } from './IntlConfig.js';
|
|
|
5
5
|
import IntlError from './IntlError.js';
|
|
6
6
|
import type { MessageKeys, NestedKeyOf, NestedValueOf } from './MessageKeys.js';
|
|
7
7
|
import type { MarkupTranslationValues, RichTranslationValues, TranslationValues } from './TranslationValues.js';
|
|
8
|
-
import {
|
|
8
|
+
import type { Formatters, IntlCache } from './formatters.js';
|
|
9
9
|
export type CreateBaseTranslatorProps<Messages> = InitializedIntlConfig & {
|
|
10
10
|
cache: IntlCache;
|
|
11
11
|
formatters: Formatters;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { FormatMessage } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Compiles and formats an ICU message at runtime using intl-messageformat.
|
|
4
|
+
* This is the default implementation used when messages are not precompiled.
|
|
5
|
+
*/
|
|
6
|
+
declare function formatMessage(
|
|
7
|
+
/** The raw ICU message string (or precompiled message, though this implementation ignores precompilation) */
|
|
8
|
+
...[key, message, values, options]: Parameters<FormatMessage<string>>): ReturnType<FormatMessage<string>>;
|
|
9
|
+
declare namespace formatMessage {
|
|
10
|
+
var raw: boolean;
|
|
11
|
+
}
|
|
12
|
+
export default formatMessage;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type CompiledMessage } from 'icu-minify/format';
|
|
2
|
+
import type { FormatMessage } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Formats a precompiled ICU message using icu-minify/format.
|
|
5
|
+
* This implementation requires messages to be precompiled at build time.
|
|
6
|
+
*/
|
|
7
|
+
declare function formatMessage(
|
|
8
|
+
/** The precompiled ICU message (CompiledMessage from icu-minify) */
|
|
9
|
+
...[, message, values, options]: Parameters<FormatMessage<CompiledMessage>>): ReturnType<FormatMessage<CompiledMessage>>;
|
|
10
|
+
declare namespace formatMessage {
|
|
11
|
+
var raw: boolean;
|
|
12
|
+
}
|
|
13
|
+
export default formatMessage;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type Formats from '../Formats.js';
|
|
3
|
+
import type TimeZone from '../TimeZone.js';
|
|
4
|
+
import type { RichTranslationValues } from '../TranslationValues.js';
|
|
5
|
+
import type { Formatters, IntlCache } from '../formatters.js';
|
|
6
|
+
type FormatMessageOptions = {
|
|
7
|
+
cache: IntlCache;
|
|
8
|
+
formatters: Formatters;
|
|
9
|
+
globalFormats?: Formats;
|
|
10
|
+
formats?: Formats;
|
|
11
|
+
locale: string;
|
|
12
|
+
timeZone?: TimeZone;
|
|
13
|
+
};
|
|
14
|
+
export type FormatMessage<Message> = (key: string, message: Message, values: RichTranslationValues | undefined, options: FormatMessageOptions) => ReactNode;
|
|
15
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "use-intl",
|
|
3
|
-
"version": "0.0.0-canary-
|
|
3
|
+
"version": "0.0.0-canary-dc800af",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"author": "Jan Amann <jan@amann.work>",
|
|
6
|
+
"funding": [
|
|
7
|
+
{
|
|
8
|
+
"type": "individual",
|
|
9
|
+
"url": "https://github.com/sponsors/amannn"
|
|
10
|
+
}
|
|
11
|
+
],
|
|
6
12
|
"description": "Internationalization (i18n) for React",
|
|
7
13
|
"license": "MIT",
|
|
8
14
|
"homepage": "https://next-intl.dev",
|
|
@@ -38,12 +44,24 @@
|
|
|
38
44
|
"types": "./dist/types/react.d.ts",
|
|
39
45
|
"development": "./dist/esm/development/react.js",
|
|
40
46
|
"default": "./dist/esm/production/react.js"
|
|
47
|
+
},
|
|
48
|
+
"./format-message": {
|
|
49
|
+
"types": "./dist/types/core/format-message/index.d.ts",
|
|
50
|
+
"development": "./dist/esm/development/format-message/index.js",
|
|
51
|
+
"default": "./dist/esm/production/format-message/index.js"
|
|
52
|
+
},
|
|
53
|
+
"./format-message/format-only": {
|
|
54
|
+
"types": "./dist/types/core/format-message/format-only.d.ts",
|
|
55
|
+
"development": "./dist/esm/development/format-message/format-only.js",
|
|
56
|
+
"default": "./dist/esm/production/format-message/format-only.js"
|
|
41
57
|
}
|
|
42
58
|
},
|
|
43
59
|
"files": [
|
|
44
60
|
"dist",
|
|
45
61
|
"core.d.ts",
|
|
46
|
-
"react.d.ts"
|
|
62
|
+
"react.d.ts",
|
|
63
|
+
"format-message.d.ts",
|
|
64
|
+
"format-message"
|
|
47
65
|
],
|
|
48
66
|
"keywords": [
|
|
49
67
|
"react",
|
|
@@ -57,12 +75,13 @@
|
|
|
57
75
|
"formatting"
|
|
58
76
|
],
|
|
59
77
|
"dependencies": {
|
|
60
|
-
"@formatjs/fast-memoize": "^
|
|
78
|
+
"@formatjs/fast-memoize": "^3.1.0",
|
|
61
79
|
"@schummar/icu-type-parser": "1.21.5",
|
|
62
|
-
"
|
|
80
|
+
"icu-minify": "^0.0.0-canary-dc800af",
|
|
81
|
+
"intl-messageformat": "^11.1.0"
|
|
63
82
|
},
|
|
64
83
|
"peerDependencies": {
|
|
65
84
|
"react": "^17.0.0 || ^18.0.0 || >=19.0.0-rc <19.0.0 || ^19.0.0"
|
|
66
85
|
},
|
|
67
|
-
"gitHead": "
|
|
86
|
+
"gitHead": "cb3f1d97f6593c4797c260adea0d0c4b2a8d72b3"
|
|
68
87
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{IntlMessageFormat as e}from"intl-messageformat";import{isValidElement as t,cloneElement as r}from"react";import{memoize as n,strategies as o}from"@formatjs/fast-memoize";class a extends Error{constructor(e,t){let r=e;t&&(r+=": "+t),super(r),this.code=e,t&&(this.originalMessage=t)}}var s=function(e){return e.MISSING_MESSAGE="MISSING_MESSAGE",e.MISSING_FORMAT="MISSING_FORMAT",e.ENVIRONMENT_FALLBACK="ENVIRONMENT_FALLBACK",e.INSUFFICIENT_PATH="INSUFFICIENT_PATH",e.INVALID_MESSAGE="INVALID_MESSAGE",e.INVALID_KEY="INVALID_KEY",e.FORMATTING_ERROR="FORMATTING_ERROR",e}(s||{});function i(...e){return e.filter(Boolean).join(".")}function u(e){return i(e.namespace,e.key)}function c(e){console.error(e)}function m(){return{dateTime:{},number:{},message:{},relativeTime:{},pluralRules:{},list:{},displayNames:{}}}function f(e,t){return n(e,{cache:(r=t,{create:()=>({get:e=>r[e],set(e,t){r[e]=t}})}),strategy:o.variadic});var r}function l(e,t){return f(((...t)=>new e(...t)),t)}function g(e){return{getDateTimeFormat:l(Intl.DateTimeFormat,e.dateTime),getNumberFormat:l(Intl.NumberFormat,e.number),getPluralRules:l(Intl.PluralRules,e.pluralRules),getRelativeTimeFormat:l(Intl.RelativeTimeFormat,e.relativeTime),getListFormat:l(Intl.ListFormat,e.list),getDisplayNames:l(Intl.DisplayNames,e.displayNames)}}function I(e,t,r,n){const o=i(n,r);if(!t)throw new Error(o);let a=t;return r.split(".").forEach((t=>{const r=a[t];if(null==t||null==r)throw new Error(o+` (${e})`);a=r})),a}function E(n){const o=function(e,t,r){try{if(!t)throw new Error(void 0);const n=r?I(e,t,r):t;if(!n)throw new Error(r);return n}catch(e){return new a(s.MISSING_MESSAGE,e.message)}}(n.locale,n.messages,n.namespace);return function({cache:n,formats:o,formatters:i,getMessageFallback:c=u,locale:m,messagesOrError:l,namespace:g,onError:E,timeZone:S}){const T=l instanceof a;function N(e,t,r,n){const o=new a(t,r);return E(o),n??c({error:o,key:e,namespace:g})}function y(a,u,y,A){const M=A;let F,R;if(T){if(!M)return E(l),c({error:l,key:a,namespace:g});F=M}else{const e=l;try{F=I(m,e,a,g)}catch(e){if(!M)return N(a,s.MISSING_MESSAGE,e.message,M);F=M}}if("object"==typeof F){let e,t;return e=Array.isArray(F)?s.INVALID_MESSAGE:s.INSUFFICIENT_PATH,N(a,e,t)}const d=function(e,t){return t||/'[{}]/.test(e)?void 0:e}(F,u);if(d)return d;i.getMessageFormat||(i.getMessageFormat=function(t,r){return f(((...t)=>new e(t[0],t[1],t[2],{formatters:r,...t[3]})),t.message)}(n,i));try{R=i.getMessageFormat(F,m,function(t,r,n){const o=e.formats.date,a=e.formats.time,s={...t?.dateTime,...r?.dateTime},i={date:{...o,...s},time:{...a,...s},number:{...t?.number,...r?.number}};return n&&["date","time"].forEach((e=>{const t=i[e];for(const[e,r]of Object.entries(t))t[e]={timeZone:n,...r}})),i}(o,y,S),{formatters:{...i,getDateTimeFormat:(e,t)=>i.getDateTimeFormat(e,{timeZone:S,...t})}})}catch(e){const t=e;return N(a,s.INVALID_MESSAGE,t.message,M)}try{const e=R.format(u?function(e){const n={};return Object.keys(e).forEach((o=>{let a=0;const s=e[o];let i;i="function"==typeof s?e=>{const n=s(e);return t(n)?r(n,{key:o+a++}):n}:s,n[o]=i})),n}(u):u);if(null==e)throw new Error(void 0);return t(e)||Array.isArray(e)||"string"==typeof e?e:String(e)}catch(e){return N(a,s.FORMATTING_ERROR,e.message,M)}}function A(e,t,r,n){const o=y(e,t,r,n);return"string"!=typeof o?N(e,s.INVALID_MESSAGE,void 0):o}return A.rich=y,A.markup=(e,t,r,n)=>y(e,t,r,n),A.raw=e=>{if(T)return E(l),c({error:l,key:e,namespace:g});const t=l;try{return I(m,t,e,g)}catch(t){return N(e,s.MISSING_MESSAGE,t.message)}},A.has=e=>{if(T)return!1;try{return I(m,l,e,g),!0}catch{return!1}},A}({...n,messagesOrError:o})}function S(e,t){return e===t?void 0:e.slice((t+".").length)}const T=3600,N=86400,y=7*N,A=2628e3,M=7884e3,F=365*N,R={second:1,seconds:1,minute:60,minutes:60,hour:T,hours:T,day:N,days:N,week:y,weeks:y,month:A,months:A,quarter:M,quarters:M,year:F,years:F};function d(e){const{_cache:t=m(),_formatters:r=g(t),formats:n,locale:o,onError:i=c,timeZone:u}=e;function f(e){return e?.timeZone||(u?e={...e,timeZone:u}:i(new a(s.ENVIRONMENT_FALLBACK,void 0))),e}function l(e,t,r,n,o){let u;try{u=function(e,t,r){let n;if("string"==typeof t){const r=t;if(n=e?.[r],!n){const e=new a(s.MISSING_FORMAT,void 0);throw i(e),e}}else n=t;return r&&(n={...n,...r}),n}(r,e,t)}catch{return o()}try{return n(u)}catch(e){return i(new a(s.FORMATTING_ERROR,e.message)),o()}}function I(e,t,a){return l(t,a,n?.dateTime,(t=>(t=f(t),r.getDateTimeFormat(o,t).format(e))),(()=>String(e)))}function E(){return e.now?e.now:(i(new a(s.ENVIRONMENT_FALLBACK,void 0)),new Date)}return{dateTime:I,number:function(e,t,a){return l(t,a,n?.number,(t=>r.getNumberFormat(o,t).format(e)),(()=>String(e)))},relativeTime:function(e,t){try{let n,a;const s={};t instanceof Date||"number"==typeof t?n=new Date(t):t&&(n=null!=t.now?new Date(t.now):E(),a=t.unit,s.style=t.style,s.numberingSystem=t.numberingSystem),n||(n=E());const i=(new Date(e).getTime()-n.getTime())/1e3;a||(a=function(e){const t=Math.abs(e);return t<60?"second":t<T?"minute":t<N?"hour":t<y?"day":t<A?"week":t<F?"month":"year"}(i)),s.numeric="second"===a?"auto":"always";const u=function(e,t){return Math.round(e/R[t])}(i,a);return r.getRelativeTimeFormat(o,s).format(u,a)}catch(t){return i(new a(s.FORMATTING_ERROR,t.message)),String(e)}},list:function(e,t,a){const s=[],i=new Map;let u=0;for(const t of e){let e;"object"==typeof t?(e=String(u),i.set(e,t)):e=String(t),s.push(e),u++}return l(t,a,n?.list,(e=>{const t=r.getListFormat(o,e).formatToParts(s).map((e=>"literal"===e.type?e.value:i.get(e.value)||e.value));return i.size>0?t:t.join("")}),(()=>String(e)))},dateTimeRange:function(e,t,a,s){return l(a,s,n?.dateTime,(n=>(n=f(n),r.getDateTimeFormat(o,n).formatRange(e,t))),(()=>[I(e),I(t)].join(" – ")))}}}function h({formats:e,getMessageFallback:t,messages:r,onError:n,...o}){return{...o,formats:e||void 0,messages:r||void 0,onError:n||c,getMessageFallback:t||u}}export{a as I,s as a,g as b,d as c,m as d,E as e,u as f,c as g,h as i,S as r};
|