How to use intl-messageformat - 10 common examples

To help you get started, we’ve selected a few intl-messageformat examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github opencollective / opencollective-api / server / lib / twitter.js View on Github external
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;
};
github format-message / format-message / scripts / benchmark.src.js View on Github external
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') }
github GoogleChrome / lighthouse / lighthouse-core / lib / i18n / i18n.js View on Github external
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};
}
github format-message / format-message / scripts / benchmark.src.js View on Github external
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 () {
github format-message / format-message / scripts / benchmark.src.js View on Github external
}
)
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') }
github i18next / i18next-icu / src / index.js View on Github external
locales.forEach(localeData => {
      if (localeData && localeData.locale) {
        IntlMessageFormat.__addLocaleData(localeData);
        // IntlRelativeFormat.__addLocaleData(localeData);
      }
    });
  }
github JargonInc / jargon-sdk-nodejs / packages / sdk-core / lib / resourceManager / icuFormat.ts View on Github external
locales.forEach((localeData: any) => {
      if (localeData && localeData.locale) {
        // @ts-ignore Inaccurate type information
        IntlMessageFormat.__addLocaleData(localeData)
      }
    })
  }
github formatjs / intl-relativeformat / src / core.js View on Github external
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);
    }
}});
github sebastian-software / vue-locale / src / VueLocale.js View on Github external
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)
github formatjs / react-intl / src / plural.js View on Github external
function resolveLocale(locales) {
  // IntlMessageFormat#_resolveLocale() does not depend on `this`.
  return IntlMessageFormat.prototype._resolveLocale(locales);
}

intl-messageformat

Formats ICU Message strings with number, date, plural, and select placeholders to create localized messages.

BSD-3-Clause
Latest version published 3 months ago

Package Health Score

100 / 100
Full package analysis