Giter Club home page Giter Club logo

intl-relativeformat's Introduction

THIS PACKAGE HAS BEEN DEPRECATED

Migration Guide

This package has deviated from the Intl.RelativeTimeFormat spec rather heavily. Therefore, we've deprecated this package and add `@formatjs/intl-relativetimeformat as the spec-compliant polyfill.

  1. All units (such as day-short) should be migrated similarly to:
new IntlRelativeFormat('en', { units: 'second-short' }).format(
  Date.now() - 1000
);
// will be
new Intl.RelativeTimeFormat('en', { style: 'short' }).format(-1, 'second');

new IntlRelativeFormat('en', { units: 'day-narrow' }).format(
  Date.now() - 48 * 3600 * 1000
);
// will be
new Intl.RelativeTimeFormat('en', { style: 'narrow' }).format(-2, 'day');
  1. style: numeric will become numeric: always per spec (which is also the default)
new IntlRelativeFormat('en', {
  units: 'second-short',
  style: 'numeric'
}).format(Date.now() - 1000);
// will be
new Intl.RelativeTimeFormat('en', { style: 'short' }).format(-1, 'second');
new IntlRelativeFormat('en', { units: 'day-narrow', style: 'numeric' }).format(
  Date.now() - 48 * 3600 * 1000
);
// will be
new Intl.RelativeTimeFormat('en', { style: 'narrow' }).format(-2, 'day');
  1. style: 'best fit' is a little trickier but we have released @formatjs/intl-utils to ease the transition:
new IntlRelativeFormat('en', { style: 'best fit' }).format(Date.now() - 1000);
// will be
import { selectUnit } from '@formatjs/intl-utils';
const diff = selectUnit(Date.now() - 1000);
new Intl.RelativeTimeFormat('en', { numeric: 'auto' }).format(
  diff.value,
  diff.unit
);
new IntlRelativeFormat('en', { style: 'best fit' }).format(
  Date.now() - 48 * 3600 * 1000
);
// will be
import { selectUnit } from '@formatjs/intl-utils';
const diff = selectUnit(Date.now() - 48 * 3600 * 1000);
new Intl.RelativeTimeFormat('en', { numeric: 'auto' }).format(
  diff.value,
  diff.unit
);
  1. If you were using options.now in format, you can use formatjs/intl-utils to transition as well
new IntlRelativeFormat('en', { style: 'best fit' }).format(Date.now() - 1000, {
  now: Date.now() + 1000
});
// will be
import { selectUnit } from '@formatjs/intl-utils';
const diff = selectUnit(Date.now() - 1000, Date.now() + 1000);
new Intl.RelativeTimeFormat('en', { numeric: 'auto' }).format(
  diff.value,
  diff.unit
);
new IntlRelativeFormat('en', { style: 'best fit' }).format(
  Date.now() - 48 * 3600 * 1000,
  { now: Date.now() + 1000 }
);
// will be
import { selectUnit } from '@formatjs/intl-utils';
const diff = selectUnit(Date.now() - 48 * 3600 * 1000, Date.now() + 1000);
new Intl.RelativeTimeFormat('en', { numeric: 'auto' }).format(
  diff.value,
  diff.unit
);

Intl RelativeFormat

Formats JavaScript dates to relative time strings (e.g., "3 hours ago").

npm Version

Overview

Goals

This package aims to provide a way to format different variations of relative time. You can use this package in the browser and on the server via Node.js.

This implementation is very similar to moment.js, in concept, although it provides only formatting features based on the Unicode CLDR locale data, an industry standard that supports more than 200 languages.

How It Works

var rf = new IntlRelativeFormat(locales, [options]);

The locales can either be a single language tag, e.g., "en-US" or an array of them from which the first match will be used. options provides a way to control the output of the formatted relative time string.

var output = rf.format(someDate, [options]);

Common Usage Example

The most common way to use this library is to construct an IntlRelativeFormat instance and reuse it many times for formatting different date values; e.g.:

var rf = new IntlRelativeFormat('en-US');

var posts = [
  {
    id: 1,
    title: 'Some Blog Post',
    date: new Date(1426271670524)
  },
  {
    id: 2,
    title: 'Another Blog Post',
    date: new Date(1426278870524)
  }
];

posts.forEach(function(post) {
  console.log(rf.format(post.date));
});
// => "3 hours ago"
// => "1 hour ago"

Features

  • Style options for "best fit" ("yesterday") and "numeric" ("1 day ago") output based on thresholds.

  • Units options for always rendering in a particular unit; e.g. "30 days ago", instead of "1 month ago".

  • Ability to specify the "now" value from which the relative time is calculated, allowing format().

  • Format output in relative time strings using `Intl.RelativeTimeFormat

  • Optimized for repeated calls to an IntlRelativeFormat instance's format() method.

Usage

Intl Dependency

This package assumes the following capabilities from Intl:

  1. Intl.PluralRules
  2. Intl.RelativeTimeFormat

If your environment does not support those, feel free to grab polyfills:

  1. https://www.npmjs.com/package/intl-pluralrules
  2. https://www.npmjs.com/package/@formatjs/intl-relativetimeformat

Loading IntlRelativeFormat in Node.js

Install package and polyfill:

npm install intl-relativeformat --save

Simply require() this package:

var IntlRelativeFormat = require('intl-relativeformat');
var rf = new IntlRelativeFormat('en');
var output = rf.format(dateValue);

Bundling IntlRelativeFormat with Browserify/Webpack/Rollup

Install package:

npm install intl-relativeformat --save

Simply require() this package and the specific locales you wish to support in the bundle:

var IntlRelativeFormat = require('intl-relativeformat');

Note: in Node.js, the data for all 200+ languages is loaded along with the library, but when bundling it with Browserify/Webpack, the data is intentionally ignored (see package.json for more details) to avoid blowing up the size of the bundle with data that you might not need.

Public API

IntlRelativeFormat Constructor

To format a date to relative time, use the IntlRelativeFormat constructor. The constructor takes two parameters:

  • locales - {String | String[]} - A string with a BCP 47 language tag, or an array of such strings. If you do not provide a locale, the default locale will be used. When an array of locales is provided, each item and its ancestor locales are checked and the first one with registered locale data is returned. See: Locale Resolution for more details.

  • [options] - {Object} - Optional object with user defined options for format styles. See: Custom Options for more details.

Note: The rf instance should be enough for your entire application, unless you want to use custom options.

Locale Resolution

IntlRelativeFormat uses a locale resolution process similar to that of the built-in Intl APIs to determine which locale data to use based on the locales value passed to the constructor. The result of this resolution process can be determined by call the resolvedOptions() prototype method.

The following are the abstract steps IntlRelativeFormat goes through to resolve the locale value:

  • If no extra locale data is loaded, the locale will always resolved to "en".

  • If locale data is missing for a leaf locale like "fr-FR", but there is data for one of its ancestors, "fr" in this case, then its ancestor will be used.

  • If there's data for the specified locale, then that locale will be resolved; i.e.,

    var rf = new IntlRelativeFormat('en-US');
    assert(rf.resolvedOptions().locale === 'en-US'); // true
  • The resolved locales are now normalized; e.g., "en-us" will resolve to: "en-US".

Note: When an array is provided for locales, the above steps happen for each item in that array until a match is found.

Custom Options

The optional second argument options provides a way to customize how the relative time will be formatted.

Units

By default, the relative time is computed to the best fit unit, but you can explicitly call it to force units to be displayed in "second", "second-short", "second-narrow", "minute", "minute-short", "minute-narrow", "hour", "hour-short", "hour-narrow", "day", "day-short", "day-narrow", "month", "month-short", "month-narrow", "year", "year-short" or "year-narrow":

var rf = new IntlRelativeFormat('en', {
  units: 'day'
});
var output = rf.format(dateValue);

As a result, the output will be "70 days ago" instead of "2 months ago".

Style

By default, the relative time is computed as "best fit", which means that instead of "1 day ago", it will display "yesterday", or "in 1 year" will be "next year", etc. But you can force to always use the "numeric" alternative:

var rf = new IntlRelativeFormat('en', {
  style: 'numeric'
});
var output = rf.format(dateValue);

As a result, the output will be "1 day ago" instead of "yesterday".

resolvedOptions() Method

This method returns an object with the options values that were resolved during instance creation. It currently only contains a locale property; here's an example:

var rf = new IntlRelativeFormat('en-us');
console.log(rf.resolvedOptions().locale); // => "en-US"

Notice how the specified locale was the all lower-case value: "en-us", but it was resolved and normalized to: "en-US".

format(date, [options]) Method

The format method (which takes a JavaScript date or timestamp) and optional options arguments will compare the date with "now" (or options.now), and returns the formatted string; e.g., "3 hours ago" in the corresponding locale passed into the constructor.

var output = rf.format(new Date());
console.log(output); // => "now"

If you wish to specify a "now" value, it can be provided via options.now and will be used instead of querying Date.now() to get the current "now" value.

License

This software is free to use under the Yahoo! Inc. BSD license. See the LICENSE file for license text and copyright information.

intl-relativeformat's People

Contributors

atlanteh avatar caridy avatar dtm5011 avatar ericf avatar jasonmit avatar jbaudanza avatar juandopazo avatar longlho avatar moox avatar okuryu avatar pawsong avatar redonkulus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

intl-relativeformat's Issues

Missing source map files in "lib" folder

The current version in npm does not contains source map files inside the lib folder. Unfortunately these are referenced in the js-files in this folder. This currently breaks building the library with jspm.

array.indexOf()

This module uses FIELDS.indexOf() in multiple cases. We should probably shim it somehow or it won't work in ES3 browsers.

Updating translations?

Where does the locale-data folder content come from? Are those part of an exernal library? I've found a "wrong" translation in the Italian language.

Rounding of in the diff is causing a half a day loss

Hi,

We are using react-intl intensively for internationalization. React-intl is internally using intl-relativeformat for calculating 'today' and 'yesterday'.

Intl-relativeformate incorrectly shows 'today' even if the datetime send to it was from yesterday for the following times.

Time Sent Current Time Relative
2017-09-06T18:29:30.314143+00:00 2017-09-067T04:29:30.314143+00:00 Today
2017-09-06T18:00:30.314143+00:00 2017-09-067T04:09:30.314143+00:00 Today

The code that I am talking about is:

file name : intl-relativeformat/src/diff.js
------------
 var millisecond = round(to - from),
        second      = round(millisecond / 1000),
        minute      = round(second / 60),
        hour        = round(minute / 60),
        day         = round(hour / 24),
        week        = round(day / 7);
---------------

The issue can be easily solved by replacing the code with the below code

var millisecond = to - from,
        second      = millisecond / 1000,
        minute      = second / 60,
        hour        = minute / 60,
        day         = round(hour / 24),
        week        = round(day / 7);

Regards
Arsh

How do I load locale data when using Webpack

Hi, I'm trying to figure out how to load locale data in an application using Webpack. The application is based on Este.js.

The documentation says that all locales are automatically loaded in Node.js (which I can see when the dev server starts). The other way described is a normal script tag for the browser. However, my application is compiled into a single JS file by Webpack and I just can't figure out where and how do I include a specific locale data.

Question -How to change the locale in IntlRelativeFormat ?

Hi,
I did the following
const IntlRelativeFormat = require('intl-relativeformat');
require('../../localedata/relative-format/de.js');
let rf = new IntlRelativeFormat('de');
rf.format(value);

But I always get the date value in english locale. Even the resolved options method returns english locale.
Can someone help me to know what is done wrongly here?

Regards
Santhosh

Align with ECMA402 spec proposal

Great to see this polyfill!

We reached stage 3 with the spec proposal - https://github.com/tc39/proposal-intl-relative-time - and are looking for implementers feedback.

I just landed a native implementation in SpiderMonkey, but that's going to be behind the flag for a while, and it would be good to have more more implementations provide feedback on the current language of the spec.

Also, it would be very helpful to get people working on this polyfill to contribute to the open issues - https://github.com/tc39/proposal-intl-relative-time/issues

Thanks!

Question around registering non-default locales

Hi there, just a question I'm hoping you can help me with in order to understand the code. I currently have an issue that french translations are only working server side and not client side.

I am loading all the locale data on the client and (via react-intl) this is working correctly for all other types of intl (number, message etc.) It falls down with relative and I've been trying to work my way through the code to understand where the problem is and what I might be doing incorrectly.

Here I see you're adding all localeData on the server hence why it works fine https://github.com/yahoo/intl-relativeformat/blob/master/index.js#L9

This is skipped on the client and addLocaleData is only called with the defaultLocale which you export from 'en' https://github.com/yahoo/intl-relativeformat/blob/master/src/main.js#L6. If I check RelativeFormat.__localeData__ I see that en is the only one registered.

And then, in 'react-intl', the mixin exports getRelativeFormat: intl$format$cache$$["default"](intl$relativeformat$$["default"]). Where default is already specified as en.

My question is at what point does the localeData that is registered on the window.Intl instance get copied over to the IntlRelativeFormat module? I feel like this is the part I need to troubleshoot to see where I'm going wrong and to understand it.

Thanks in advance for any help you can give me.

Not able to load IntlRelativeFormat with only a couple locales with module bundler

Hi, I would like to bundle my code using a module bundler but with only a few locales loaded instead of the all 200+. You currently recommend to use the script tag to import a specific locale but it is not compatible with the module bundler way of doing.
For the moment I am running require('intl-relativeformat/lib/locales.js') that works fine except that we cannot restrain the number of locales.

Would you have any recommendations ? Loading the locale-data files does not work because the module bundler executes the code within a specific context where IntlRelativeFormat is in.

Thank you

Feature request: Allow a custom value for now()

Here are some possible solutions:

  1. Change the signature of format to be format(date, now). now would be optional.
  2. Add a now option to the constructor. This could be either a Date/number constant or a function reference.

My motivation for this request is to use intl-relativeformat from within a React component. I can use this from within React now, but idiomatic React components are supposed to be pure functions of this.props and this.state. Having my render function call Date.now is somewhat of an antipattern.

Let me know what you think. I'd be happy to put together a PR.

Question - Intl Dependency

hi, you mention that you have an Intl dependency, but where is that and why ? if you already have the metadata in your library.

thanks in advance.

The original set of `locales` is not being passed through to the internal IntlMessageFormat instances

I noticed a sneaky bug — because I use Safari which requires the Intl.js Polyfill — where numbers within relative formatted strings aren't being formatted with the exact locale the IntlRelativeFormat instance was created with.

An example is when using the Intl.js Polyfill and loading the fr-FR data, and creating an IntlRelativeFormat('fr-FR'). This ends up creating an internal IntlMessageFormat instance with only the root locale; i.e., IntlMessageFormat('fr'). Which then propagates that to creating Intl.NumberFormat('fr'), for which there's no data on the page and it falls back to English.

A change is required to propagate the original locales value the use specified when creating the IntlRelativeFormat instance all the way through the stack of format instances.

Narrow/short versions

Hi!

Is is possible to use the narrow/short versions for relative times? E.g. {0} wk. ago instead of {0} week ago? They seem to be available on CLDR.

I would love short version to be {0}w ago, but you can't have it all I guess ;).

Non space option

If it returns "3 day ago" in English, it returns "3 日前" in Japanese. There are times when the space between "3" and "日前" is not required in Japanese.

This is an use case in Japanese (Chinese maybe has similar situation), so I wish any options for this use case.

FYI, moment.js seems to has customizable relative time format.
http://momentjs.com/docs/#/i18n/changing-locale/

Thought?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.