use-intl 3.0.0-rc.5 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -15
- package/dist/development/_IntlProvider.js +31 -5
- package/dist/development/core.js +3 -28
- package/dist/development/{createFormatter-bcc5a49f.js → createFormatter-4848f6c8.js} +23 -0
- package/dist/development/index.js +1 -2
- package/dist/development/react.js +1 -1
- package/dist/esm/_IntlProvider.js +1 -1
- package/dist/esm/core.js +1 -1
- package/dist/esm/createFormatter-c871a8ce.js +1 -0
- package/dist/esm/index.js +1 -1
- package/dist/esm/react.js +1 -1
- package/dist/production/_IntlProvider.js +1 -1
- package/dist/production/core.js +1 -1
- package/dist/production/{createFormatter-8c0b8eb2.js → createFormatter-7a8c6f36.js} +1 -1
- package/dist/production/index.js +1 -1
- package/dist/production/react.js +1 -1
- package/dist/types/src/core/TranslationValues.d.ts +1 -1
- package/dist/types/src/core/createBaseTranslator.d.ts +2 -1
- package/dist/types/src/core/createTranslator.d.ts +16 -3
- package/dist/types/src/core/createTranslatorImpl.d.ts +7 -4
- package/dist/types/src/core/index.d.ts +1 -2
- package/dist/types/src/react/IntlProvider.d.ts +2 -2
- package/dist/types/src/react/useTranslations.d.ts +10 -1
- package/dist/types/src/react/useTranslationsImpl.d.ts +1 -0
- package/dist/types/test/react/IntlProvider.test.d.ts +1 -0
- package/package.json +5 -4
- package/dist/esm/createFormatter-cccbd794.js +0 -1
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
Internationalization is an essential part of the user experience. use-intl gives you everything you need to get language subtleties right and has always got your back whenever you need to fine-tune a translation.
|
|
10
10
|
|
|
11
|
-
- 🌟 **ICU message syntax**: Localize your messages with interpolation,
|
|
11
|
+
- 🌟 **ICU message syntax**: Localize your messages with interpolation, cardinal & ordinal plurals, enum-based label selection and rich text.
|
|
12
12
|
- 📅 **Dates, times & numbers**: Apply appropriate formatting without worrying about server/client differences like time zones.
|
|
13
13
|
- ✅ **Type-safe**: Speed up development with autocompletion for message keys and catch typos early with compile-time checks.
|
|
14
14
|
- 💡 **Hooks-only API**: Learn a single API that can be used across your code base to turn translations into plain strings or rich text.
|
|
@@ -19,19 +19,17 @@ Internationalization is an essential part of the user experience. use-intl gives
|
|
|
19
19
|
This library is based on the premise that messages can be grouped by namespaces (typically a component name).
|
|
20
20
|
|
|
21
21
|
```jsx
|
|
22
|
-
//
|
|
23
|
-
import {useTranslations
|
|
22
|
+
// UserProfile.tsx
|
|
23
|
+
import {useTranslations} from 'next-intl';
|
|
24
24
|
|
|
25
|
-
function
|
|
26
|
-
const t = useTranslations('
|
|
27
|
-
const format = useFormatter();
|
|
25
|
+
export default function UserProfile({user}) {
|
|
26
|
+
const t = useTranslations('UserProfile');
|
|
28
27
|
|
|
29
28
|
return (
|
|
30
29
|
<section>
|
|
31
|
-
<
|
|
32
|
-
<p>{t('
|
|
33
|
-
<p>{t('
|
|
34
|
-
<Image alt={t('portrait', {username: user.name})} src={user.portrait} />
|
|
30
|
+
<h1>{t('title', {firstName: user.firstName})}</h1>
|
|
31
|
+
<p>{t('membership', {memberSince: user.memberSince})}</p>
|
|
32
|
+
<p>{t('followers', {count: user.numFollowers})}</p>
|
|
35
33
|
</section>
|
|
36
34
|
);
|
|
37
35
|
}
|
|
@@ -40,15 +38,14 @@ function UserDetails({user}) {
|
|
|
40
38
|
```js
|
|
41
39
|
// en.json
|
|
42
40
|
{
|
|
43
|
-
"
|
|
44
|
-
"title": "
|
|
41
|
+
"UserProfile": {
|
|
42
|
+
"title": "{username}'s profile",
|
|
43
|
+
"membership": "Member since {memberSince, date, short}",
|
|
45
44
|
"followers": "{count, plural, ↵
|
|
46
45
|
=0 {No followers yet} ↵
|
|
47
46
|
=1 {One follower} ↵
|
|
48
47
|
other {# followers} ↵
|
|
49
|
-
}"
|
|
50
|
-
"lastSeen": "Last seen {time}",
|
|
51
|
-
"portrait": "Portrait of {username}"
|
|
48
|
+
}"
|
|
52
49
|
}
|
|
53
50
|
}
|
|
54
51
|
```
|
|
@@ -13,14 +13,40 @@ var React__default = /*#__PURE__*/_interopDefault(React);
|
|
|
13
13
|
function IntlProvider(_ref) {
|
|
14
14
|
let {
|
|
15
15
|
children,
|
|
16
|
-
|
|
16
|
+
defaultTranslationValues,
|
|
17
|
+
formats,
|
|
18
|
+
getMessageFallback,
|
|
19
|
+
locale,
|
|
20
|
+
messages,
|
|
21
|
+
now,
|
|
22
|
+
onError,
|
|
23
|
+
timeZone
|
|
17
24
|
} = _ref;
|
|
18
25
|
const [messageFormatCache] = React.useState(() => new Map());
|
|
26
|
+
|
|
27
|
+
// Memoizing this value helps to avoid triggering a re-render of all
|
|
28
|
+
// context consumers in case the configuration didn't change. However,
|
|
29
|
+
// if some of the non-primitive values change, a re-render will still
|
|
30
|
+
// be triggered. Note that there's no need to put `memo` on `IntlProvider`
|
|
31
|
+
// itself, because the `children` typically change on every render.
|
|
32
|
+
// There's some burden on the consumer side if it's important to reduce
|
|
33
|
+
// re-renders, put that's how React works.
|
|
34
|
+
// See: https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-mostly-complete-guide-to-react-rendering-behavior/#context-updates-and-render-optimizations
|
|
35
|
+
const value = React.useMemo(() => ({
|
|
36
|
+
...initializeConfig.initializeConfig({
|
|
37
|
+
locale,
|
|
38
|
+
defaultTranslationValues,
|
|
39
|
+
formats,
|
|
40
|
+
getMessageFallback,
|
|
41
|
+
messages,
|
|
42
|
+
now,
|
|
43
|
+
onError,
|
|
44
|
+
timeZone
|
|
45
|
+
}),
|
|
46
|
+
messageFormatCache
|
|
47
|
+
}), [defaultTranslationValues, formats, getMessageFallback, locale, messageFormatCache, messages, now, onError, timeZone]);
|
|
19
48
|
return /*#__PURE__*/React__default.default.createElement(IntlContext.IntlContext.Provider, {
|
|
20
|
-
value:
|
|
21
|
-
...initializeConfig.initializeConfig(config),
|
|
22
|
-
messageFormatCache
|
|
23
|
-
}
|
|
49
|
+
value: value
|
|
24
50
|
}, children);
|
|
25
51
|
}
|
|
26
52
|
|
package/dist/development/core.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var initializeConfig = require('./initializeConfig-0a0b87a7.js');
|
|
6
|
-
var createFormatter = require('./createFormatter-
|
|
6
|
+
var createFormatter = require('./createFormatter-4848f6c8.js');
|
|
7
7
|
require('intl-messageformat');
|
|
8
8
|
require('react');
|
|
9
9
|
|
|
@@ -19,38 +19,13 @@ function createTranslatorImpl(_ref, namespacePrefix) {
|
|
|
19
19
|
// See the comment in the function invocation.
|
|
20
20
|
messages = messages[namespacePrefix];
|
|
21
21
|
namespace = createFormatter.resolveNamespace(namespace, namespacePrefix);
|
|
22
|
-
|
|
22
|
+
return createFormatter.createBaseTranslator({
|
|
23
23
|
...rest,
|
|
24
24
|
onError,
|
|
25
25
|
getMessageFallback,
|
|
26
26
|
messages,
|
|
27
27
|
namespace
|
|
28
28
|
});
|
|
29
|
-
const originalRich = translator.rich;
|
|
30
|
-
function base() {
|
|
31
|
-
return translator(...arguments);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// Augment `t.rich` to return plain strings
|
|
35
|
-
base.rich = (key, values, formats) => {
|
|
36
|
-
// `chunks` is returned as a string when no React element
|
|
37
|
-
// is used, therefore it's safe to cast this type.
|
|
38
|
-
const result = originalRich(key, values, formats);
|
|
39
|
-
|
|
40
|
-
// When only string chunks are provided to the parser, only strings should be returned here.
|
|
41
|
-
if (typeof result !== 'string') {
|
|
42
|
-
const error = new initializeConfig.IntlError(initializeConfig.IntlErrorCode.FORMATTING_ERROR, "`createTranslator` only accepts functions for rich text formatting that receive and return strings.\n\nE.g. t.rich('rich', {b: (chunks) => `<b>${chunks}</b>`})" );
|
|
43
|
-
onError(error);
|
|
44
|
-
return getMessageFallback({
|
|
45
|
-
error,
|
|
46
|
-
key,
|
|
47
|
-
namespace
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
return result;
|
|
51
|
-
};
|
|
52
|
-
base.raw = translator.raw;
|
|
53
|
-
return base;
|
|
54
29
|
}
|
|
55
30
|
|
|
56
31
|
/**
|
|
@@ -76,6 +51,7 @@ function createTranslator(_ref) {
|
|
|
76
51
|
...rest,
|
|
77
52
|
onError,
|
|
78
53
|
getMessageFallback,
|
|
54
|
+
// @ts-expect-error `messages` is allowed to be `undefined` here and will be handled internally
|
|
79
55
|
messages: {
|
|
80
56
|
'!': messages
|
|
81
57
|
},
|
|
@@ -86,6 +62,5 @@ function createTranslator(_ref) {
|
|
|
86
62
|
exports.IntlError = initializeConfig.IntlError;
|
|
87
63
|
exports.IntlErrorCode = initializeConfig.IntlErrorCode;
|
|
88
64
|
exports.initializeConfig = initializeConfig.initializeConfig;
|
|
89
|
-
exports.createBaseTranslator = createFormatter.createBaseTranslator;
|
|
90
65
|
exports.createFormatter = createFormatter.createFormatter;
|
|
91
66
|
exports.createTranslator = createTranslator;
|
|
@@ -245,6 +245,29 @@ function createBaseTranslatorImpl(_ref2) {
|
|
|
245
245
|
return result;
|
|
246
246
|
}
|
|
247
247
|
translateFn.rich = translateBaseFn;
|
|
248
|
+
|
|
249
|
+
// Augment `translateBaseFn` to return plain strings
|
|
250
|
+
translateFn.markup = (key, values, formats) => {
|
|
251
|
+
const result = translateBaseFn(key,
|
|
252
|
+
// @ts-expect-error -- `MarkupTranslationValues` is practically a sub type
|
|
253
|
+
// of `RichTranslationValues` but TypeScript isn't smart enough here.
|
|
254
|
+
values, formats);
|
|
255
|
+
|
|
256
|
+
// When only string chunks are provided to the parser, only
|
|
257
|
+
// strings should be returned here. Note that we need a runtime
|
|
258
|
+
// check for this since rich text values could be accidentally
|
|
259
|
+
// inherited from `defaultTranslationValues`.
|
|
260
|
+
if (typeof result !== 'string') {
|
|
261
|
+
const error = new initializeConfig.IntlError(initializeConfig.IntlErrorCode.FORMATTING_ERROR, "`t.markup` only accepts functions for formatting that receive and return strings.\n\nE.g. t.markup('markup', {b: (chunks) => `<b>${chunks}</b>`})" );
|
|
262
|
+
onError(error);
|
|
263
|
+
return getMessageFallback({
|
|
264
|
+
error,
|
|
265
|
+
key,
|
|
266
|
+
namespace
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
return result;
|
|
270
|
+
};
|
|
248
271
|
translateFn.raw = key => {
|
|
249
272
|
if (messagesOrError instanceof initializeConfig.IntlError) {
|
|
250
273
|
// We have already warned about this during render
|
|
@@ -4,7 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var initializeConfig = require('./initializeConfig-0a0b87a7.js');
|
|
6
6
|
var core = require('./core.js');
|
|
7
|
-
var createFormatter = require('./createFormatter-
|
|
7
|
+
var createFormatter = require('./createFormatter-4848f6c8.js');
|
|
8
8
|
var _IntlProvider = require('./_IntlProvider.js');
|
|
9
9
|
var react = require('./react.js');
|
|
10
10
|
var _useLocale = require('./_useLocale-321e619f.js');
|
|
@@ -18,7 +18,6 @@ exports.IntlError = initializeConfig.IntlError;
|
|
|
18
18
|
exports.IntlErrorCode = initializeConfig.IntlErrorCode;
|
|
19
19
|
exports.initializeConfig = initializeConfig.initializeConfig;
|
|
20
20
|
exports.createTranslator = core.createTranslator;
|
|
21
|
-
exports.createBaseTranslator = createFormatter.createBaseTranslator;
|
|
22
21
|
exports.createFormatter = createFormatter.createFormatter;
|
|
23
22
|
exports.IntlProvider = _IntlProvider.IntlProvider;
|
|
24
23
|
exports.useFormatter = react.useFormatter;
|
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var _IntlProvider = require('./_IntlProvider.js');
|
|
6
6
|
var _useLocale = require('./_useLocale-321e619f.js');
|
|
7
7
|
var React = require('react');
|
|
8
|
-
var createFormatter = require('./createFormatter-
|
|
8
|
+
var createFormatter = require('./createFormatter-4848f6c8.js');
|
|
9
9
|
var initializeConfig = require('./initializeConfig-0a0b87a7.js');
|
|
10
10
|
require('./IntlContext-b5cc6be8.js');
|
|
11
11
|
require('intl-messageformat');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("./initializeConfig-29e7ba4c.js"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("./initializeConfig-29e7ba4c.js"),a=require("./IntlContext-381f3ce4.js");function r(e){return e&&e.__esModule?e:{default:e}}var n=r(e);exports.IntlProvider=function(r){let{children:o,defaultTranslationValues:s,formats:l,getMessageFallback:i,locale:u,messages:c,now:f,onError:d,timeZone:m}=r;const[g]=e.useState((()=>new Map)),v=e.useMemo((()=>({...t.initializeConfig({locale:u,defaultTranslationValues:s,formats:l,getMessageFallback:i,messages:c,now:f,onError:d,timeZone:m}),messageFormatCache:g})),[s,l,i,u,g,c,f,d,m]);return n.default.createElement(a.IntlContext.Provider,{value:v},o)};
|
package/dist/esm/core.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./initializeConfig-29e7ba4c.js"),r=require("./createFormatter-
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./initializeConfig-29e7ba4c.js"),r=require("./createFormatter-c871a8ce.js");require("intl-messageformat"),require("react"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.initializeConfig=e.initializeConfig,exports.createFormatter=r.createFormatter,exports.createTranslator=function(a){let{getMessageFallback:t=e.defaultGetMessageFallback,messages:s,namespace:o,onError:n=e.defaultOnError,...l}=a;return function(e,a){let{getMessageFallback:t,messages:s,namespace:o,onError:n,...l}=e;return s=s[a],o=r.resolveNamespace(o,a),r.createBaseTranslator({...l,onError:n,getMessageFallback:t,messages:s,namespace:o})}({...l,onError:n,getMessageFallback:t,messages:{"!":s},namespace:o?"!.".concat(o):"!"},"!")};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var e=require("intl-messageformat"),r=require("react"),t=require("./initializeConfig-29e7ba4c.js");function n(e){return e&&e.__esModule?e:{default:e}}var o=n(e);function a(e,r){return e?Object.keys(e).reduce(((t,n)=>(t[n]={timeZone:r,...e[n]},t)),{}):e}function s(e,r,t){if(!e)throw new Error("No messages available at `".concat(t,"`."));let n=e;return r.split(".").forEach((e=>{const o=n[e];if(null==e||null==o)throw new Error("Could not resolve `".concat(r,"` in ").concat(t?"`".concat(t,"`"):"messages","."));n=o})),n}const i=60,c=60*i,u=24*c,l=7*u,m=u*(365/12),d=3*m,f=365*u,g={second:1,seconds:1,minute:i,minutes:i,hour:c,hours:c,day:u,days:u,week:l,weeks:l,month:m,months:m,quarter:d,quarters:d,year:f,years:f};exports.createBaseTranslator=function(e){const n=function(e){let{messages:r,namespace:n,onError:o=t.defaultOnError}=e;try{if(!r)throw new Error("No messages were configured on the provider.");const e=n?s(r,n):r;if(!e)throw new Error("No messages for namespace `".concat(n,"` found."));return e}catch(e){const r=new t.IntlError(t.IntlErrorCode.MISSING_MESSAGE,e.message);return o(r),r}}({messages:e.messages,namespace:e.namespace,onError:e.onError});return function(e){let{defaultTranslationValues:n,formats:i,getMessageFallback:c=t.defaultGetMessageFallback,locale:u,messageFormatCache:l,messagesOrError:m,namespace:d,onError:f,timeZone:g}=e;function E(e,r,n){const o=new t.IntlError(r,n);return f(o),c({error:o,key:e,namespace:d})}function p(e,f,p){if(m instanceof t.IntlError)return c({error:m,key:e,namespace:d});const I=m;let h;try{h=s(I,e,d)}catch(r){return E(e,t.IntlErrorCode.MISSING_MESSAGE,r.message)}function y(e){return e.filter((e=>null!=e)).join(".")}const w=y([u,d,e,String(h)]);let v;if(null!=l&&l.has(w))v=l.get(w);else{if("object"==typeof h){let r,n;return Array.isArray(h)?(r=t.IntlErrorCode.INVALID_MESSAGE,n="Message at `".concat(y([d,e]),"` resolved to an array, but only strings are supported. See https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages")):(r=t.IntlErrorCode.INSUFFICIENT_PATH,n="Message at `".concat(y([d,e]),"` resolved to an object, but only strings are supported. Use a `.` to retrieve nested messages. See https://next-intl-docs.vercel.app/docs/usage/messages#structuring-messages")),E(e,r,n)}const r=function(e,r){if(r)return;const t=e.replace(/'([{}])/gi,"$1");return/<|{/.test(t)?void 0:t}(h,f);if(r)return r;try{v=new o.default(h,u,function(e,r){const t=r?{...e,dateTime:a(e.dateTime,r)}:e,n=o.default.formats.date,s=r?a(n,r):n,i=o.default.formats.time,c=r?a(i,r):i;return{...t,date:{...s,...null==t?void 0:t.dateTime},time:{...c,...null==t?void 0:t.dateTime}}}({...i,...p},g))}catch(r){return E(e,t.IntlErrorCode.INVALID_MESSAGE,r.message)}null==l||l.set(w,v)}try{const t=v.format(function(e){if(0===Object.keys(e).length)return;const t={};return Object.keys(e).forEach((n=>{let o=0;const a=e[n];let s;s="function"==typeof a?e=>{const t=a(e);return r.isValidElement(t)?r.cloneElement(t,{key:n+o++}):t}:a,t[n]=s})),t}({...n,...f}));if(null==t)throw new Error("Unable to format `".concat(e,"` in ").concat(d?"namespace `".concat(d,"`"):"messages"));return r.isValidElement(t)||Array.isArray(t)||"string"==typeof t?t:String(t)}catch(r){return E(e,t.IntlErrorCode.FORMATTING_ERROR,r.message)}}function I(e,r,n){const o=p(e,r,n);return"string"!=typeof o?E(e,t.IntlErrorCode.INVALID_MESSAGE,"The message `".concat(e,"` in ").concat(d?"namespace `".concat(d,"`"):"messages"," didn't resolve to a string. If you want to format rich text, use `t.rich` instead.")):o}return I.rich=p,I.markup=(e,r,n)=>{const o=p(e,r,n);if("string"!=typeof o){const r=new t.IntlError(t.IntlErrorCode.FORMATTING_ERROR,"`t.markup` only accepts functions for formatting that receive and return strings.\n\nE.g. t.markup('markup', {b: (chunks) => `<b>${chunks}</b>`})");return f(r),c({error:r,key:e,namespace:d})}return o},I.raw=e=>{if(m instanceof t.IntlError)return c({error:m,key:e,namespace:d});const r=m;try{return s(r,e,d)}catch(r){return E(e,t.IntlErrorCode.MISSING_MESSAGE,r.message)}},I}({...e,messagesOrError:n})},exports.createFormatter=function(e){let{formats:r,locale:n,now:o,onError:a=t.defaultOnError,timeZone:s}=e;function d(e,r,n,o){let s;try{s=function(e,r){let n;if("string"==typeof r){const o=r;if(n=null==e?void 0:e[o],!n){const e=new t.IntlError(t.IntlErrorCode.MISSING_FORMAT,"Format `".concat(o,"` is not available. You can configure it on the provider or provide custom options."));throw a(e),e}}else n=r;return n}(n,r)}catch(r){return String(e)}try{return o(s)}catch(r){return a(new t.IntlError(t.IntlErrorCode.FORMATTING_ERROR,r.message)),String(e)}}function E(e){return e instanceof Date||"number"==typeof e?new Date(e):void 0!==(null==e?void 0:e.now)?new Date(e.now):o||(a(new t.IntlError(t.IntlErrorCode.ENVIRONMENT_FALLBACK,"The `now` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now")),new Date)}return{dateTime:function(e,o){return d(e,o,null==r?void 0:r.dateTime,(r=>{var o;return null!==(o=r)&&void 0!==o&&o.timeZone||(s?r={...r,timeZone:s}:a(new t.IntlError(t.IntlErrorCode.ENVIRONMENT_FALLBACK,"The `timeZone` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#time-zone"))),new Intl.DateTimeFormat(n,r).format(e)}))},number:function(e,t){return d(e,t,null==r?void 0:r.number,(r=>new Intl.NumberFormat(n,r).format(e)))},relativeTime:function(e,r){try{const t=new Date(e),o=E(r),a=(t.getTime()-o.getTime())/1e3,s="number"==typeof r||r instanceof Date||void 0===(null==r?void 0:r.unit)?function(e){const r=Math.abs(e);return r<i?"second":r<c?"minute":r<u?"hour":r<l?"day":r<m?"week":r<f?"month":"year"}(a):r.unit,d=function(e,r){return Math.round(e/g[r])}(a,s);return new Intl.RelativeTimeFormat(n,{numeric:"auto"}).format(d,s)}catch(r){return a(new t.IntlError(t.IntlErrorCode.FORMATTING_ERROR,r.message)),String(e)}},list:function(e,t){return d(e,t,null==r?void 0:r.list,(r=>new Intl.ListFormat(n,r).format(e)))}}},exports.resolveNamespace=function(e,r){return e===r?void 0:e.slice((r+".").length)};
|
package/dist/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./initializeConfig-29e7ba4c.js"),r=require("./core.js"),t=require("./createFormatter-
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./initializeConfig-29e7ba4c.js"),r=require("./core.js"),t=require("./createFormatter-c871a8ce.js"),s=require("./_IntlProvider.js"),o=require("./react.js"),i=require("./_useLocale-89b32eb9.js");require("intl-messageformat"),require("react"),require("./IntlContext-381f3ce4.js"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.initializeConfig=e.initializeConfig,exports.createTranslator=r.createTranslator,exports.createFormatter=t.createFormatter,exports.IntlProvider=s.IntlProvider,exports.useFormatter=o.useFormatter,exports.useMessages=o.useMessages,exports.useNow=o.useNow,exports.useTimeZone=o.useTimeZone,exports.useTranslations=o.useTranslations,exports.useLocale=i.useLocale;
|
package/dist/esm/react.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_IntlProvider.js"),t=require("./_useLocale-89b32eb9.js"),r=require("react"),o=require("./createFormatter-
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_IntlProvider.js"),t=require("./_useLocale-89b32eb9.js"),r=require("react"),o=require("./createFormatter-c871a8ce.js"),n=require("./initializeConfig-29e7ba4c.js");require("./IntlContext-381f3ce4.js"),require("intl-messageformat");let s=!1;const a="undefined"==typeof window;function u(){return new Date}exports.IntlProvider=e.IntlProvider,exports.useLocale=t.useLocale,exports.useFormatter=function(){const{formats:e,locale:n,now:s,onError:a,timeZone:u}=t.useIntlContext();return r.useMemo((()=>o.createFormatter({formats:e,locale:n,now:s,onError:a,timeZone:u})),[e,s,n,a,u])},exports.useMessages=function(){const e=t.useIntlContext();if(!e.messages)throw new Error("No messages found. Have you configured them correctly? See https://next-intl-docs.vercel.app/docs/configuration#messages");return e.messages},exports.useNow=function(e){const o=null==e?void 0:e.updateInterval,{now:n}=t.useIntlContext(),[s,a]=r.useState(n||u());return r.useEffect((()=>{if(!o)return;const e=setInterval((()=>{a(u())}),o);return()=>{clearInterval(e)}}),[n,o]),s},exports.useTimeZone=function(){return t.useIntlContext().timeZone},exports.useTranslations=function(e){return function(e,u,i){const{defaultTranslationValues:l,formats:c,getMessageFallback:m,locale:f,messageFormatCache:d,onError:g,timeZone:p}=t.useIntlContext();return e=e[i],u=o.resolveNamespace(u,i),p||s||!a||(s=!0,g(new n.IntlError(n.IntlErrorCode.ENVIRONMENT_FALLBACK,"There is no `timeZone` configured, this can lead to markup mismatches caused by environment differences. Consider adding a global default: https://next-intl-docs.vercel.app/docs/configuration#time-zone"))),r.useMemo((()=>o.createBaseTranslator({messageFormatCache:d,getMessageFallback:m,messages:e,defaultTranslationValues:l,namespace:u,onError:g,formats:c,locale:f,timeZone:p})),[d,m,e,u,g,l,c,f,p])}({"!":t.useIntlContext().messages},e?"!.".concat(e):"!","!")};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("./initializeConfig-984a566d.js"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("./initializeConfig-984a566d.js"),a=require("./IntlContext-381f3ce4.js");function r(e){return e&&e.__esModule?e:{default:e}}var n=r(e);exports.IntlProvider=function(r){let{children:o,defaultTranslationValues:s,formats:l,getMessageFallback:i,locale:u,messages:c,now:f,onError:d,timeZone:m}=r;const[g]=e.useState((()=>new Map)),v=e.useMemo((()=>({...t.initializeConfig({locale:u,defaultTranslationValues:s,formats:l,getMessageFallback:i,messages:c,now:f,onError:d,timeZone:m}),messageFormatCache:g})),[s,l,i,u,g,c,f,d,m]);return n.default.createElement(a.IntlContext.Provider,{value:v},o)};
|
package/dist/production/core.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./createFormatter-
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./createFormatter-7a8c6f36.js"),r=require("./initializeConfig-984a566d.js");require("intl-messageformat"),require("react"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.createFormatter=e.createFormatter,exports.initializeConfig=r.initializeConfig,exports.createTranslator=function(a){let{getMessageFallback:t=r.defaultGetMessageFallback,messages:s,namespace:o,onError:n=r.defaultOnError,...l}=a;return function(r,a){let{getMessageFallback:t,messages:s,namespace:o,onError:n,...l}=r;return s=s[a],o=e.resolveNamespace(o,a),e.createBaseTranslator({...l,onError:n,getMessageFallback:t,messages:s,namespace:o})}({...l,onError:n,getMessageFallback:t,messages:{"!":s},namespace:o?"!.".concat(o):"!"},"!")};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("intl-messageformat"),t=require("react"),r=require("./initializeConfig-984a566d.js");function n(e){return e&&e.__esModule?e:{default:e}}var o=n(e);function i(e,t,r){return(t=function(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}let a=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}({});class s extends Error{constructor(e,t){let r=e;t&&(r+=": "+t),super(r),i(this,"code",void 0),i(this,"originalMessage",void 0),this.code=e,t&&(this.originalMessage=t)}}function u(e,t){return e?Object.keys(e).reduce(((r,n)=>(r[n]={timeZone:t,...e[n]},r)),{}):e}function c(e,t,r){if(!e)throw new Error(void 0);let n=e;return t.split(".").forEach((e=>{const t=n[e];if(null==e||null==t)throw new Error(void 0);n=t})),n}const l=60,f=60*l,m=24*f,E=7*m,d=m*(365/12),I=3*d,S=365*m,g={second:1,seconds:1,minute:l,minutes:l,hour:f,hours:f,day:m,days:m,week:E,weeks:E,month:d,months:d,quarter:I,quarters:I,year:S,years:S};exports.IntlError=s,exports.IntlErrorCode=a,exports.createBaseTranslator=function(e){const n=function(e){let{messages:t,namespace:n,onError:o=r.defaultOnError}=e;try{if(!t)throw new Error(void 0);const e=n?c(t,n):t;if(!e)throw new Error(void 0);return e}catch(e){const t=new s(a.MISSING_MESSAGE,e.message);return o(t),t}}({messages:e.messages,namespace:e.namespace,onError:e.onError});return function(e){let{defaultTranslationValues:n,formats:i,getMessageFallback:l=r.defaultGetMessageFallback,locale:f,messageFormatCache:m,messagesOrError:E,namespace:d,onError:I,timeZone:S}=e;function g(e,t,r){const n=new s(t,r);return I(n),l({error:n,key:e,namespace:d})}function N(e,r,I){if(E instanceof s)return l({error:E,key:e,namespace:d});const N=E;let
|
|
1
|
+
"use strict";var e=require("intl-messageformat"),t=require("react"),r=require("./initializeConfig-984a566d.js");function n(e){return e&&e.__esModule?e:{default:e}}var o=n(e);function i(e,t,r){return(t=function(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}let a=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}({});class s extends Error{constructor(e,t){let r=e;t&&(r+=": "+t),super(r),i(this,"code",void 0),i(this,"originalMessage",void 0),this.code=e,t&&(this.originalMessage=t)}}function u(e,t){return e?Object.keys(e).reduce(((r,n)=>(r[n]={timeZone:t,...e[n]},r)),{}):e}function c(e,t,r){if(!e)throw new Error(void 0);let n=e;return t.split(".").forEach((e=>{const t=n[e];if(null==e||null==t)throw new Error(void 0);n=t})),n}const l=60,f=60*l,m=24*f,E=7*m,d=m*(365/12),I=3*d,S=365*m,g={second:1,seconds:1,minute:l,minutes:l,hour:f,hours:f,day:m,days:m,week:E,weeks:E,month:d,months:d,quarter:I,quarters:I,year:S,years:S};exports.IntlError=s,exports.IntlErrorCode=a,exports.createBaseTranslator=function(e){const n=function(e){let{messages:t,namespace:n,onError:o=r.defaultOnError}=e;try{if(!t)throw new Error(void 0);const e=n?c(t,n):t;if(!e)throw new Error(void 0);return e}catch(e){const t=new s(a.MISSING_MESSAGE,e.message);return o(t),t}}({messages:e.messages,namespace:e.namespace,onError:e.onError});return function(e){let{defaultTranslationValues:n,formats:i,getMessageFallback:l=r.defaultGetMessageFallback,locale:f,messageFormatCache:m,messagesOrError:E,namespace:d,onError:I,timeZone:S}=e;function g(e,t,r){const n=new s(t,r);return I(n),l({error:n,key:e,namespace:d})}function N(e,r,I){if(E instanceof s)return l({error:E,key:e,namespace:d});const N=E;let y;try{y=c(N,e)}catch(t){return g(e,a.MISSING_MESSAGE,t.message)}function A(e){return e.filter((e=>null!=e)).join(".")}const v=A([f,d,e,String(y)]);let T;if(null!=m&&m.has(v))T=m.get(v);else{if("object"==typeof y){let t,r;return t=Array.isArray(y)?a.INVALID_MESSAGE:a.INSUFFICIENT_PATH,g(e,t,r)}const t=function(e,t){if(t)return;const r=e.replace(/'([{}])/gi,"$1");return/<|{/.test(r)?void 0:r}(y,r);if(t)return t;try{T=new o.default(y,f,function(e,t){const r=t?{...e,dateTime:u(e.dateTime,t)}:e,n=o.default.formats.date,i=t?u(n,t):n,a=o.default.formats.time,s=t?u(a,t):a;return{...r,date:{...i,...null==r?void 0:r.dateTime},time:{...s,...null==r?void 0:r.dateTime}}}({...i,...I},S))}catch(t){return g(e,a.INVALID_MESSAGE,t.message)}null==m||m.set(v,T)}try{const e=T.format(function(e){if(0===Object.keys(e).length)return;const r={};return Object.keys(e).forEach((n=>{let o=0;const i=e[n];let a;a="function"==typeof i?e=>{const r=i(e);return t.isValidElement(r)?t.cloneElement(r,{key:n+o++}):r}:i,r[n]=a})),r}({...n,...r}));if(null==e)throw new Error(void 0);return t.isValidElement(e)||Array.isArray(e)||"string"==typeof e?e:String(e)}catch(t){return g(e,a.FORMATTING_ERROR,t.message)}}function y(e,t,r){const n=N(e,t,r);return"string"!=typeof n?g(e,a.INVALID_MESSAGE,void 0):n}return y.rich=N,y.markup=(e,t,r)=>{const n=N(e,t,r);if("string"!=typeof n){const t=new s(a.FORMATTING_ERROR,void 0);return I(t),l({error:t,key:e,namespace:d})}return n},y.raw=e=>{if(E instanceof s)return l({error:E,key:e,namespace:d});const t=E;try{return c(t,e)}catch(t){return g(e,a.MISSING_MESSAGE,t.message)}},y}({...e,messagesOrError:n})},exports.createFormatter=function(e){let{formats:t,locale:n,now:o,onError:i=r.defaultOnError,timeZone:u}=e;function c(e,t,r,n){let o;try{o=function(e,t){let r;if("string"==typeof t){if(r=null==e?void 0:e[t],!r){const e=new s(a.MISSING_FORMAT,void 0);throw i(e),e}}else r=t;return r}(r,t)}catch(t){return String(e)}try{return n(o)}catch(t){return i(new s(a.FORMATTING_ERROR,t.message)),String(e)}}function I(e){return e instanceof Date||"number"==typeof e?new Date(e):void 0!==(null==e?void 0:e.now)?new Date(e.now):o||(i(new s(a.ENVIRONMENT_FALLBACK,void 0)),new Date)}return{dateTime:function(e,r){return c(e,r,null==t?void 0:t.dateTime,(t=>{var r;return null!==(r=t)&&void 0!==r&&r.timeZone||(u?t={...t,timeZone:u}:i(new s(a.ENVIRONMENT_FALLBACK,void 0))),new Intl.DateTimeFormat(n,t).format(e)}))},number:function(e,r){return c(e,r,null==t?void 0:t.number,(t=>new Intl.NumberFormat(n,t).format(e)))},relativeTime:function(e,t){try{const r=new Date(e),o=I(t),i=(r.getTime()-o.getTime())/1e3,a="number"==typeof t||t instanceof Date||void 0===(null==t?void 0:t.unit)?function(e){const t=Math.abs(e);return t<l?"second":t<f?"minute":t<m?"hour":t<E?"day":t<d?"week":t<S?"month":"year"}(i):t.unit,s=function(e,t){return Math.round(e/g[t])}(i,a);return new Intl.RelativeTimeFormat(n,{numeric:"auto"}).format(s,a)}catch(t){return i(new s(a.FORMATTING_ERROR,t.message)),String(e)}},list:function(e,r){return c(e,r,null==t?void 0:t.list,(t=>new Intl.ListFormat(n,t).format(e)))}}},exports.resolveNamespace=function(e,t){return e===t?void 0:e.slice((t+".").length)};
|
package/dist/production/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./createFormatter-
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./createFormatter-7a8c6f36.js"),r=require("./core.js"),t=require("./initializeConfig-984a566d.js"),s=require("./_IntlProvider.js"),o=require("./react.js"),i=require("./_useLocale-8e23751a.js");require("intl-messageformat"),require("react"),require("./IntlContext-381f3ce4.js"),exports.IntlError=e.IntlError,exports.IntlErrorCode=e.IntlErrorCode,exports.createFormatter=e.createFormatter,exports.createTranslator=r.createTranslator,exports.initializeConfig=t.initializeConfig,exports.IntlProvider=s.IntlProvider,exports.useFormatter=o.useFormatter,exports.useMessages=o.useMessages,exports.useNow=o.useNow,exports.useTimeZone=o.useTimeZone,exports.useTranslations=o.useTranslations,exports.useLocale=i.useLocale;
|
package/dist/production/react.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_IntlProvider.js"),t=require("./_useLocale-8e23751a.js"),r=require("react"),o=require("./createFormatter-
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_IntlProvider.js"),t=require("./_useLocale-8e23751a.js"),r=require("react"),o=require("./createFormatter-7a8c6f36.js");require("./initializeConfig-984a566d.js"),require("./IntlContext-381f3ce4.js"),require("intl-messageformat");let n=!1;const s="undefined"==typeof window;function a(){return new Date}exports.IntlProvider=e.IntlProvider,exports.useLocale=t.useLocale,exports.useFormatter=function(){const{formats:e,locale:n,now:s,onError:a,timeZone:u}=t.useIntlContext();return r.useMemo((()=>o.createFormatter({formats:e,locale:n,now:s,onError:a,timeZone:u})),[e,s,n,a,u])},exports.useMessages=function(){const e=t.useIntlContext();if(!e.messages)throw new Error(void 0);return e.messages},exports.useNow=function(e){const o=null==e?void 0:e.updateInterval,{now:n}=t.useIntlContext(),[s,u]=r.useState(n||a());return r.useEffect((()=>{if(!o)return;const e=setInterval((()=>{u(a())}),o);return()=>{clearInterval(e)}}),[n,o]),s},exports.useTimeZone=function(){return t.useIntlContext().timeZone},exports.useTranslations=function(e){return function(e,a,u){const{defaultTranslationValues:l,formats:i,getMessageFallback:c,locale:m,messageFormatCache:f,onError:I,timeZone:d}=t.useIntlContext();return e=e[u],a=o.resolveNamespace(a,u),d||n||!s||(n=!0,I(new o.IntlError(o.IntlErrorCode.ENVIRONMENT_FALLBACK,void 0))),r.useMemo((()=>o.createBaseTranslator({messageFormatCache:f,getMessageFallback:c,messages:e,defaultTranslationValues:l,namespace:a,onError:I,formats:i,locale:m,timeZone:d})),[f,c,e,a,I,l,i,m,d])}({"!":t.useIntlContext().messages},e?"!.".concat(e):"!","!")};
|
|
@@ -2,5 +2,5 @@ import { ReactNode } from 'react';
|
|
|
2
2
|
export type TranslationValue = string | number | boolean | Date | null | undefined;
|
|
3
3
|
type TranslationValues = Record<string, TranslationValue>;
|
|
4
4
|
export type RichTranslationValues = Record<string, TranslationValue | ((chunks: ReactNode) => ReactNode)>;
|
|
5
|
-
export type
|
|
5
|
+
export type MarkupTranslationValues = Record<string, TranslationValue | ((chunks: string) => string)>;
|
|
6
6
|
export default TranslationValues;
|
|
@@ -4,7 +4,7 @@ import Formats from './Formats';
|
|
|
4
4
|
import { InitializedIntlConfig } from './IntlConfig';
|
|
5
5
|
import IntlError from './IntlError';
|
|
6
6
|
import MessageFormatCache from './MessageFormatCache';
|
|
7
|
-
import TranslationValues, { RichTranslationValues } from './TranslationValues';
|
|
7
|
+
import TranslationValues, { MarkupTranslationValues, RichTranslationValues } from './TranslationValues';
|
|
8
8
|
import MessageKeys from './utils/MessageKeys';
|
|
9
9
|
import NestedKeyOf from './utils/NestedKeyOf';
|
|
10
10
|
import NestedValueOf from './utils/NestedValueOf';
|
|
@@ -17,5 +17,6 @@ export type CreateBaseTranslatorProps<Messages> = InitializedIntlConfig & {
|
|
|
17
17
|
export default function createBaseTranslator<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>(config: Omit<CreateBaseTranslatorProps<Messages>, 'messagesOrError'>): {
|
|
18
18
|
<TargetKey extends MessageKeys<NestedValueOf<Messages, NestedKey>, NestedKeyOf<NestedValueOf<Messages, NestedKey>>>>(key: TargetKey, values?: TranslationValues | undefined, formats?: Partial<Formats> | undefined): string;
|
|
19
19
|
rich: (key: string, values?: RichTranslationValues | undefined, formats?: Partial<Formats> | undefined) => string | ReactElement<any, string | import("react").JSXElementConstructor<any>> | ReactNodeArray;
|
|
20
|
+
markup(key: string, values: MarkupTranslationValues, formats?: Partial<Formats> | undefined): string;
|
|
20
21
|
raw(key: string): any;
|
|
21
22
|
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { ReactElement, ReactNodeArray } from 'react';
|
|
1
2
|
import Formats from './Formats';
|
|
2
3
|
import IntlConfig from './IntlConfig';
|
|
3
|
-
import
|
|
4
|
+
import MessageFormatCache from './MessageFormatCache';
|
|
5
|
+
import TranslationValues, { MarkupTranslationValues, RichTranslationValues } from './TranslationValues';
|
|
4
6
|
import MessageKeys from './utils/MessageKeys';
|
|
5
7
|
import NamespaceKeys from './utils/NamespaceKeys';
|
|
6
8
|
import NestedKeyOf from './utils/NestedKeyOf';
|
|
@@ -14,8 +16,10 @@ import NestedValueOf from './utils/NestedValueOf';
|
|
|
14
16
|
* (e.g. `namespace.Component`).
|
|
15
17
|
*/
|
|
16
18
|
export default function createTranslator<NestedKey extends NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>> = never>({ getMessageFallback, messages, namespace, onError, ...rest }: Omit<IntlConfig<IntlMessages>, 'defaultTranslationValues' | 'messages'> & {
|
|
17
|
-
messages:
|
|
19
|
+
messages: IntlConfig<IntlMessages>['messages'];
|
|
18
20
|
namespace?: NestedKey;
|
|
21
|
+
/** @private */
|
|
22
|
+
messageFormatCache?: MessageFormatCache;
|
|
19
23
|
}): {
|
|
20
24
|
<TargetKey extends MessageKeys<NestedValueOf<{
|
|
21
25
|
'!': IntlMessages;
|
|
@@ -34,7 +38,16 @@ export default function createTranslator<NestedKey extends NamespaceKeys<IntlMes
|
|
|
34
38
|
'!': IntlMessages;
|
|
35
39
|
}, [
|
|
36
40
|
NestedKey
|
|
37
|
-
] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?:
|
|
41
|
+
] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?: RichTranslationValues, formats?: Partial<Formats>): string | ReactElement | ReactNodeArray;
|
|
42
|
+
markup<TargetKey extends MessageKeys<NestedValueOf<{
|
|
43
|
+
'!': IntlMessages;
|
|
44
|
+
}, [
|
|
45
|
+
NestedKey
|
|
46
|
+
] extends [never] ? '!' : `!.${NestedKey}`>, NestedKeyOf<NestedValueOf<{
|
|
47
|
+
'!': IntlMessages;
|
|
48
|
+
}, [
|
|
49
|
+
NestedKey
|
|
50
|
+
] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?: MarkupTranslationValues, formats?: Partial<Formats>): string;
|
|
38
51
|
raw<TargetKey extends MessageKeys<NestedValueOf<{
|
|
39
52
|
'!': IntlMessages;
|
|
40
53
|
}, [
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
1
2
|
import AbstractIntlMessages from './AbstractIntlMessages';
|
|
2
3
|
import { InitializedIntlConfig } from './IntlConfig';
|
|
3
|
-
import
|
|
4
|
+
import MessageFormatCache from './MessageFormatCache';
|
|
4
5
|
import NestedKeyOf from './utils/NestedKeyOf';
|
|
5
6
|
export type CreateTranslatorImplProps<Messages> = Omit<InitializedIntlConfig, 'messages'> & {
|
|
6
7
|
namespace: string;
|
|
7
8
|
messages: Messages;
|
|
9
|
+
messageFormatCache?: MessageFormatCache;
|
|
8
10
|
};
|
|
9
11
|
export default function createTranslatorImpl<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>({ getMessageFallback, messages, namespace, onError, ...rest }: CreateTranslatorImplProps<Messages>, namespacePrefix: string): {
|
|
10
|
-
|
|
11
|
-
rich(key: string, values
|
|
12
|
-
|
|
12
|
+
<TargetKey extends import("./utils/MessageKeys").default<import("./utils/NestedValueOf").default<Messages, NestedKey>, NestedKeyOf<import("./utils/NestedValueOf").default<Messages, NestedKey>>>>(key: TargetKey, values?: import("./TranslationValues").default | undefined, formats?: Partial<import("./Formats").default> | undefined): string;
|
|
13
|
+
rich: (key: string, values?: import("./TranslationValues").RichTranslationValues | undefined, formats?: Partial<import("./Formats").default> | undefined) => string | import("react").ReactElement<any, string | import("react").JSXElementConstructor<any>> | import("react").ReactNodeArray;
|
|
14
|
+
markup(key: string, values: import("./TranslationValues").MarkupTranslationValues, formats?: Partial<import("./Formats").default> | undefined): string;
|
|
15
|
+
raw(key: string): any;
|
|
13
16
|
};
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
export type { default as AbstractIntlMessages } from './AbstractIntlMessages';
|
|
2
|
-
export type { default as TranslationValues, RichTranslationValues,
|
|
2
|
+
export type { default as TranslationValues, RichTranslationValues, MarkupTranslationValues } from './TranslationValues';
|
|
3
3
|
export type { default as Formats } from './Formats';
|
|
4
4
|
export type { default as IntlConfig } from './IntlConfig';
|
|
5
5
|
export type { default as DateTimeFormatOptions } from './DateTimeFormatOptions';
|
|
6
6
|
export type { default as NumberFormatOptions } from './NumberFormatOptions';
|
|
7
7
|
export { default as IntlError, IntlErrorCode } from './IntlError';
|
|
8
8
|
export { default as createTranslator } from './createTranslator';
|
|
9
|
-
export { default as createBaseTranslator } from './createBaseTranslator';
|
|
10
9
|
export { default as createFormatter } from './createFormatter';
|
|
11
10
|
export { default as initializeConfig } from './initializeConfig';
|
|
12
11
|
export type { default as MessageKeys } from './utils/MessageKeys';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { ReactNode } from 'react';
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
2
|
import IntlConfig from '../core/IntlConfig';
|
|
3
3
|
type Props = IntlConfig & {
|
|
4
4
|
children: ReactNode;
|
|
5
5
|
};
|
|
6
|
-
export default function IntlProvider({ children,
|
|
6
|
+
export default function IntlProvider({ children, defaultTranslationValues, formats, getMessageFallback, locale, messages, now, onError, timeZone }: Props): React.JSX.Element;
|
|
7
7
|
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ReactElement, ReactNodeArray } from 'react';
|
|
2
2
|
import Formats from '../core/Formats';
|
|
3
|
-
import TranslationValues, { RichTranslationValues } from '../core/TranslationValues';
|
|
3
|
+
import TranslationValues, { MarkupTranslationValues, RichTranslationValues } from '../core/TranslationValues';
|
|
4
4
|
import MessageKeys from '../core/utils/MessageKeys';
|
|
5
5
|
import NamespaceKeys from '../core/utils/NamespaceKeys';
|
|
6
6
|
import NestedKeyOf from '../core/utils/NestedKeyOf';
|
|
@@ -32,6 +32,15 @@ export default function useTranslations<NestedKey extends NamespaceKeys<IntlMess
|
|
|
32
32
|
}, [
|
|
33
33
|
NestedKey
|
|
34
34
|
] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?: RichTranslationValues, formats?: Partial<Formats>): string | ReactElement | ReactNodeArray;
|
|
35
|
+
markup<TargetKey extends MessageKeys<NestedValueOf<{
|
|
36
|
+
'!': IntlMessages;
|
|
37
|
+
}, [
|
|
38
|
+
NestedKey
|
|
39
|
+
] extends [never] ? '!' : `!.${NestedKey}`>, NestedKeyOf<NestedValueOf<{
|
|
40
|
+
'!': IntlMessages;
|
|
41
|
+
}, [
|
|
42
|
+
NestedKey
|
|
43
|
+
] extends [never] ? '!' : `!.${NestedKey}`>>>>(key: TargetKey, values?: MarkupTranslationValues, formats?: Partial<Formats>): string;
|
|
35
44
|
raw<TargetKey extends MessageKeys<NestedValueOf<{
|
|
36
45
|
'!': IntlMessages;
|
|
37
46
|
}, [
|
|
@@ -4,5 +4,6 @@ import NestedKeyOf from '../core/utils/NestedKeyOf';
|
|
|
4
4
|
export default function useTranslationsImpl<Messages extends AbstractIntlMessages, NestedKey extends NestedKeyOf<Messages>>(allMessages: Messages, namespace: NestedKey, namespacePrefix: string): {
|
|
5
5
|
<TargetKey extends unknown>(key: TargetKey, values?: import("../core/TranslationValues").default | undefined, formats?: Partial<import("../core/Formats").default> | undefined): string;
|
|
6
6
|
rich: (key: string, values?: import("../core").RichTranslationValues | undefined, formats?: Partial<import("../core/Formats").default> | undefined) => string | import("react").ReactElement<any, string | import("react").JSXElementConstructor<any>> | import("react").ReactNodeArray;
|
|
7
|
+
markup(key: string, values: import("../core").MarkupTranslationValues, formats?: Partial<import("../core/Formats").default> | undefined): string;
|
|
7
8
|
raw(key: string): any;
|
|
8
9
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "use-intl",
|
|
3
|
-
"version": "3.0.0
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"author": "Jan Amann <jan@amann.work>",
|
|
6
6
|
"description": "Minimal, but complete solution for managing internationalization in React apps.",
|
|
@@ -64,7 +64,8 @@
|
|
|
64
64
|
"@size-limit/preset-big-lib": "^8.2.6",
|
|
65
65
|
"@testing-library/react": "^13.0.0",
|
|
66
66
|
"@types/node": "^17.0.23",
|
|
67
|
-
"@types/react": "^18.2.
|
|
67
|
+
"@types/react": "^18.2.29",
|
|
68
|
+
"@types/react-dom": "^18.2.5",
|
|
68
69
|
"date-fns": "^2.16.1",
|
|
69
70
|
"eslint": "^8.46.0",
|
|
70
71
|
"eslint-config-molindo": "^7.0.0",
|
|
@@ -72,13 +73,13 @@
|
|
|
72
73
|
"react-dom": "^18.2.0",
|
|
73
74
|
"rollup": "^3.28.1",
|
|
74
75
|
"size-limit": "^8.2.6",
|
|
75
|
-
"typescript": "^5.
|
|
76
|
+
"typescript": "^5.2.2",
|
|
76
77
|
"vitest": "^0.32.2"
|
|
77
78
|
},
|
|
78
79
|
"size-limit": [
|
|
79
80
|
{
|
|
80
81
|
"path": "dist/production/index.js",
|
|
81
|
-
"limit": "12.
|
|
82
|
+
"limit": "12.355 kB"
|
|
82
83
|
}
|
|
83
84
|
],
|
|
84
85
|
"scripts": {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var e=require("intl-messageformat"),r=require("react"),t=require("./initializeConfig-29e7ba4c.js");function n(e){return e&&e.__esModule?e:{default:e}}var o=n(e);function a(e,r){return e?Object.keys(e).reduce(((t,n)=>(t[n]={timeZone:r,...e[n]},t)),{}):e}function s(e,r,t){if(!e)throw new Error("No messages available at `".concat(t,"`."));let n=e;return r.split(".").forEach((e=>{const o=n[e];if(null==e||null==o)throw new Error("Could not resolve `".concat(r,"` in ").concat(t?"`".concat(t,"`"):"messages","."));n=o})),n}const i=60,c=60*i,u=24*c,l=7*u,m=u*(365/12),d=3*m,f=365*u,g={second:1,seconds:1,minute:i,minutes:i,hour:c,hours:c,day:u,days:u,week:l,weeks:l,month:m,months:m,quarter:d,quarters:d,year:f,years:f};exports.createBaseTranslator=function(e){const n=function(e){let{messages:r,namespace:n,onError:o=t.defaultOnError}=e;try{if(!r)throw new Error("No messages were configured on the provider.");const e=n?s(r,n):r;if(!e)throw new Error("No messages for namespace `".concat(n,"` found."));return e}catch(e){const r=new t.IntlError(t.IntlErrorCode.MISSING_MESSAGE,e.message);return o(r),r}}({messages:e.messages,namespace:e.namespace,onError:e.onError});return function(e){let{defaultTranslationValues:n,formats:i,getMessageFallback:c=t.defaultGetMessageFallback,locale:u,messageFormatCache:l,messagesOrError:m,namespace:d,onError:f,timeZone:g}=e;function E(e,r,n){const o=new t.IntlError(r,n);return f(o),c({error:o,key:e,namespace:d})}function p(e,f,p){if(m instanceof t.IntlError)return c({error:m,key:e,namespace:d});const I=m;let h;try{h=s(I,e,d)}catch(r){return E(e,t.IntlErrorCode.MISSING_MESSAGE,r.message)}function w(e){return e.filter((e=>null!=e)).join(".")}const v=w([u,d,e,String(h)]);let y;if(null!=l&&l.has(v))y=l.get(v);else{if("object"==typeof h){let r,n;return Array.isArray(h)?(r=t.IntlErrorCode.INVALID_MESSAGE,n="Message at `".concat(w([d,e]),"` resolved to an array, but only strings are supported. See https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages")):(r=t.IntlErrorCode.INSUFFICIENT_PATH,n="Message at `".concat(w([d,e]),"` resolved to an object, but only strings are supported. Use a `.` to retrieve nested messages. See https://next-intl-docs.vercel.app/docs/usage/messages#structuring-messages")),E(e,r,n)}const r=function(e,r){if(r)return;const t=e.replace(/'([{}])/gi,"$1");return/<|{/.test(t)?void 0:t}(h,f);if(r)return r;try{y=new o.default(h,u,function(e,r){const t=r?{...e,dateTime:a(e.dateTime,r)}:e,n=o.default.formats.date,s=r?a(n,r):n,i=o.default.formats.time,c=r?a(i,r):i;return{...t,date:{...s,...null==t?void 0:t.dateTime},time:{...c,...null==t?void 0:t.dateTime}}}({...i,...p},g))}catch(r){return E(e,t.IntlErrorCode.INVALID_MESSAGE,r.message)}null==l||l.set(v,y)}try{const t=y.format(function(e){if(0===Object.keys(e).length)return;const t={};return Object.keys(e).forEach((n=>{let o=0;const a=e[n];let s;s="function"==typeof a?e=>{const t=a(e);return r.isValidElement(t)?r.cloneElement(t,{key:n+o++}):t}:a,t[n]=s})),t}({...n,...f}));if(null==t)throw new Error("Unable to format `".concat(e,"` in ").concat(d?"namespace `".concat(d,"`"):"messages"));return r.isValidElement(t)||Array.isArray(t)||"string"==typeof t?t:String(t)}catch(r){return E(e,t.IntlErrorCode.FORMATTING_ERROR,r.message)}}function I(e,r,n){const o=p(e,r,n);return"string"!=typeof o?E(e,t.IntlErrorCode.INVALID_MESSAGE,"The message `".concat(e,"` in ").concat(d?"namespace `".concat(d,"`"):"messages"," didn't resolve to a string. If you want to format rich text, use `t.rich` instead.")):o}return I.rich=p,I.raw=e=>{if(m instanceof t.IntlError)return c({error:m,key:e,namespace:d});const r=m;try{return s(r,e,d)}catch(r){return E(e,t.IntlErrorCode.MISSING_MESSAGE,r.message)}},I}({...e,messagesOrError:n})},exports.createFormatter=function(e){let{formats:r,locale:n,now:o,onError:a=t.defaultOnError,timeZone:s}=e;function d(e,r,n,o){let s;try{s=function(e,r){let n;if("string"==typeof r){const o=r;if(n=null==e?void 0:e[o],!n){const e=new t.IntlError(t.IntlErrorCode.MISSING_FORMAT,"Format `".concat(o,"` is not available. You can configure it on the provider or provide custom options."));throw a(e),e}}else n=r;return n}(n,r)}catch(r){return String(e)}try{return o(s)}catch(r){return a(new t.IntlError(t.IntlErrorCode.FORMATTING_ERROR,r.message)),String(e)}}function E(e){return e instanceof Date||"number"==typeof e?new Date(e):void 0!==(null==e?void 0:e.now)?new Date(e.now):o||(a(new t.IntlError(t.IntlErrorCode.ENVIRONMENT_FALLBACK,"The `now` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now")),new Date)}return{dateTime:function(e,o){return d(e,o,null==r?void 0:r.dateTime,(r=>{var o;return null!==(o=r)&&void 0!==o&&o.timeZone||(s?r={...r,timeZone:s}:a(new t.IntlError(t.IntlErrorCode.ENVIRONMENT_FALLBACK,"The `timeZone` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#time-zone"))),new Intl.DateTimeFormat(n,r).format(e)}))},number:function(e,t){return d(e,t,null==r?void 0:r.number,(r=>new Intl.NumberFormat(n,r).format(e)))},relativeTime:function(e,r){try{const t=new Date(e),o=E(r),a=(t.getTime()-o.getTime())/1e3,s="number"==typeof r||r instanceof Date||void 0===(null==r?void 0:r.unit)?function(e){const r=Math.abs(e);return r<i?"second":r<c?"minute":r<u?"hour":r<l?"day":r<m?"week":r<f?"month":"year"}(a):r.unit,d=function(e,r){return Math.round(e/g[r])}(a,s);return new Intl.RelativeTimeFormat(n,{numeric:"auto"}).format(d,s)}catch(r){return a(new t.IntlError(t.IntlErrorCode.FORMATTING_ERROR,r.message)),String(e)}},list:function(e,t){return d(e,t,null==r?void 0:r.list,(r=>new Intl.ListFormat(n,r).format(e)))}}},exports.resolveNamespace=function(e,r){return e===r?void 0:e.slice((r+".").length)};
|