-
Notifications
You must be signed in to change notification settings - Fork 603
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
Changes from all commits
1642b91
cf9e57b
856456b
cb79465
d283059
3425b33
d74a1b5
1eed12e
bb48a14
b7463b8
54d55e5
8927271
d31ea90
d92cef3
3e99c5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
``` | ||
|
||
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" | ||
``` | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
@@ -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; | ||
|
@@ -55,6 +57,7 @@ require([ | |
enCurrencies, | ||
enGregorian, | ||
enNumbers, | ||
enDateFields, | ||
likelySubtags, | ||
pluralsData, | ||
timeData, | ||
|
@@ -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(); | ||
|
@@ -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. | ||
|
@@ -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' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need extra space and to replace - document.getElementById( "relative-time").innerText = en.formatRelativeTime( -35, 'second' );
+ document.getElementById( "relative-time" ).innerText = en.formatRelativeTime( -35, "second" ); |
||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" ), | ||
|
@@ -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" ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs EOL (end of line) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
||
<!-- | ||
|
@@ -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 | ||
|
@@ -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": { | ||
|
@@ -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. | ||
|
@@ -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' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
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; |
There was a problem hiding this comment.
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?