Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// TODO: Figure out if there is a ES2015 way to conditional load this
import "intl"
// NodeJS by default to not offer full ICU support and therefor break the unit tests
if (!areLocalesSupported([ "en", "de", "fr", "es" ]))
{
/* global IntlPolyfill */
Intl.NumberFormat = IntlPolyfill.NumberFormat
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat
}
const formats = IntlMessageFormat.formats
const getCachedNumberFormat = createFormatCache(Intl.NumberFormat)
const getCachedDateTimeFormat = createFormatCache(Intl.DateTimeFormat)
const getCachedMessageFormat = createFormatCache(IntlMessageFormat)
const getCachedRelativeFormat = createFormatCache(IntlRelativeFormat)
// A constant defined by the standard Intl.NumberFormat
// const maximumFractionDigits = 20;
// Unfortunately through formatting issues of percent values in IE
// we have to use a small value here, because IE (as of v11) seems to
// account the percent symbol + optional space to the fraction digits.
// See also: https://github.com/sebastian-software/vue-locale/issues/1#issuecomment-215396481
const maximumFractionDigits = 18;
function install(Vue, options)
{
var { language, currency, messages } = options
var locale = language
See the accompanying LICENSE file for terms.
*/
/* jshint esnext: true */
import IntlMessageFormat from 'intl-messageformat';
import IntlRelativeFormat from 'intl-relativeformat';
import createFormatCache from 'intl-format-cache';
import {extend} from './utils.js';
export {registerWith};
// -----------------------------------------------------------------------------
var getNumberFormat = createFormatCache(Intl.NumberFormat);
var getDateTimeFormat = createFormatCache(Intl.DateTimeFormat);
var getMessageFormat = createFormatCache(IntlMessageFormat);
var getRelativeFormat = createFormatCache(IntlRelativeFormat);
function registerWith(Handlebars) {
var SafeString = Handlebars.SafeString,
createFrame = Handlebars.createFrame,
escape = Handlebars.Utils.escapeExpression;
var helpers = {
intl : intl,
intlGet : intlGet,
formatDate : formatDate,
formatTime : formatTime,
formatRelative : formatRelative,
formatNumber : formatNumber,
/* jshint esnext: true */
import IntlMessageFormat from 'intl-messageformat';
import IntlRelativeFormat from 'intl-relativeformat';
import createFormatCache from 'intl-format-cache';
import {extend} from './utils.js';
export {registerWith};
// -----------------------------------------------------------------------------
var getNumberFormat = createFormatCache(Intl.NumberFormat);
var getDateTimeFormat = createFormatCache(Intl.DateTimeFormat);
var getMessageFormat = createFormatCache(IntlMessageFormat);
var getRelativeFormat = createFormatCache(IntlRelativeFormat);
function registerWith(Handlebars) {
var SafeString = Handlebars.SafeString,
createFrame = Handlebars.createFrame,
escape = Handlebars.Utils.escapeExpression;
var helpers = {
intl : intl,
intlGet : intlGet,
formatDate : formatDate,
formatTime : formatTime,
formatRelative : formatRelative,
formatNumber : formatNumber,
formatMessage : formatMessage,
formatHTMLMessage: formatHTMLMessage,
}
const customFormats = {}
if (numberFormat) {
customFormats.number = numberFormat
}
let additionalOptions
if (timezone) {
// https://github.com/formatjs/formatjs/tree/master/packages/intl-messageformat#formatters
// customize formatters to be able to render dates to a specific timezone
additionalOptions = {
formatters: {
getNumberFormat: memoizeIntlConstructor(Intl.NumberFormat),
getPluralRules: memoizeIntlConstructor(Intl.PluralRules),
getDateTimeFormat (locales, intlOptions) {
return {
format (date) {
if (!(date instanceof Date)) throw new Error('Invalid valid date passed to format')
const zone = momentTimezone.tz.zone(timezone)
if (!zone) throw new Error('Invalid timezone')
const clonedDate = new Date(date.getTime())
const fromOffset = clonedDate.getTimezoneOffset()
const toOffset = zone.parse(clonedDate)
const newDate = new Date(clonedDate.getTime() - (toOffset - fromOffset) * 60 * 1000)
return new Intl.DateTimeFormat(locales, intlOptions).format(newDate)
}
}
}
const customFormats = {}
if (numberFormat) {
customFormats.number = numberFormat
}
let additionalOptions
if (timezone) {
// https://github.com/formatjs/formatjs/tree/master/packages/intl-messageformat#formatters
// customize formatters to be able to render dates to a specific timezone
additionalOptions = {
formatters: {
getNumberFormat: memoizeIntlConstructor(Intl.NumberFormat),
getPluralRules: memoizeIntlConstructor(Intl.PluralRules),
getDateTimeFormat (locales, intlOptions) {
return {
format (date) {
if (!(date instanceof Date)) throw new Error('Invalid valid date passed to format')
const zone = momentTimezone.tz.zone(timezone)
if (!zone) throw new Error('Invalid timezone')
const clonedDate = new Date(date.getTime())
const fromOffset = clonedDate.getTimezoneOffset()
const toOffset = zone.parse(clonedDate)
const newDate = new Date(clonedDate.getTime() - (toOffset - fromOffset) * 60 * 1000)
return new Intl.DateTimeFormat(locales, intlOptions).format(newDate)
console.log(
'Done in',
(minutes ? `${minutes}m ` : '') +
(seconds ? `${seconds}s` : '') +
(!minutes && !seconds ? 'a moment' : '')
);
});
}
/**
* Intl message formatter.
*
* @param {string} format Format string.
* @param {string} lang Language.
*/
export const getMessageFormat = createFormatCache(IntlMessageFormat);
/**
* Intl date/time formatter.
*
* @param {string} lang Language.
* @param {Object} format Format.
*/
export const getDateTimeFormat = createFormatCache(DateTimeFormat);
constructor() {
super(...arguments);
this.formatter = createFormatCache(IntlRelativeFormat);
}
opts[name] = defaults[name];
}
return opts;
}, {});
}
},
propTypes : typesSpec,
contextTypes : typesSpec,
childContextTypes: typesSpec,
getNumberFormat : createFormatCache(Intl.NumberFormat),
getDateTimeFormat: createFormatCache(Intl.DateTimeFormat),
getMessageFormat : createFormatCache(IntlMessageFormat),
getRelativeFormat: createFormatCache(IntlRelativeFormat),
getChildContext: function () {
var context = this.context;
var props = this.props;
return {
locales: props.locales || context.locales,
formats: props.formats || context.formats,
messages: props.messages || context.messages
};
},
formatDate: function (date, options) {
date = new Date(date);
assertIsDate(date, 'A date or timestamp must be provided to formatDate()');
return this._format('date', date, options);
export function createFormatters(
cache: IntlCache = createIntlCache()
): Formatters {
const RelativeTimeFormat = (Intl as any).RelativeTimeFormat;
const ListFormat = (Intl as any).ListFormat;
return {
getDateTimeFormat: memoizeIntlConstructor(
Intl.DateTimeFormat,
cache.dateTime
),
getNumberFormat: memoizeIntlConstructor(Intl.NumberFormat, cache.number),
getMessageFormat: memoizeIntlConstructor(IntlMessageFormat, cache.message),
getRelativeTimeFormat: memoizeIntlConstructor(
RelativeTimeFormat,
cache.relativeTime
),
getPluralRules: memoizeIntlConstructor(Intl.PluralRules, cache.pluralRules),
getListFormat: memoizeIntlConstructor(ListFormat, cache.list),
};
}