How to use intl-format-cache - 10 common examples

To help you get started, we’ve selected a few intl-format-cache 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 sebastian-software / vue-locale / src / VueLocale.js View on Github external
// 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
github formatjs / handlebars-intl / src / helpers.js View on Github external
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,
github formatjs / handlebars-intl / src / helpers.js View on Github external
/* 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,
github stelace / stelace / plugins / email / util / i18n.js View on Github external
}

  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)
            }
github stelace / stelace / plugins / email / util / i18n.js View on Github external
}
  }

  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)
github sapegin / fledermaus / src / util.js View on Github external
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);
github ember-intl / ember-intl / addon / -private / formatters / format-relative.js View on Github external
constructor() {
    super(...arguments);
    this.formatter = createFormatCache(IntlRelativeFormat);
  }
github formatjs / react-intl / src / mixin.js View on Github external
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);
github formatjs / react-intl / src / utils.ts View on Github external
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),
  };
}

intl-format-cache

A memoizer factory for Intl format constructors.

BSD-3-Clause
Latest version published 4 years ago

Package Health Score

80 / 100
Full package analysis

Popular intl-format-cache functions