Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Become a backer! 😃`,
},
};
if (message) {
messages['en-US'][template] = message;
}
if (!messages['en-US'][template]) {
console.error('Invalid tweet template', template);
return;
}
const thankyou = '\n\nThank you! 🙏';
const compiled = new IntlMessageFormat(messages['en-US'][template], 'en-US');
let tweet = compiled.format(data);
if (template === 'monthlyStats') {
// A URL always takes 23 chars (+ space)
if (tweet.length < 280 - 24 - thankyou.length) {
tweet += thankyou;
}
}
return tweet;
};
onComplete: function () {
console.log('Fastest is ' + this.filter('fastest').map('name').join(' or '))
console.log()
}
}
var suite = new Benchmark.Suite(name, suiteOptions)
Object.keys(cases).forEach(function (name) {
suite.add(name, cases[name])
})
suite.run()
}
pattern = 'Simple string with nothing special'
formatMessage(pattern, args, 'en') // prime cache
intlMF = new IntlMF(pattern, 'en').format
mf2 = new MessageFormat2('en').compile(pattern)
mf = new MessageFormat(pattern, 'en').format
BENCHMARK_PARSE && benchmark(
'Parse simple message', {
'intl-messageformat-parser': function () { return intlParse(pattern) },
'messageformat-parser': function () { return parse2(pattern) },
'format-message-parse': function () { return parse(pattern) }
}
)
benchmark(
'Format simple message', {
'intl-messageformat (reuse object)': function () { return intlMF(args) },
'messageformat (reuse object)': function () { return mf2(args) },
'message-format (reuse object)': function () { return mf(args) },
'format-message': function () { return formatMessage(pattern, args, 'en') },
'format-message (inlined)': function () { return formatMessage('Simple string with nothing special', args, 'en') }
function _formatIcuMessage(locale, icuMessageId, icuMessage, values) {
const localeMessages = LOCALES[locale];
const localeMessage = localeMessages[icuMessageId] && localeMessages[icuMessageId].message;
// fallback to the original english message if we couldn't find a message in the specified locale
// better to have an english message than no message at all, in some number cases it won't even matter
const messageForMessageFormat = localeMessage || icuMessage;
// when using accented english, force the use of a different locale for number formatting
const localeForMessageFormat = locale === 'en-XA' ? 'de-DE' : locale;
// pre-process values for the message format like KB and milliseconds
const valuesForMessageFormat = _preprocessMessageValues(icuMessage, values);
const formatter = new MessageFormat(messageForMessageFormat, localeForMessageFormat, formats);
const formattedString = formatter.format(valuesForMessageFormat);
return {formattedString, icuMessage: messageForMessageFormat};
}
other {#th}
} banana today, which makes {
numBananas, plural,
=0 {no bananas}
=1 {a banana}
other {lots of bananas}
} total.`
args = {
date: new Date(),
name: 'Curious George',
gender: 'male',
numBananas: 300,
nth: 3
}
formatMessage(pattern, args, 'en') // prime cache
intlMF = new IntlMF(pattern, 'en').format
mf2 = new MessageFormat2('en').compile(pattern)
mf = new MessageFormat(pattern, 'en').format
BENCHMARK_PARSE && benchmark(
'Parse complex message (no numbers or dates)', {
'intl-messageformat-parser': function () { return intlParse(pattern) },
'messageformat-parser': function () { return parse2(pattern) },
'format-message-parse': function () { return parse(pattern) }
}
)
benchmark(
'Format complex message (no numbers or dates)', {
'intl-messageformat (reuse object)': function () { return intlMF(args) },
'messageformat (reuse object)': function () { return mf2(args) },
'message-format (reuse object)': function () { return mf(args) },
'format-message': function () { return formatMessage(pattern, args) },
'format-message (inlined)': function () {
}
)
benchmark(
'Format simple message', {
'intl-messageformat (reuse object)': function () { return intlMF(args) },
'messageformat (reuse object)': function () { return mf2(args) },
'message-format (reuse object)': function () { return mf(args) },
'format-message': function () { return formatMessage(pattern, args, 'en') },
'format-message (inlined)': function () { return formatMessage('Simple string with nothing special', args, 'en') }
}
)
pattern = 'Simple string with { placeholder }.'
args = { placeholder: 'replaced value' }
formatMessage(pattern, args, 'en') // prime cache
intlMF = new IntlMF(pattern, 'en').format
mf2 = new MessageFormat2('en').compile(pattern)
mf = new MessageFormat(pattern, 'en').format
BENCHMARK_PARSE && benchmark(
'Parse common one arg message', {
'intl-messageformat-parser': function () { return intlParse(pattern) },
'messageformat-parser': function () { return parse2(pattern) },
'format-message-parse': function () { return parse(pattern) }
}
)
benchmark(
'Format common one arg message', {
'intl-messageformat (reuse object)': function () { return intlMF(args) },
'messageformat (reuse object)': function () { return mf2(args) },
'message-format (reuse object)': function () { return mf(args) },
'format-message': function () { return formatMessage(pattern, args, 'en') },
'format-message (inlined)': function () { return formatMessage('Simple string with { placeholder }.', { placeholder: 'replaced value' }, 'en') }
locales.forEach(localeData => {
if (localeData && localeData.locale) {
IntlMessageFormat.__addLocaleData(localeData);
// IntlRelativeFormat.__addLocaleData(localeData);
}
});
}
locales.forEach((localeData: any) => {
if (localeData && localeData.locale) {
// @ts-ignore Inaccurate type information
IntlMessageFormat.__addLocaleData(localeData)
}
})
}
defineProperty(RelativeFormat, '__addLocaleData', {value: function () {
for (var i = 0; i < arguments.length; i++) {
var datum = arguments[i]
if (!(datum && datum.locale)) {
throw new Error(
'Locale data provided to IntlRelativeFormat is missing a ' +
'`locale` property value'
);
}
RelativeFormat.__localeData__[datum.locale.toLowerCase()] = datum;
// Add data to IntlMessageFormat.
IntlMessageFormat.__addLocaleData(datum);
}
}});
import { kebabCase, isPlainObject, isString, isNumber, isDate, each, clamp } from "lodash"
// Be sure to import the Polyfill
// 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)
function resolveLocale(locales) {
// IntlMessageFormat#_resolveLocale() does not depend on `this`.
return IntlMessageFormat.prototype._resolveLocale(locales);
}