next-intl
Advanced tools
Comparing version
@@ -1,1 +0,4 @@ | ||
export default function useTranslations(): void; | ||
export { default as NextIntlProvider } from './NextIntlProvider'; | ||
export { default as NextIntlMessages } from './NextIntlMessages'; | ||
export { default as useTranslations } from './useTranslations'; | ||
export { default as TranslationValues } from './TranslationValues'; |
@@ -5,6 +5,121 @@ 'use strict'; | ||
function useTranslations() {// TODO | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var React = require('react'); | ||
var React__default = _interopDefault(React); | ||
var IntlMessageFormat = _interopDefault(require('intl-messageformat')); | ||
var router = require('next/router'); | ||
var NextIntlContext = /*#__PURE__*/React.createContext(undefined); | ||
function NextIntlProvider(_ref) { | ||
var children = _ref.children, | ||
messages = _ref.messages; | ||
return React__default.createElement(NextIntlContext.Provider, { | ||
value: { | ||
messages: messages | ||
} | ||
}, children); | ||
} | ||
exports.default = useTranslations; | ||
function resolvePath(messages, idPath) { | ||
var message = messages; | ||
idPath.split('.').forEach(function (part) { | ||
var next = message[part]; | ||
{ | ||
if (!part || !next) { | ||
throw new Error("Could not resolve `" + idPath + "` in `" + JSON.stringify(messages, null, 2) + "`."); | ||
} | ||
} | ||
message = next; | ||
}); | ||
return message; | ||
} | ||
/** | ||
* Translates messages from the given path by using the ICU syntax. | ||
* See https://formatjs.io/docs/core-concepts/icu-syntax. | ||
* | ||
* If no path is provided, all available messages are returned. | ||
* | ||
* The path can also indicate nesting by using a dot (e.g. `namespace.Component`). | ||
*/ | ||
function useTranslations(path) { | ||
var context = React.useContext(NextIntlContext); | ||
var _useRouter = router.useRouter(), | ||
locale = _useRouter.locale; | ||
var cachedFormatsByLocaleRef = React.useRef({}); | ||
if (!context) { | ||
{ | ||
throw new Error('No NextIntlContext configured.'); | ||
} | ||
} | ||
var allMessages = context.messages; | ||
var messages = React.useMemo(function () { | ||
return path ? resolvePath(allMessages, path) : allMessages; | ||
}, [allMessages, path]); | ||
{ | ||
if (!messages) { | ||
throw new Error("No messages for component `" + path + "` found."); | ||
} | ||
} | ||
function translate( | ||
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */ | ||
idPath, values) { | ||
var _cachedFormatsByLocal; | ||
if (!locale) { | ||
{ | ||
throw new Error('No `locale` received from `useRouter()`'); | ||
} | ||
} | ||
var cachedFormatsByLocale = cachedFormatsByLocaleRef.current; | ||
var messageFormat; | ||
if ((_cachedFormatsByLocal = cachedFormatsByLocale[locale]) == null ? void 0 : _cachedFormatsByLocal[idPath]) { | ||
messageFormat = cachedFormatsByLocale[locale][idPath]; | ||
} else { | ||
var message = resolvePath(messages, idPath); | ||
if (typeof message === 'object') { | ||
{ | ||
throw new Error("Insufficient path specified for `" + idPath + "` in `" + path + "`."); | ||
} | ||
} | ||
messageFormat = new IntlMessageFormat(message, locale); | ||
if (!cachedFormatsByLocale[locale]) { | ||
cachedFormatsByLocale[locale] = {}; | ||
} | ||
cachedFormatsByLocale[locale][idPath] = messageFormat; | ||
} | ||
var formattedMessage = messageFormat.format(values); | ||
{ | ||
if (formattedMessage === undefined) { | ||
throw new Error("Unable to format " + path + "." + idPath); | ||
} | ||
} | ||
return formattedMessage; | ||
} | ||
return translate; | ||
} | ||
exports.NextIntlProvider = NextIntlProvider; | ||
exports.useTranslations = useTranslations; | ||
//# sourceMappingURL=next-intl.cjs.development.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=function(){}; | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var r=require("react"),t=e(r),n=e(require("intl-messageformat")),o=require("next/router"),u=r.createContext(void 0);function s(e,r){var t=e;return r.split(".").forEach((function(e){t=t[e]})),t}exports.NextIntlProvider=function(e){return t.createElement(u.Provider,{value:{messages:e.messages}},e.children)},exports.useTranslations=function(e){var t=r.useContext(u),i=o.useRouter().locale,a=r.useRef({});if(!t)throw new Error;var f=t.messages,c=r.useMemo((function(){return e?s(f,e):f}),[f,e]);return function(e,r){var t;if(!i)throw new Error;var o,u=a.current;if(null==(t=u[i])?void 0:t[e])o=u[i][e];else{var f=s(c,e);if("object"==typeof f)throw new Error;o=new n(f,i),u[i]||(u[i]={}),u[i][e]=o}return o.format(r)}}; | ||
//# sourceMappingURL=next-intl.cjs.production.min.js.map |
@@ -1,5 +0,122 @@ | ||
function useTranslations() {// TODO | ||
import React, { createContext, useContext, useRef, useMemo } from 'react'; | ||
import IntlMessageFormat from 'intl-messageformat'; | ||
import { useRouter } from 'next/router'; | ||
var NextIntlContext = /*#__PURE__*/createContext(undefined); | ||
function NextIntlProvider(_ref) { | ||
var children = _ref.children, | ||
messages = _ref.messages; | ||
return React.createElement(NextIntlContext.Provider, { | ||
value: { | ||
messages: messages | ||
} | ||
}, children); | ||
} | ||
export default useTranslations; | ||
function resolvePath(messages, idPath) { | ||
var message = messages; | ||
idPath.split('.').forEach(function (part) { | ||
var next = message[part]; | ||
if (process.env.NODE_ENV !== "production") { | ||
if (!part || !next) { | ||
throw new Error("Could not resolve `" + idPath + "` in `" + JSON.stringify(messages, null, 2) + "`."); | ||
} | ||
} | ||
message = next; | ||
}); | ||
return message; | ||
} | ||
/** | ||
* Translates messages from the given path by using the ICU syntax. | ||
* See https://formatjs.io/docs/core-concepts/icu-syntax. | ||
* | ||
* If no path is provided, all available messages are returned. | ||
* | ||
* The path can also indicate nesting by using a dot (e.g. `namespace.Component`). | ||
*/ | ||
function useTranslations(path) { | ||
var context = useContext(NextIntlContext); | ||
var _useRouter = useRouter(), | ||
locale = _useRouter.locale; | ||
var cachedFormatsByLocaleRef = useRef({}); | ||
if (!context) { | ||
if (process.env.NODE_ENV !== "production") { | ||
throw new Error('No NextIntlContext configured.'); | ||
} else { | ||
throw new Error(); | ||
} | ||
} | ||
var allMessages = context.messages; | ||
var messages = useMemo(function () { | ||
return path ? resolvePath(allMessages, path) : allMessages; | ||
}, [allMessages, path]); | ||
if (process.env.NODE_ENV !== "production") { | ||
if (!messages) { | ||
throw new Error("No messages for component `" + path + "` found."); | ||
} | ||
} | ||
function translate( | ||
/** Use a dot to indicate a level of nesting (e.g. `namespace.nestedLabel`). */ | ||
idPath, values) { | ||
var _cachedFormatsByLocal; | ||
if (!locale) { | ||
if (process.env.NODE_ENV !== "production") { | ||
throw new Error('No `locale` received from `useRouter()`'); | ||
} else { | ||
throw new Error(); | ||
} | ||
} | ||
var cachedFormatsByLocale = cachedFormatsByLocaleRef.current; | ||
var messageFormat; | ||
if ((_cachedFormatsByLocal = cachedFormatsByLocale[locale]) == null ? void 0 : _cachedFormatsByLocal[idPath]) { | ||
messageFormat = cachedFormatsByLocale[locale][idPath]; | ||
} else { | ||
var message = resolvePath(messages, idPath); | ||
if (typeof message === 'object') { | ||
if (process.env.NODE_ENV !== "production") { | ||
throw new Error("Insufficient path specified for `" + idPath + "` in `" + path + "`."); | ||
} else { | ||
throw new Error(); | ||
} | ||
} | ||
messageFormat = new IntlMessageFormat(message, locale); | ||
if (!cachedFormatsByLocale[locale]) { | ||
cachedFormatsByLocale[locale] = {}; | ||
} | ||
cachedFormatsByLocale[locale][idPath] = messageFormat; | ||
} | ||
var formattedMessage = messageFormat.format(values); | ||
if (process.env.NODE_ENV !== "production") { | ||
if (formattedMessage === undefined) { | ||
throw new Error("Unable to format " + path + "." + idPath); | ||
} | ||
} | ||
return formattedMessage; | ||
} | ||
return translate; | ||
} | ||
export { NextIntlProvider, useTranslations }; | ||
//# sourceMappingURL=next-intl.esm.js.map |
{ | ||
"name": "next-intl", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"license": "MIT", | ||
@@ -26,2 +26,3 @@ "description": "TODO", | ||
"peerDependencies": { | ||
"next": "^10.0.0", | ||
"react": "^16.8.0 || ^17.0.0", | ||
@@ -31,2 +32,3 @@ "react-dom": "^16.8.0 || ^17.0.0" | ||
"dependencies": { | ||
"intl-messageformat": "^9.3.18", | ||
"tslib": "^2.0.0" | ||
@@ -36,2 +38,3 @@ }, | ||
"@testing-library/react": "^11.1.2", | ||
"next": "^10.0.2", | ||
"react": "^17.0.0", | ||
@@ -38,0 +41,0 @@ "react-dom": "^17.0.0" |
@@ -5,2 +5,32 @@ # next-intl | ||
TODO | ||
TODO | ||
short icu guide | ||
https://formatjs.io/docs/core-concepts/icu-syntax | ||
same api for react attributes and children | ||
make sure you have polyfills | ||
cache constructor with ast? | ||
// number (https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#examples) | ||
date https://formatjs.io/docs/intl-messageformat#datetime-skeleton | ||
select https://formatjs.io/docs/core-concepts/icu-syntax/#select-format | ||
get rid of key for rich text? | ||
// optional component name – return all | ||
// otherwise - componentize | ||
typically structure by components | ||
biggest strength of this library is that it's hooks only. therefore the same API for attributes as well as in children | ||
cache values as well? | ||
separate repo? | ||
TODO: | ||
- Pass currency to number? | ||
- Relative time | ||
- Check other features of react-intl |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
30520
1231.01%15
50%242
1173.68%36
620%5
66.67%4
33.33%3
50%7
600%1
Infinity%104
-5.45%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added