Added language (aka i18n, aka locale) support.

* Use $.timeago.settings.strings to override the default english verbiage
This commit is contained in:
Ryan McGeary 2008-08-19 19:40:43 -04:00
parent 72fc2cecdc
commit b30865ba11
2 changed files with 57 additions and 20 deletions

View file

@ -1,5 +1,5 @@
/*
* timeago: a jQuery plugin, version: 0.4 (08/05/2008)
* timeago: a jQuery plugin, version: 0.5 (08/19/2008)
* @requires jQuery v1.2 or later
*
* Timeago is a jQuery plugin that makes it easy to support automatically
@ -19,16 +19,33 @@
else if (typeof timestamp == "string") return inWords($.timeago.parse(timestamp));
else return inWords($.timeago.parse($(timestamp).attr("title")));
};
var $t = $.timeago;
$.extend($.timeago, {
settings: {
refreshMillis: 60000,
allowFuture: false
allowFuture: false,
strings: {
ago: "ago",
fromNow: "from now",
seconds: "less than a minute",
minute: "about a minute",
minutes: "%d minutes",
hour: "about an hour",
hours: "about %d hours",
day: "a day",
days: "%d days",
month: "about a month",
months: "%d months",
year: "about a year",
years: "%d years"
}
},
inWords: function(distanceMillis) {
var suffix = " ago";
var $l = this.settings.strings;
var suffix = $l.ago;
if (this.settings.allowFuture) {
if (distanceMillis < 0) suffix = " from now";
if (distanceMillis < 0) suffix = $l.fromNow;
distanceMillis = Math.abs(distanceMillis);
}
@ -38,19 +55,19 @@
var days = hours / 24;
var years = days / 365;
var words = seconds < 45 && "less than a minute" ||
seconds < 90 && "about a minute" ||
minutes < 45 && Math.round(minutes) + " minutes" ||
minutes < 90 && "about an hour" ||
hours < 24 && "about " + Math.round(hours) + " hours" ||
hours < 48 && "a day" ||
days < 30 && Math.floor(days) + " days" ||
days < 60 && "about a month" ||
days < 365 && Math.floor(days / 30) + " months" ||
years < 2 && "about a year" ||
Math.floor(years) + " years";
var words = seconds < 45 && sprintf($l.seconds, Math.round(seconds)) ||
seconds < 90 && $l.minute ||
minutes < 45 && sprintf($l.minutes, Math.round(minutes)) ||
minutes < 90 && $l.hour ||
hours < 24 && sprintf($l.hours, Math.round(hours)) ||
hours < 48 && $l.day ||
days < 30 && sprintf($l.days, Math.floor(days)) ||
days < 60 && $l.month ||
days < 365 && sprintf($l.months, Math.floor(days / 30)) ||
years < 2 && $l.year ||
sprintf($l.years, Math.floor(years));
return words + suffix;
return words + " " + suffix;
},
parse: function(iso8601) {
var s = $.trim(iso8601);
@ -65,7 +82,7 @@
var self = this;
self.each(refresh);
var $s = $.timeago.settings;
var $s = $t.settings;
if ($s.refreshMillis > 0) {
setInterval(function() { self.each(refresh); }, $s.refreshMillis);
}
@ -73,7 +90,7 @@
};
function refresh() {
var date = $.timeago.parse(this.title);
var date = $t.parse(this.title);
if (!isNaN(date)) {
$(this).text(inWords(date));
}
@ -81,11 +98,15 @@
}
function inWords(date) {
return $.timeago.inWords(distance(date));
return $t.inWords(distance(date));
}
function distance(date) {
return (new Date().getTime() - date.getTime());
}
})(jQuery);
// lame sprintf implementation
function sprintf(string, value) {
return string.replace(/%d/i, value);
}
})(jQuery);

View file

@ -9,6 +9,22 @@
<script src="test.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery.timeago.settings.allowFuture = true;
// jQuery.timeago.settings.strings = {
// ago: "geleden",
// fromNow: "vanaf nu",
// seconds: "iets minder dan een minute",
// minute: "ongeveer een minuut",
// minutes: "%d minuten",
// hour: "ongeveer een uur",
// hours: "ongeveer %d uren",
// day: "een dag",
// days: "%d dagen",
// month: "ongeveer een maand",
// months: "%d maanden",
// year: "ongeveer een jaar",
// years: "%d jaar"
// };
jQuery(document).ready(function($) {
// functional tests
$('abbr[class*=timeago]').timeago();