use-intl 0.0.0-canary-5bbd7ad56b530e283b07cff950de61d6af81f00c
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/LICENSE +21 -0
- package/README.md +83 -0
- package/_IntlProvider.d.ts +1 -0
- package/_useLocale.d.ts +1 -0
- package/core.d.ts +1 -0
- package/dist/_IntlProvider.js +7 -0
- package/dist/_useLocale.js +7 -0
- package/dist/core.js +7 -0
- package/dist/development/IntlContext-BKfsnzBx.js +7 -0
- package/dist/development/_IntlProvider.js +61 -0
- package/dist/development/_useLocale-0Rl9uR82.js +19 -0
- package/dist/development/_useLocale.js +11 -0
- package/dist/development/core.js +69 -0
- package/dist/development/createFormatter-TZTkYRKI.js +542 -0
- package/dist/development/index.js +31 -0
- package/dist/development/initializeConfig-o7L_y07P.js +185 -0
- package/dist/development/react.js +151 -0
- package/dist/esm/IntlContext-DoS4CDM3.js +1 -0
- package/dist/esm/_IntlProvider.js +1 -0
- package/dist/esm/_useLocale-v-ZT5JoE.js +1 -0
- package/dist/esm/_useLocale.js +1 -0
- package/dist/esm/core.js +1 -0
- package/dist/esm/createFormatter-D1WNHqdE.js +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/initializeConfig-D2A8plWf.js +1 -0
- package/dist/esm/react.js +1 -0
- package/dist/index.js +7 -0
- package/dist/production/IntlContext-DcFt0tgW.js +1 -0
- package/dist/production/_IntlProvider.js +1 -0
- package/dist/production/_useLocale-CpTrqBDt.js +1 -0
- package/dist/production/_useLocale.js +1 -0
- package/dist/production/core.js +1 -0
- package/dist/production/createFormatter-CTr0qKAI.js +1 -0
- package/dist/production/index.js +1 -0
- package/dist/production/initializeConfig-AbYTngyP.js +1 -0
- package/dist/production/react.js +1 -0
- package/dist/react.js +7 -0
- package/dist/types/src/_IntlProvider.d.ts +1 -0
- package/dist/types/src/_useLocale.d.ts +1 -0
- package/dist/types/src/core/AbstractIntlMessages.d.ts +9 -0
- package/dist/types/src/core/DateTimeFormatOptions.d.ts +73 -0
- package/dist/types/src/core/Formats.d.ts +8 -0
- package/dist/types/src/core/IntlConfig.d.ts +58 -0
- package/dist/types/src/core/IntlError.d.ts +14 -0
- package/dist/types/src/core/NumberFormatOptions.d.ts +3 -0
- package/dist/types/src/core/RelativeTimeFormatOptions.d.ts +7 -0
- package/dist/types/src/core/TimeZone.d.ts +2 -0
- package/dist/types/src/core/TranslationValues.d.ts +6 -0
- package/dist/types/src/core/convertFormatsToIntlMessageFormat.d.ts +11 -0
- package/dist/types/src/core/createBaseTranslator.d.ts +24 -0
- package/dist/types/src/core/createFormatter.d.ts +27 -0
- package/dist/types/src/core/createFormatter.test.d.ts +1 -0
- package/dist/types/src/core/createTranslator.d.ts +71 -0
- package/dist/types/src/core/createTranslator.test.d.ts +1 -0
- package/dist/types/src/core/createTranslatorImpl.d.ts +17 -0
- package/dist/types/src/core/defaults.d.ts +11 -0
- package/dist/types/src/core/formatters.d.ts +25 -0
- package/dist/types/src/core/index.d.ts +16 -0
- package/dist/types/src/core/initializeConfig.d.ts +13 -0
- package/dist/types/src/core/joinPath.d.ts +1 -0
- package/dist/types/src/core/resolveNamespace.d.ts +5 -0
- package/dist/types/src/core/utils/MessageKeys.d.ts +5 -0
- package/dist/types/src/core/utils/NamespaceKeys.d.ts +5 -0
- package/dist/types/src/core/utils/NestedKeyOf.d.ts +4 -0
- package/dist/types/src/core/utils/NestedValueOf.d.ts +2 -0
- package/dist/types/src/core/validateMessages.d.ts +3 -0
- package/dist/types/src/core.d.ts +1 -0
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/react/IntlContext.d.ts +8 -0
- package/dist/types/src/react/IntlProvider.d.ts +7 -0
- package/dist/types/src/react/IntlProvider.test.d.ts +1 -0
- package/dist/types/src/react/index.d.ts +7 -0
- package/dist/types/src/react/index.test.d.ts +1 -0
- package/dist/types/src/react/useFormatter.d.ts +2 -0
- package/dist/types/src/react/useFormatter.test.d.ts +1 -0
- package/dist/types/src/react/useIntlContext.d.ts +2 -0
- package/dist/types/src/react/useLocale.d.ts +1 -0
- package/dist/types/src/react/useLocale.test.d.ts +1 -0
- package/dist/types/src/react/useMessages.d.ts +2 -0
- package/dist/types/src/react/useMessages.test.d.ts +1 -0
- package/dist/types/src/react/useNow.d.ts +23 -0
- package/dist/types/src/react/useNow.test.d.ts +1 -0
- package/dist/types/src/react/useTimeZone.d.ts +1 -0
- package/dist/types/src/react/useTimeZone.test.d.ts +1 -0
- package/dist/types/src/react/useTranslations.d.ts +62 -0
- package/dist/types/src/react/useTranslations.test.d.ts +1 -0
- package/dist/types/src/react/useTranslationsImpl.d.ts +9 -0
- package/dist/types/src/react.d.ts +1 -0
- package/dist/types/test/setup.d.ts +1 -0
- package/package.json +73 -0
- package/react.d.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Jan Amann
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# 🌐 use-intl
|
|
2
|
+
|
|
3
|
+
> Internationalization (i18n) for React
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
Internationalization (i18n) is an essential part of the user experience, therefore `use-intl` gives you all the parts you need to get language nuances right.
|
|
8
|
+
|
|
9
|
+
- 🌟 **ICU message syntax**: Localize your messages with interpolation, cardinal & ordinal plurals, enum-based label selection and rich text.
|
|
10
|
+
- 📅 **Dates, times & numbers**: Apply appropriate formatting without worrying about server/client differences like time zones.
|
|
11
|
+
- ✅ **Type-safe**: Speed up development with autocompletion for message keys and catch typos early with compile-time checks.
|
|
12
|
+
- 💡 **Hooks-based API**: Learn a single API that can be used across your code base to turn translations into plain strings or rich text.
|
|
13
|
+
- ⚔️ **Standards-based**: Use the best parts of built-in JavaScript APIs and supplemental lower-level APIs from Format.JS.
|
|
14
|
+
|
|
15
|
+
## What does it look like?
|
|
16
|
+
|
|
17
|
+
```jsx
|
|
18
|
+
// UserProfile.tsx
|
|
19
|
+
import {useTranslations} from 'use-intl';
|
|
20
|
+
|
|
21
|
+
export default function UserProfile({user}) {
|
|
22
|
+
const t = useTranslations('UserProfile');
|
|
23
|
+
|
|
24
|
+
return (
|
|
25
|
+
<section>
|
|
26
|
+
<h1>{t('title', {firstName: user.firstName})}</h1>
|
|
27
|
+
<p>{t('membership', {memberSince: user.memberSince})}</p>
|
|
28
|
+
<p>{t('followers', {count: user.numFollowers})}</p>
|
|
29
|
+
</section>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
// en.json
|
|
36
|
+
{
|
|
37
|
+
"UserProfile": {
|
|
38
|
+
"title": "{username}'s profile",
|
|
39
|
+
"membership": "Member since {memberSince, date, short}",
|
|
40
|
+
"followers": "{count, plural, ↵
|
|
41
|
+
=0 {No followers yet} ↵
|
|
42
|
+
=1 {One follower} ↵
|
|
43
|
+
other {# followers} ↵
|
|
44
|
+
}"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
|
|
51
|
+
1. `npm install use-intl`
|
|
52
|
+
2. Add the provider
|
|
53
|
+
3. Use internationalization in components
|
|
54
|
+
|
|
55
|
+
```jsx
|
|
56
|
+
import {IntlProvider, useTranslations} from 'use-intl';
|
|
57
|
+
|
|
58
|
+
// You can get the messages from anywhere you like. You can also
|
|
59
|
+
// fetch them from within a component and then render the provider
|
|
60
|
+
// along with your app once you have the messages.
|
|
61
|
+
const messages = {
|
|
62
|
+
"App": {
|
|
63
|
+
"hello": 'Hello {username}!'
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
function Root() {
|
|
68
|
+
return (
|
|
69
|
+
<IntlProvider messages={messages} locale="en">
|
|
70
|
+
<App user={{name: 'Jane'}} />
|
|
71
|
+
</IntlProvider>
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function App({user}) {
|
|
76
|
+
const t = useTranslations('App');
|
|
77
|
+
return <h1>{t('hello', {username: user.name})}</h1>;
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Have a look at [the minimal setup example](https://github.com/amannn/next-intl/tree/main/examples/example-use-intl) to explore a working app.
|
|
82
|
+
|
|
83
|
+
### [→ Read the docs](https://next-intl-docs.vercel.app/docs/environments/core-library)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/types/src/_IntlProvider';
|
package/_useLocale.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/types/src/_useLocale';
|
package/core.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/types/src/core';
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var initializeConfig = require('./initializeConfig-o7L_y07P.js');
|
|
7
|
+
var IntlContext = require('./IntlContext-BKfsnzBx.js');
|
|
8
|
+
require('@formatjs/fast-memoize');
|
|
9
|
+
|
|
10
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
11
|
+
|
|
12
|
+
var React__default = /*#__PURE__*/_interopDefault(React);
|
|
13
|
+
|
|
14
|
+
function IntlProvider(_ref) {
|
|
15
|
+
let {
|
|
16
|
+
children,
|
|
17
|
+
defaultTranslationValues,
|
|
18
|
+
formats,
|
|
19
|
+
getMessageFallback,
|
|
20
|
+
locale,
|
|
21
|
+
messages,
|
|
22
|
+
now,
|
|
23
|
+
onError,
|
|
24
|
+
timeZone
|
|
25
|
+
} = _ref;
|
|
26
|
+
// The formatter cache is released when the locale changes. For
|
|
27
|
+
// long-running apps with a persistent `IntlProvider` at the root,
|
|
28
|
+
// this can reduce the memory footprint (e.g. in React Native).
|
|
29
|
+
const cache = React.useMemo(() => {
|
|
30
|
+
return initializeConfig.createCache();
|
|
31
|
+
}, [locale]);
|
|
32
|
+
const formatters = React.useMemo(() => initializeConfig.createIntlFormatters(cache), [cache]);
|
|
33
|
+
|
|
34
|
+
// Memoizing this value helps to avoid triggering a re-render of all
|
|
35
|
+
// context consumers in case the configuration didn't change. However,
|
|
36
|
+
// if some of the non-primitive values change, a re-render will still
|
|
37
|
+
// be triggered. Note that there's no need to put `memo` on `IntlProvider`
|
|
38
|
+
// itself, because the `children` typically change on every render.
|
|
39
|
+
// There's some burden on the consumer side if it's important to reduce
|
|
40
|
+
// re-renders, put that's how React works.
|
|
41
|
+
// See: https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-mostly-complete-guide-to-react-rendering-behavior/#context-updates-and-render-optimizations
|
|
42
|
+
const value = React.useMemo(() => ({
|
|
43
|
+
...initializeConfig.initializeConfig({
|
|
44
|
+
locale,
|
|
45
|
+
defaultTranslationValues,
|
|
46
|
+
formats,
|
|
47
|
+
getMessageFallback,
|
|
48
|
+
messages,
|
|
49
|
+
now,
|
|
50
|
+
onError,
|
|
51
|
+
timeZone
|
|
52
|
+
}),
|
|
53
|
+
formatters,
|
|
54
|
+
cache
|
|
55
|
+
}), [cache, defaultTranslationValues, formats, formatters, getMessageFallback, locale, messages, now, onError, timeZone]);
|
|
56
|
+
return /*#__PURE__*/React__default.default.createElement(IntlContext.IntlContext.Provider, {
|
|
57
|
+
value: value
|
|
58
|
+
}, children);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
exports.IntlProvider = IntlProvider;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
var IntlContext = require('./IntlContext-BKfsnzBx.js');
|
|
5
|
+
|
|
6
|
+
function useIntlContext() {
|
|
7
|
+
const context = React.useContext(IntlContext.IntlContext);
|
|
8
|
+
if (!context) {
|
|
9
|
+
throw new Error('No intl context found. Have you configured the provider? See https://next-intl-docs.vercel.app/docs/usage/configuration#client-server-components' );
|
|
10
|
+
}
|
|
11
|
+
return context;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function useLocale() {
|
|
15
|
+
return useIntlContext().locale;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
exports.useIntlContext = useIntlContext;
|
|
19
|
+
exports.useLocale = useLocale;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var initializeConfig = require('./initializeConfig-o7L_y07P.js');
|
|
6
|
+
var createFormatter = require('./createFormatter-TZTkYRKI.js');
|
|
7
|
+
require('@formatjs/fast-memoize');
|
|
8
|
+
require('intl-messageformat');
|
|
9
|
+
require('react');
|
|
10
|
+
|
|
11
|
+
function createTranslatorImpl(_ref, namespacePrefix) {
|
|
12
|
+
let {
|
|
13
|
+
messages,
|
|
14
|
+
namespace,
|
|
15
|
+
...rest
|
|
16
|
+
} = _ref;
|
|
17
|
+
// The `namespacePrefix` is part of the type system.
|
|
18
|
+
// See the comment in the function invocation.
|
|
19
|
+
messages = messages[namespacePrefix];
|
|
20
|
+
namespace = createFormatter.resolveNamespace(namespace, namespacePrefix);
|
|
21
|
+
return createFormatter.createBaseTranslator({
|
|
22
|
+
...rest,
|
|
23
|
+
messages,
|
|
24
|
+
namespace
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Translates messages from the given namespace by using the ICU syntax.
|
|
30
|
+
* See https://formatjs.io/docs/core-concepts/icu-syntax.
|
|
31
|
+
*
|
|
32
|
+
* If no namespace is provided, all available messages are returned.
|
|
33
|
+
* The namespace can also indicate nesting by using a dot
|
|
34
|
+
* (e.g. `namespace.Component`).
|
|
35
|
+
*/
|
|
36
|
+
function createTranslator(_ref) {
|
|
37
|
+
let {
|
|
38
|
+
_cache = initializeConfig.createCache(),
|
|
39
|
+
_formatters = initializeConfig.createIntlFormatters(_cache),
|
|
40
|
+
getMessageFallback = initializeConfig.defaultGetMessageFallback,
|
|
41
|
+
messages,
|
|
42
|
+
namespace,
|
|
43
|
+
onError = initializeConfig.defaultOnError,
|
|
44
|
+
...rest
|
|
45
|
+
} = _ref;
|
|
46
|
+
// We have to wrap the actual function so the type inference for the optional
|
|
47
|
+
// namespace works correctly. See https://stackoverflow.com/a/71529575/343045
|
|
48
|
+
// The prefix ("!") is arbitrary.
|
|
49
|
+
return createTranslatorImpl({
|
|
50
|
+
...rest,
|
|
51
|
+
onError,
|
|
52
|
+
cache: _cache,
|
|
53
|
+
formatters: _formatters,
|
|
54
|
+
getMessageFallback,
|
|
55
|
+
// @ts-expect-error `messages` is allowed to be `undefined` here and will be handled internally
|
|
56
|
+
messages: {
|
|
57
|
+
'!': messages
|
|
58
|
+
},
|
|
59
|
+
namespace: namespace ? "!.".concat(namespace) : '!'
|
|
60
|
+
}, '!');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
exports.IntlError = initializeConfig.IntlError;
|
|
64
|
+
exports.IntlErrorCode = initializeConfig.IntlErrorCode;
|
|
65
|
+
exports._createCache = initializeConfig.createCache;
|
|
66
|
+
exports._createIntlFormatters = initializeConfig.createIntlFormatters;
|
|
67
|
+
exports.initializeConfig = initializeConfig.initializeConfig;
|
|
68
|
+
exports.createFormatter = createFormatter.createFormatter;
|
|
69
|
+
exports.createTranslator = createTranslator;
|