Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add relative time formatting #395

Closed
wants to merge 15 commits into from
22 changes: 21 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,25 @@ module.exports = function( grunt ) {
endFile: "src/build/outro.js"
}
}
},
{
name: "globalize.relative-time",
include: [ "relative-time" ],
exclude: [
"cldr",
"cldr/event",
"cldr/supplemental",
"./core",
"./number",
"./plural"
],
create: true,
override: {
wrap: {
startFile: "src/build/intro-relative-time.js",
endFile: "src/build/outro.js"
}
}
}
]
}
Expand Down Expand Up @@ -330,7 +349,8 @@ module.exports = function( grunt ) {
"tmp/globalize/date.min.js": [ "dist/globalize/date.js" ],
"tmp/globalize/number.min.js": [ "dist/globalize/number.js" ],
"tmp/globalize/plural.min.js": [ "dist/globalize/plural.js" ],
"tmp/globalize/message.min.js": [ "dist/globalize/message.js" ]
"tmp/globalize/message.min.js": [ "dist/globalize/message.js" ],
"tmp/globalize/relative-time.min.js": [ "dist/globalize/relative-time.js" ]
}
}
},
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Node.js module.
- [Number module](#number_module)
- [Currency module](#currency_module)
- [Plural module](#plural_module)
- [Relative time module](#relative-time_module)
- more to come...
- [Error reference](#error)
- [Development](#development)
Expand Down Expand Up @@ -103,6 +104,7 @@ information on its usage.
| globalize/message.js | +5.6KB | [Message module](#message_module) provides ICU message format support |
| globalize/number.js | +2.9KB | [Number module](#number_module) provides number formatting and parsing |
| globalize/plural.js | +1.7KB | [Plural module](#plural_module) provides pluralization support |
| globalize/relative-time.js | +0.7KB | [Relative time module](#relative-time_module) provides relative time formatting support |

<a name="browser_support"></a>
### Browser Support
Expand Down Expand Up @@ -172,6 +174,7 @@ requirements. See table below.
| Date module | cldr/main/`locale`/ca-gregorian.json<br>cldr/main/`locale`/timeZoneNames.json<br>cldr/supplemental/timeData.json<br>cldr/supplemental/weekData.json<br>+CLDR JSON files from number module |
| Number module | cldr/main/`locale`/numbers.json<br>cldr/supplemental/numberingSystems.json |
| Plural module | cldr/supplemental/plurals.json |
| Relative time module | cldr/main/`locale`/dateFields.json<br>+CLDR JSON files from number and plural modules |

*(b) How am I supposed to get and load CLDR content?*

Expand Down Expand Up @@ -384,6 +387,26 @@ to you in different flavors):

Alias for `.pluralGenerator()( value )`.

<a name="relative-time_module"></a>
### Relative time module

- **`.relativeTimeFormatter( unit [, options] )`**

Returns a function that formats a relative time according to the given unit, options, and the
default/instance locale.

```javascript
.relativeTimeFormatter( "day" )( 1 ) ➡ "tomorrow"
.relativeTimeFormatter( "month" )( -1 ) ➡ "last month"
.relativeTimeFormatter( "month" )( 3 ) ➡ "in 3 months"
```

[Read more...](doc/api/relative-time/relative-time-formatter.md)

- **`.formatRelativeTime( value, unit [, options] )`**

Alias for `.relativeTimeFormatter( unit, options )( value )`.


<a name="error"></a>
## Error reference
Expand Down Expand Up @@ -477,6 +500,8 @@ to you in different flavors):
│ ├── number/ (number source code)
│ ├── plural.js (plural module)
│ ├── plural/ (plural source code)
│ ├── relative-time.js (relative time module)
│ ├── relative-time/ (relative time source code)
│ └── util/ (basic JavaScript helpers polyfills, eg array.map)
└── test/ (unit and functional test files)
├── fixtures/ (CLDR fixture data)
Expand Down
58 changes: 58 additions & 0 deletions doc/api/relative-time/relative-time-formatter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
## .relativeTimeFormatter( unit[, options ]) ➜ function( value )

Returns a function that formats a relative time according to the given unit, options, and the
default/instance locale.

The returned function is invoked with one argument: the number `value` to
be formatted.

### Parameters

**unit**

String value indicating the unit to be formatted. eg. "day", "week", "month", etc.

**options**

- form: [String] eg. "short" or "narrow". Or falsy for default long form

**value**

The number to be formatted.


### Example

Prior to using any relative time methods, you must load
`cldr/main/{locale}/dateFields.json` and the
CLDR content required by the number and plural modules. Read [CLDR content][] if you need
more information.

[CLDR content]: ../../../README.md#2-cldr-content

You can use the static method `Globalize.relativeTimeFormatter()`, which uses the default
locale.

```javascript
var formatter;

Globalize.locale( "en" );
formatter = Globalize.relativeTimeFormatter( "month" );

formatter( 1 ); // "next month"
formatter( 3 ); // "in 3 months"
formatter( -1 ); // "last month"
formatter( -3 ); // "3 months ago"
```

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, can you insert a new paragraph with "You can use the instance method .relativeTimeFormatter(), which uses the instance locale." and an example?

You can use the instance method `.relativeTimeFormatter()`, which uses the instance locale.

```javascript
var globalize = new Globalize( "en" ),
formatter = globalize.relativeTimeFormatter( "week" );

formatter( 1 ); // "next week"
```



1 change: 1 addition & 0 deletions examples/amd-bower/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ <h2>Demo output</h2>
<li><span id="message-2"></span></li>
<li><span id="message-3"></span></li>
</ul>
<p>Something happened: <span id="relative-time"></span></p>
</div>

<!--
Expand Down
13 changes: 9 additions & 4 deletions examples/amd-bower/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require([
"json!cldr-data/main/en/currencies.json",
"json!cldr-data/main/en/ca-gregorian.json",
"json!cldr-data/main/en/numbers.json",
"json!cldr-data/main/en/dateFields.json",
"json!cldr-data/supplemental/currencyData.json",
"json!cldr-data/supplemental/likelySubtags.json",
"json!cldr-data/supplemental/plurals.json",
Expand All @@ -43,8 +44,9 @@ require([
"globalize/date",
"globalize/message",
"globalize/number",
"globalize/plural"
], function( Globalize, enCurrencies, enGregorian, enNumbers, currencyData, likelySubtags,
"globalize/plural",
"globalize/relative-time"
], function( Globalize, enCurrencies, enGregorian, enNumbers, enDateFields, currencyData, likelySubtags,
pluralsData, timeData, weekData, messages ) {

var en, like, number;
Expand All @@ -55,6 +57,7 @@ require([
enCurrencies,
enGregorian,
enNumbers,
enDateFields,
likelySubtags,
pluralsData,
timeData,
Expand All @@ -68,7 +71,7 @@ require([
// Use Globalize to format dates.
document.getElementById( "date" ).innerHTML = en.formatDate( new Date(), {
datetime: "medium"
})
});

// Use Globalize to format numbers.
number = en.numberFormatter();
Expand All @@ -78,7 +81,7 @@ require([
document.getElementById( "currency" ).innerHTML = en.formatCurrency( 69900, "USD" );

// Use Globalize to get the plural form of a numeric value.
document.getElementById( "plural-number" ).innerHTML = number( 12345.6789 )
document.getElementById( "plural-number" ).innerHTML = number( 12345.6789 );
document.getElementById( "plural-form" ).innerHTML = en.plural( 12345.6789 );

// Use Globalize to format a message with plural inflection.
Expand All @@ -91,4 +94,6 @@ require([
document.getElementById( "requirements" ).style.display = "none";
document.getElementById( "demo" ).style.display = "block";

document.getElementById( "relative-time").innerText = en.formatRelativeTime( -35, 'second' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need extra space and to replace ' for ":

- document.getElementById( "relative-time").innerText = en.formatRelativeTime( -35, 'second' );
+ document.getElementById( "relative-time" ).innerText = en.formatRelativeTime( -35, "second" );


});
3 changes: 3 additions & 0 deletions examples/node-npm/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Globalize.load(
cldrData( "main/en/currencies" ),
cldrData( "main/en/ca-gregorian" ),
cldrData( "main/en/numbers" ),
cldrData( "main/en/dateFields" ),
cldrData( "supplemental/currencyData" ),
cldrData( "supplemental/likelySubtags" ),
cldrData( "supplemental/plurals" ),
Expand Down Expand Up @@ -38,3 +39,5 @@ console.log( like( 0 ) );
console.log( like( 1 ) );
console.log( like( 2 ) );
console.log( like( 3 ) );

console.log( Globalize.formatRelativeTime( -35, "second" ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs EOL (end of line)

22 changes: 21 additions & 1 deletion examples/plain-javascript/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ <h2>Demo output</h2>
<li><span id="message-2"></span></li>
<li><span id="message-3"></span></li>
</ul>
<p>Something happened: <span id="relative-time"></span></p>
</div>

<!--
Expand All @@ -52,6 +53,9 @@ <h2>Demo output</h2>
<script src="../../dist/globalize/date.js"></script>
<script src="../../dist/globalize/currency.js"></script>

<!-- Load after globalize/number.js and globalize/plural.js-->
<script src="../../dist/globalize/relative-time.js"></script>

<script>

// At this point, we have Globalize loaded. But, before we can use it, we
Expand Down Expand Up @@ -114,6 +118,20 @@ <h2>Demo output</h2>
"medium": "{1}, {0}"
}
}
},
"fields": {
"second": {
"displayName": "Second",
"relative-type-0": "now",
"relativeTime-type-future": {
"relativeTimePattern-count-one": "in {0} second",
"relativeTimePattern-count-other": "in {0} seconds"
},
"relativeTime-type-past": {
"relativeTimePattern-count-one": "{0} second ago",
"relativeTimePattern-count-other": "{0} seconds ago"
}
}
}
},
"numbers": {
Expand Down Expand Up @@ -211,7 +229,7 @@ <h2>Demo output</h2>
document.getElementById( "currency" ).innerHTML = en.formatCurrency( 69900, "USD" );

// Use Globalize to get the plural form of a numeric value.
document.getElementById( "plural-number" ).innerHTML = number( 12345.6789 )
document.getElementById( "plural-number" ).innerHTML = number( 12345.6789 );
document.getElementById( "plural-form" ).innerHTML = en.plural( 12345.6789 );

// Use Globalize to format a message with plural inflection.
Expand All @@ -224,6 +242,8 @@ <h2>Demo output</h2>
document.getElementById( "requirements" ).style.display = "none";
document.getElementById( "demo" ).style.display = "block";

document.getElementById( "relative-time").innerText = en.formatRelativeTime( -35, 'second' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- document.getElementById( "relative-time").innerText = en.formatRelativeTime( -35, 'second' );
+ document.getElementById( "relative-time" ).innerText = en.formatRelativeTime( -35, "second" );


</script>

</body>
Expand Down
45 changes: 45 additions & 0 deletions src/build/intro-relative-time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Globalize v@VERSION
*
* http://github.com/jquery/globalize
*
* Copyright 2010, 2014 jQuery Foundation, Inc. and other contributors
* Released under the MIT license
* http://jquery.org/license
*
* Date: @DATE
*/
/*!
* Globalize v@VERSION @DATE Released under the MIT license
* http://git.io/TrdQbw
*/
(function( root, factory ) {

// UMD returnExports
if ( typeof define === "function" && define.amd ) {

// AMD
define([
"cldr",
"../globalize",
"./number",
"./plural",
"cldr/event",
"cldr/supplemental"
], factory );
} else if ( typeof exports === "object" ) {

// Node, CommonJS
module.exports = factory( require( "cldrjs" ), require( "globalize" ) );
} else {

// Extend global
factory( root.Cldr, root.Globalize );
}
}(this, function( Cldr, Globalize ) {

var formatMessage = Globalize._formatMessage,
validateCldr = Globalize._validateCldr,
validateParameterPresence = Globalize._validateParameterPresence,
validateParameterTypeString = Globalize._validateParameterTypeString,
validateParameterTypeNumber = Globalize._validateParameterTypeNumber;
3 changes: 3 additions & 0 deletions src/build/node-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ require( "./globalize/plural" );
// Load after globalize/number
require( "./globalize/currency" );
require( "./globalize/date" );

// Load after globalize/number and globalize/plural
require( "./globalize/relative-time" );
Loading