How to use intl-relativeformat - 10 common examples

To help you get started, we’ve selected a few intl-relativeformat 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 magicshifter / MS3000-Firmware / React / FirstIteration / src / client / intl / store.js View on Github external
import IntlMessageFormat from 'intl-messageformat';
import IntlRelativeFormat from 'intl-relativeformat';
import {i18nCursor} from '../state';
import {register} from '../dispatcher';
import {List, Map} from 'immutable';

const cachedInstances = Object.create(null);
const intlRelativeFormat = new IntlRelativeFormat;

function getCachedInstanceOf(message) {
  if (message in cachedInstances)
    return cachedInstances[message];

  // TODO: Add locales support.
  cachedInstances[message] = new IntlMessageFormat(message);
  return cachedInstances[message];
}

export function msg(path, values = null): string {
  const pathParts = ['messages'].concat(path.split('.'));
  const message = i18nCursor(pathParts);

  if (message == null) {
    throw new ReferenceError('Could not find Intl message: ' + path);
github este / este / src / common / intl / format.js View on Github external
import IntlMessageFormat from 'intl-messageformat';
import IntlRelativeFormat from 'intl-relativeformat';

// TODO: Memoize all.
const cachedFormatters = Object.create(null);
const intlRelativeFormat = new IntlRelativeFormat;

function getCachedFormatter(message) {
  if (message in cachedFormatters) return cachedFormatters[message];
  cachedFormatters[message] = new IntlMessageFormat(message);
  return cachedFormatters[message];
}

export function format(msg, options = null) {
  if (!options) return msg;
  return getCachedFormatter(msg).format(options);
}

export function dateFormat(date, locales, options) {
  const dateTimeFormat = new Intl.DateTimeFormat(locales, options); // eslint-disable-line no-undef
  return dateTimeFormat.format(date);
}
github hiroppy / fusuma / packages / client / src / components / CommentsList.js View on Github external
import React, { memo, useState, useEffect } from 'react';
import posed, { PoseGroup } from 'react-pose';
import IntlRelativeFormat from 'intl-relativeformat';
import '../../assets/style/commentsList.css';

const Ul = posed.ul({});
const Li = posed.li({
  enter: { opacity: 1 },
  exit: { opacity: 0 }
});
const rf = new IntlRelativeFormat();
let ws;

export const CommentsList = memo(() => {
  const [comments, updateComments] = useState([]);

  useEffect(() => {
    if (!ws) {
      ws = new WebSocket(`ws://${window.location.hostname}:${process.env.SERVER_PORT}`);
      ws.onmessage = ({ data }) => {
        const fetchedComments = JSON.parse(data);

        if (Array.isArray(fetchedComments) && fetchedComments.length !== 0) {
          if (fetchedComments[0].message === 'Rate limit exceeded') {
            // TODO: implement
          } else if (fetchedComments[0].message === 'hello:)') {
            // TODO: implement
github formatjs / react-intl / test / unit / locale-data-registry.js View on Github external
it('delegates to IntlRelativeFormat', () => {
            emptyLocaleData();
            expect(registry.hasLocaleData('en')).toBe(false);

            IntlRelativeFormat.__addLocaleData(IRF_LOCALE_DATA.en);
            expect(registry.hasLocaleData('en')).toBe(true);
        });
github formatjs / react-intl / test / unit / locale-data-registry.js View on Github external
const emptyLocaleData = () => {
        const emptyObject = (obj) => {
            Object.keys(obj).forEach((prop) => delete obj[prop]);
        };

        emptyObject(IntlRelativeFormat.__localeData__);
    };
github formatjs / react-intl / test / unit / locale-data-registry.js View on Github external
const restoreLocaleData = () => {
        emptyLocaleData();
        Object.assign(IntlRelativeFormat.__localeData__, IRF_LOCALE_DATA);
    };
github formatjs / react-intl / test / unit / locale-data-registry.js View on Github external
describe('locale data registry', () => {
    const IRF_LOCALE_DATA = {...IntlRelativeFormat.__localeData__};

    const emptyLocaleData = () => {
        const emptyObject = (obj) => {
            Object.keys(obj).forEach((prop) => delete obj[prop]);
        };

        emptyObject(IntlRelativeFormat.__localeData__);
    };

    const restoreLocaleData = () => {
        emptyLocaleData();
        Object.assign(IntlRelativeFormat.__localeData__, IRF_LOCALE_DATA);
    };

    afterEach(() => {
        restoreLocaleData();
github formatjs / react-intl / src / locale-data-registry.js View on Github external
function hasIMFAndIRFLocaleData(locale) {
  let normalizedLocale = locale && locale.toLowerCase();

  return !!IntlRelativeFormat.__localeData__[normalizedLocale];
}
github bitnami-labs / cabin / src / utils / LocalesUtils.js View on Github external
static getLocalizedRelativeDate(date, options) {
    const lang = this.getLanguage();
    const formater = new IntlRelativeFormat(lang, options);
    return formater.format(new Date(date), options);
  }
github formatjs / react-intl / src / locale-data-registry.js View on Github external
export function addLocaleData(data = []) {
  let locales = Array.isArray(data) ? data : [data];

  IntlRelativeFormat.__addLocaleData(...locales.filter(l => !!l.locale));
}

intl-relativeformat

Formats JavaScript dates to relative time strings.

BSD-3-Clause
Latest version published 5 years ago

Package Health Score

70 / 100
Full package analysis