Added support for using words instead of figures for a range of numbers

* The intention is to give the ability to support words instead of figures for numbers up to ten.
* Credit goes to Nick Zadrozny (nzadrozny) for the idea
This commit is contained in:
Ryan McGeary 2010-02-28 14:24:23 -05:00
parent 2ee76c6d0a
commit 3109ef5d86
3 changed files with 29 additions and 6 deletions

View file

@ -40,7 +40,8 @@
month: "about a month", month: "about a month",
months: "%d months", months: "%d months",
year: "about a year", year: "about a year",
years: "%d years" years: "%d years",
numbers: []
} }
}, },
inWords: function(distanceMillis) { inWords: function(distanceMillis) {
@ -61,6 +62,12 @@
var days = hours / 24; var days = hours / 24;
var years = days / 365; var years = days / 365;
function substitute(stringOrFunction, number) {
var string = $.isFunction(stringOrFunction) ? stringOrFunction(number) : stringOrFunction;
var value = ($l.numbers && $l.numbers[number]) || number;
return string.replace(/%d/i, value);
}
var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) || var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
seconds < 90 && substitute($l.minute, 1) || seconds < 90 && substitute($l.minute, 1) ||
minutes < 45 && substitute($l.minutes, Math.round(minutes)) || minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
@ -127,11 +134,6 @@
return (new Date().getTime() - date.getTime()); return (new Date().getTime() - date.getTime());
} }
function substitute(stringOrFunction, value) {
var string = $.isFunction(stringOrFunction) ? stringOrFunction(value) : stringOrFunction;
return string.replace(/%d/i, value);
}
// fix for IE6 suckage // fix for IE6 suckage
document.createElement("abbr"); document.createElement("abbr");
document.createElement("time"); document.createElement("time");

View file

@ -120,6 +120,10 @@
<h2>Settings</h2> <h2>Settings</h2>
<ul> <ul>
<li><abbr id="testNumbersSettings1" class="tonumbers" title="120"></abbr> [2 min]</li>
<li><abbr id="testNumbersSettings2" class="tonumbers" title="540"></abbr> [9 min]</li>
<li><abbr id="testNumbersSettings3" class="tonumbers" title="600"></abbr> [10 min]</li>
<li><abbr id="testYoungOldSettings1" class="toyoungold" title="504576000"></abbr> [5840 days]</li> <li><abbr id="testYoungOldSettings1" class="toyoungold" title="504576000"></abbr> [5840 days]</li>
<li><abbr id="testYoungOldSettings2" class="toyoungold" title="2018304000"></abbr> [23360 days]</li> <li><abbr id="testYoungOldSettings2" class="toyoungold" title="2018304000"></abbr> [23360 days]</li>
@ -206,6 +210,10 @@
$("abbr.towords").each(toWords); $("abbr.towords").each(toWords);
loadNumbers();
$("abbr.tonumbers").each(toWords);
unloadNumbers();
loadYoungOldYears(); loadYoungOldYears();
$("abbr.toyoungold").each(toWords); $("abbr.toyoungold").each(toWords);
@ -466,6 +474,12 @@
ok($("#testRussianSettings21").html().match(/^3 года/), "Settings correctly applied"); ok($("#testRussianSettings21").html().match(/^3 года/), "Settings correctly applied");
// TODO: Test "год" and "лет" // TODO: Test "год" and "лет"
}); });
test("numbers", function () {
ok($("#testNumbersSettings1").html().match(/^two minutes/), "Settings correctly applied");
ok($("#testNumbersSettings2").html().match(/^nine minutes/), "Settings correctly applied");
ok($("#testNumbersSettings3").html().match(/^10 minutes/), "Settings correctly applied");
});
})(jQuery); })(jQuery);
//]]> //]]>
</script> </script>

View file

@ -15,6 +15,13 @@ function prepareDynamicDates() {
$('abbr.modified').attr("title", iso8601(new Date(document.lastModified))); $('abbr.modified').attr("title", iso8601(new Date(document.lastModified)));
} }
function loadNumbers() {
jQuery.timeago.settings.strings.numbers = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
}
function unloadNumbers() {
jQuery.timeago.settings.strings.numbers = [];
}
function loadPigLatin() { function loadPigLatin() {
jQuery.timeago.settings.strings = { jQuery.timeago.settings.strings = {
suffixAgo: "ago-hay", suffixAgo: "ago-hay",