Socket
Socket
Sign inDemoInstall

use-intl

Package Overview
Dependencies
Maintainers
1
Versions
216
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-intl - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

dist/convertFormatsToIntlMessageFormat.d.ts

1

dist/index.d.ts

@@ -6,2 +6,3 @@ export { default as IntlProvider } from './IntlProvider';

export { default as useIntl } from './useIntl';
export { default as Formats } from './Formats';
export { default as IntlError, IntlErrorCode } from './IntlError';
/// <reference types="react" />
import Formats from './Formats';
import IntlError from './IntlError';

@@ -7,2 +8,3 @@ import IntlMessages from './IntlMessages';

locale: string;
formats?: Partial<Formats>;
onError(error: IntlError): void;

@@ -9,0 +11,0 @@ getMessageFallback(info: {

export declare const enum IntlErrorCode {
MISSING_MESSAGE = "MISSING_MESSAGE",
MISSING_FORMAT = "MISSING_FORMAT",
INSUFFICIENT_PATH = "INSUFFICIENT_PATH",

@@ -4,0 +5,0 @@ INVALID_MESSAGE = "INVALID_MESSAGE",

import { ReactNode } from 'react';
import Formats from './Formats';
import IntlMessages from './IntlMessages';

@@ -9,2 +10,5 @@ import { IntlError } from '.';

locale: string;
/** Global formats can be provided to achieve consistent
* formatting across components. */
formats?: Partial<Formats>;
/** This callback will be invoked when an error is encountered during

@@ -11,0 +15,0 @@ * resolving a message or formatting it. This defaults to `console.error` to

@@ -185,2 +185,16 @@ 'use strict';

/**
* `intl-messageformat` uses separate keys for `date` and `time`, but there's
* only one native API: `Intl.DateTimeFormat`. Additionally you might want to
* include both a time and a date in a value, therefore the separation doesn't
* seem so useful. We offer a single `dateTime` namespace instead, but we have
* to convert the format before `intl-messageformat` can be used.
*/
function convertFormatsToIntlMessageFormat(formats) {
return _extends({}, formats, {
date: formats == null ? void 0 : formats.dateTime,
time: formats == null ? void 0 : formats.dateTime
});
}
function useIntlContext() {

@@ -196,7 +210,2 @@ var context = React.useContext(IntlContext);

function useLocale() {
var context = useIntlContext();
return context.locale;
}
function resolvePath(messages, idPath, namespace) {

@@ -250,6 +259,10 @@ var message = messages;

function useTranslations(namespace) {
var context = useIntlContext();
var locale = useLocale();
var _useIntlContext = useIntlContext(),
globalFormats = _useIntlContext.formats,
getMessageFallback = _useIntlContext.getMessageFallback,
locale = _useIntlContext.locale,
allMessages = _useIntlContext.messages,
onError = _useIntlContext.onError;
var cachedFormatsByLocaleRef = React.useRef({});
var allMessages = context.messages;
var messagesOrError = React.useMemo(function () {

@@ -268,6 +281,6 @@ try {

, error.message);
context.onError(intlError);
onError(intlError);
return intlError;
}
}, [allMessages, context, namespace]);
}, [allMessages, namespace, onError]);

@@ -287,4 +300,4 @@ function translate(

var error = new IntlError(code, message);
context.onError(error);
return context.getMessageFallback({
onError(error);
return getMessageFallback({
error: error,

@@ -298,3 +311,3 @@ key: key,

// We have already warned about this during render
return context.getMessageFallback({
return getMessageFallback({
error: messagesOrError,

@@ -329,3 +342,3 @@ key: key,

try {
messageFormat = new IntlMessageFormat(message, locale, formats);
messageFormat = new IntlMessageFormat(message, locale, convertFormatsToIntlMessageFormat(_extends({}, globalFormats, formats)));
} catch (error) {

@@ -405,12 +418,59 @@ return getFallbackFromError("INVALID_MESSAGE"

function useIntl() {
var locale = useLocale();
var _useIntlContext = useIntlContext(),
formats = _useIntlContext.formats,
locale = _useIntlContext.locale,
onError = _useIntlContext.onError;
function formatDateTime(value, options) {
return new Intl.DateTimeFormat(locale, options).format(value);
function resolveFormatOrOptions(typeFormats, formatOrOptions) {
var format;
if (typeof formatOrOptions === 'string') {
var formatName = formatOrOptions;
format = typeFormats == null ? void 0 : typeFormats[formatName];
if (!format) {
var error = new IntlError("MISSING_FORMAT"
/* MISSING_FORMAT */
, "Format `" + formatName + "` is not available. You can configure it on the provider or provide custom options." );
onError(error);
throw error;
}
} else {
format = formatOrOptions;
}
return format;
}
function formatNumber(value, options) {
return new Intl.NumberFormat(locale, options).format(value);
function getFormattedValue(value, formatOrOptions, typeFormats, formatter) {
var format;
try {
format = resolveFormatOrOptions(typeFormats, formatOrOptions);
} catch (error) {
return String(value);
}
try {
return formatter(format);
} catch (error) {
onError(new IntlError("FORMATTING_ERROR"
/* FORMATTING_ERROR */
, error.message));
return String(value);
}
}
function formatDateTime(value, formatOrOptions) {
return getFormattedValue(value, formatOrOptions, _extends({}, formats == null ? void 0 : formats.dateTime), function (format) {
return new Intl.DateTimeFormat(locale, format).format(value);
});
}
function formatNumber(value, formatOrOptions) {
return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.number, function (format) {
return new Intl.NumberFormat(locale, format).format(value);
});
}
function formatRelativeTime(date, now) {

@@ -425,5 +485,12 @@ var dateDate = date instanceof Date ? date : new Date(date);

return new Intl.RelativeTimeFormat(locale, {
numeric: 'auto'
}).format(value, unit);
try {
return new Intl.RelativeTimeFormat(locale, {
numeric: 'auto'
}).format(value, unit);
} catch (error) {
onError(new IntlError("FORMATTING_ERROR"
/* FORMATTING_ERROR */
, error.message));
return String(date);
}
}

@@ -430,0 +497,0 @@

2

dist/use-intl.cjs.production.min.js

@@ -1,2 +0,2 @@

"use strict";function r(r){return r&&"object"==typeof r&&"default"in r?r.default:r}Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=r(e),n=r(require("intl-messageformat"));function o(){return(o=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}function u(r){return(u=Object.setPrototypeOf?Object.getPrototypeOf:function(r){return r.__proto__||Object.getPrototypeOf(r)})(r)}function a(r,e){return(a=Object.setPrototypeOf||function(r,e){return r.__proto__=e,r})(r,e)}function i(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(r){return!1}}function c(r,e,t){return(c=i()?Reflect.construct:function(r,e,t){var n=[null];n.push.apply(n,e);var o=new(Function.bind.apply(r,n));return t&&a(o,t.prototype),o}).apply(null,arguments)}function f(r){var e="function"==typeof Map?new Map:void 0;return(f=function(r){if(null===r||-1===Function.toString.call(r).indexOf("[native code]"))return r;if("function"!=typeof r)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(r))return e.get(r);e.set(r,t)}function t(){return c(r,arguments,u(this).constructor)}return t.prototype=Object.create(r.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),a(t,r)})(r)}var l=e.createContext(void 0);function s(r){return[r.namespace,r.key].filter((function(r){return null!=r})).join(".")}var p=function(r){var e,t;function n(e,t){var n,o=e;return t&&(o+=": "+t),(n=r.call(this,o)||this).code=e,t&&(n.originalMessage=t),n}return t=r,(e=n).prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t,n}(f(Error));function v(){var r=e.useContext(l);if(!r)throw new Error(void 0);return r}function y(){return v().locale}function m(r,e,t){var n=r;return e.split(".").forEach((function(r){var e=n[r];if(null==r||null==e)throw new Error(void 0);n=e})),n}exports.IntlError=p,exports.IntlProvider=function(r){var e=r.children,n=r.onError,u=void 0===n?console.error:n,a=r.getMessageFallback,i=void 0===a?s:a,c=function(r,e){if(null==r)return{};var t,n,o={},u=Object.keys(r);for(n=0;n<u.length;n++)e.indexOf(t=u[n])>=0||(o[t]=r[t]);return o}(r,["children","onError","getMessageFallback"]);return t.createElement(l.Provider,{value:o({},c,{onError:u,getMessageFallback:i})},e)},exports.useIntl=function(){var r=y();return{formatDateTime:function(e,t){return new Intl.DateTimeFormat(r,t).format(e)},formatNumber:function(e,t){return new Intl.NumberFormat(r,t).format(e)},formatRelativeTime:function(e,t){var n=e instanceof Date?e:new Date(e),o=t instanceof Date?t:new Date(t),u=function(r){var e,t,n=Math.abs(r);return n<60?(t="second",e=Math.round(r)):n<3600?(t="minute",e=Math.round(r/60)):n<86400?(t="hour",e=Math.round(r/3600)):n<604800?(t="day",e=Math.round(r/86400)):n<2628e3?(t="week",e=Math.round(r/604800)):n<31536e3?(t="month",e=Math.round(r/2628e3)):(t="year",e=Math.round(r/31536e3)),{value:e,unit:t}}((n.getTime()-o.getTime())/1e3),a=u.unit,i=u.value;return new Intl.RelativeTimeFormat(r,{numeric:"auto"}).format(i,a)}}},exports.useTranslations=function(r){var t=v(),o=y(),u=e.useRef({}),a=t.messages,i=e.useMemo((function(){try{var e=r?m(a,r):a;if(!e)throw new Error(void 0);return e}catch(r){var n=new p("MISSING_MESSAGE",r.message);return t.onError(n),n}}),[a,t,r]);return function(a,c,f){var l,s=u.current;function v(e,n){var o=new p(e,n);return t.onError(o),t.getMessageFallback({error:o,key:a,namespace:r})}if(i instanceof p)return t.getMessageFallback({error:i,key:a,namespace:r});var y,d=i;if(null!=(l=s[o])&&l[a])y=s[o][a];else{var h;try{h=m(d,a)}catch(r){return v("MISSING_MESSAGE",r.message)}if("object"==typeof h)return v("INSUFFICIENT_PATH",void 0);try{y=new n(h,o,f)}catch(r){return v("INVALID_MESSAGE",r.message)}s[o]||(s[o]={}),s[o][a]=y}try{var g=y.format(function(r){if(!r)return r;var t={};return Object.keys(r).forEach((function(n){var o=r[n];t[n]="function"==typeof o?function(r){var t=o(r);return e.isValidElement(t)?e.cloneElement(t,{key:t.key||n+String(r)}):t}:o})),t}(c));if(null==g)throw new Error(void 0);return g}catch(r){return v("FORMATTING_ERROR",r.message)}}};
"use strict";function r(r){return r&&"object"==typeof r&&"default"in r?r.default:r}Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react"),e=r(t),n=r(require("intl-messageformat"));function o(){return(o=Object.assign||function(r){for(var t=1;t<arguments.length;t++){var e=arguments[t];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(r[n]=e[n])}return r}).apply(this,arguments)}function u(r){return(u=Object.setPrototypeOf?Object.getPrototypeOf:function(r){return r.__proto__||Object.getPrototypeOf(r)})(r)}function a(r,t){return(a=Object.setPrototypeOf||function(r,t){return r.__proto__=t,r})(r,t)}function i(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(r){return!1}}function c(r,t,e){return(c=i()?Reflect.construct:function(r,t,e){var n=[null];n.push.apply(n,t);var o=new(Function.bind.apply(r,n));return e&&a(o,e.prototype),o}).apply(null,arguments)}function f(r){var t="function"==typeof Map?new Map:void 0;return(f=function(r){if(null===r||-1===Function.toString.call(r).indexOf("[native code]"))return r;if("function"!=typeof r)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(r))return t.get(r);t.set(r,e)}function e(){return c(r,arguments,u(this).constructor)}return e.prototype=Object.create(r.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),a(e,r)})(r)}var l=t.createContext(void 0);function s(r){return[r.namespace,r.key].filter((function(r){return null!=r})).join(".")}var v=function(r){var t,e;function n(t,e){var n,o=t;return e&&(o+=": "+e),(n=r.call(this,o)||this).code=t,e&&(n.originalMessage=e),n}return e=r,(t=n).prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e,n}(f(Error));function p(){var r=t.useContext(l);if(!r)throw new Error(void 0);return r}function m(r,t,e){var n=r;return t.split(".").forEach((function(r){var t=n[r];if(null==r||null==t)throw new Error(void 0);n=t})),n}exports.IntlError=v,exports.IntlProvider=function(r){var t=r.children,n=r.onError,u=void 0===n?console.error:n,a=r.getMessageFallback,i=void 0===a?s:a,c=function(r,t){if(null==r)return{};var e,n,o={},u=Object.keys(r);for(n=0;n<u.length;n++)t.indexOf(e=u[n])>=0||(o[e]=r[e]);return o}(r,["children","onError","getMessageFallback"]);return e.createElement(l.Provider,{value:o({},c,{onError:u,getMessageFallback:i})},t)},exports.useIntl=function(){var r=p(),t=r.formats,e=r.locale,n=r.onError;function u(r,t,e,o){var u;try{u=function(r,t){var e;if("string"==typeof t){if(!(e=null==r?void 0:r[t])){var o=new v("MISSING_FORMAT",void 0);throw n(o),o}}else e=t;return e}(e,t)}catch(t){return String(r)}try{return o(u)}catch(t){return n(new v("FORMATTING_ERROR",t.message)),String(r)}}return{formatDateTime:function(r,n){return u(r,n,o({},null==t?void 0:t.dateTime),(function(t){return new Intl.DateTimeFormat(e,t).format(r)}))},formatNumber:function(r,n){return u(r,n,null==t?void 0:t.number,(function(t){return new Intl.NumberFormat(e,t).format(r)}))},formatRelativeTime:function(r,t){var o=r instanceof Date?r:new Date(r),u=t instanceof Date?t:new Date(t),a=function(r){var t,e,n=Math.abs(r);return n<60?(e="second",t=Math.round(r)):n<3600?(e="minute",t=Math.round(r/60)):n<86400?(e="hour",t=Math.round(r/3600)):n<604800?(e="day",t=Math.round(r/86400)):n<2628e3?(e="week",t=Math.round(r/604800)):n<31536e3?(e="month",t=Math.round(r/2628e3)):(e="year",t=Math.round(r/31536e3)),{value:t,unit:e}}((o.getTime()-u.getTime())/1e3),i=a.unit,c=a.value;try{return new Intl.RelativeTimeFormat(e,{numeric:"auto"}).format(c,i)}catch(t){return n(new v("FORMATTING_ERROR",t.message)),String(r)}}}},exports.useTranslations=function(r){var e=p(),u=e.formats,a=e.getMessageFallback,i=e.locale,c=e.messages,f=e.onError,l=t.useRef({}),s=t.useMemo((function(){try{var t=r?m(c,r):c;if(!t)throw new Error(void 0);return t}catch(r){var e=new v("MISSING_MESSAGE",r.message);return f(e),e}}),[c,r,f]);return function(e,c,p){var d,y=l.current;function h(t,n){var o=new v(t,n);return f(o),a({error:o,key:e,namespace:r})}if(s instanceof v)return a({error:s,key:e,namespace:r});var g,w=s;if(null!=(d=y[i])&&d[e])g=y[i][e];else{var E;try{E=m(w,e)}catch(r){return h("MISSING_MESSAGE",r.message)}if("object"==typeof E)return h("INSUFFICIENT_PATH",void 0);try{g=new n(E,i,function(r){return o({},r,{date:null==r?void 0:r.dateTime,time:null==r?void 0:r.dateTime})}(o({},u,p)))}catch(r){return h("INVALID_MESSAGE",r.message)}y[i]||(y[i]={}),y[i][e]=g}try{var M=g.format(function(r){if(!r)return r;var e={};return Object.keys(r).forEach((function(n){var o=r[n];e[n]="function"==typeof o?function(r){var e=o(r);return t.isValidElement(e)?t.cloneElement(e,{key:e.key||n+String(r)}):e}:o})),e}(c));if(null==M)throw new Error(void 0);return M}catch(r){return h("FORMATTING_ERROR",r.message)}}};
//# sourceMappingURL=use-intl.cjs.production.min.js.map

@@ -178,2 +178,16 @@ import React, { createContext, useContext, useRef, useMemo, isValidElement, cloneElement } from 'react';

/**
* `intl-messageformat` uses separate keys for `date` and `time`, but there's
* only one native API: `Intl.DateTimeFormat`. Additionally you might want to
* include both a time and a date in a value, therefore the separation doesn't
* seem so useful. We offer a single `dateTime` namespace instead, but we have
* to convert the format before `intl-messageformat` can be used.
*/
function convertFormatsToIntlMessageFormat(formats) {
return _extends({}, formats, {
date: formats == null ? void 0 : formats.dateTime,
time: formats == null ? void 0 : formats.dateTime
});
}
function useIntlContext() {

@@ -189,7 +203,2 @@ var context = useContext(IntlContext);

function useLocale() {
var context = useIntlContext();
return context.locale;
}
function resolvePath(messages, idPath, namespace) {

@@ -243,6 +252,10 @@ var message = messages;

function useTranslations(namespace) {
var context = useIntlContext();
var locale = useLocale();
var _useIntlContext = useIntlContext(),
globalFormats = _useIntlContext.formats,
getMessageFallback = _useIntlContext.getMessageFallback,
locale = _useIntlContext.locale,
allMessages = _useIntlContext.messages,
onError = _useIntlContext.onError;
var cachedFormatsByLocaleRef = useRef({});
var allMessages = context.messages;
var messagesOrError = useMemo(function () {

@@ -261,6 +274,6 @@ try {

, error.message);
context.onError(intlError);
onError(intlError);
return intlError;
}
}, [allMessages, context, namespace]);
}, [allMessages, namespace, onError]);

@@ -280,4 +293,4 @@ function translate(

var error = new IntlError(code, message);
context.onError(error);
return context.getMessageFallback({
onError(error);
return getMessageFallback({
error: error,

@@ -291,3 +304,3 @@ key: key,

// We have already warned about this during render
return context.getMessageFallback({
return getMessageFallback({
error: messagesOrError,

@@ -322,3 +335,3 @@ key: key,

try {
messageFormat = new IntlMessageFormat(message, locale, formats);
messageFormat = new IntlMessageFormat(message, locale, convertFormatsToIntlMessageFormat(_extends({}, globalFormats, formats)));
} catch (error) {

@@ -398,12 +411,59 @@ return getFallbackFromError("INVALID_MESSAGE"

function useIntl() {
var locale = useLocale();
var _useIntlContext = useIntlContext(),
formats = _useIntlContext.formats,
locale = _useIntlContext.locale,
onError = _useIntlContext.onError;
function formatDateTime(value, options) {
return new Intl.DateTimeFormat(locale, options).format(value);
function resolveFormatOrOptions(typeFormats, formatOrOptions) {
var format;
if (typeof formatOrOptions === 'string') {
var formatName = formatOrOptions;
format = typeFormats == null ? void 0 : typeFormats[formatName];
if (!format) {
var error = new IntlError("MISSING_FORMAT"
/* MISSING_FORMAT */
, process.env.NODE_ENV !== "production" ? "Format `" + formatName + "` is not available. You can configure it on the provider or provide custom options." : undefined);
onError(error);
throw error;
}
} else {
format = formatOrOptions;
}
return format;
}
function formatNumber(value, options) {
return new Intl.NumberFormat(locale, options).format(value);
function getFormattedValue(value, formatOrOptions, typeFormats, formatter) {
var format;
try {
format = resolveFormatOrOptions(typeFormats, formatOrOptions);
} catch (error) {
return String(value);
}
try {
return formatter(format);
} catch (error) {
onError(new IntlError("FORMATTING_ERROR"
/* FORMATTING_ERROR */
, error.message));
return String(value);
}
}
function formatDateTime(value, formatOrOptions) {
return getFormattedValue(value, formatOrOptions, _extends({}, formats == null ? void 0 : formats.dateTime), function (format) {
return new Intl.DateTimeFormat(locale, format).format(value);
});
}
function formatNumber(value, formatOrOptions) {
return getFormattedValue(value, formatOrOptions, formats == null ? void 0 : formats.number, function (format) {
return new Intl.NumberFormat(locale, format).format(value);
});
}
function formatRelativeTime(date, now) {

@@ -418,5 +478,12 @@ var dateDate = date instanceof Date ? date : new Date(date);

return new Intl.RelativeTimeFormat(locale, {
numeric: 'auto'
}).format(value, unit);
try {
return new Intl.RelativeTimeFormat(locale, {
numeric: 'auto'
}).format(value, unit);
} catch (error) {
onError(new IntlError("FORMATTING_ERROR"
/* FORMATTING_ERROR */
, error.message));
return String(date);
}
}

@@ -423,0 +490,0 @@

export default function useIntl(): {
formatDateTime: (value: number | Date, options?: Intl.DateTimeFormatOptions | undefined) => string;
formatNumber: (value: number, options?: Intl.NumberFormatOptions | undefined) => string;
formatDateTime: (value: number | Date, formatOrOptions?: string | Intl.DateTimeFormatOptions | undefined) => string;
formatNumber: (value: number, formatOrOptions?: string | Intl.NumberFormatOptions | undefined) => string;
formatRelativeTime: (date: number | Date, now: number | Date) => string;
};

@@ -1,5 +0,1 @@

export default function useIntlContext(): {
messages: import("./IntlMessages").default;
locale: string;
onError(error: import("./IntlError").default): void;
};
export default function useIntlContext(): import("./IntlContext").IntlContextShape;

@@ -1,3 +0,3 @@

import { Formats } from 'intl-messageformat';
import { ReactNode } from 'react';
import Formats from './Formats';
/**

@@ -4,0 +4,0 @@ * Translates messages from the given namespace by using the ICU syntax.

{
"name": "use-intl",
"version": "0.2.0",
"version": "0.3.0",
"sideEffects": false,

@@ -5,0 +5,0 @@ "author": "Jan Amann <jan@amann.me>",

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

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc