From b30865ba1190ea62979c1c89d533a1e7c6b5cb34 Mon Sep 17 00:00:00 2001 From: Ryan McGeary Date: Tue, 19 Aug 2008 19:40:43 -0400 Subject: [PATCH] Added language (aka i18n, aka locale) support. * Use $.timeago.settings.strings to override the default english verbiage --- jquery.timeago.js | 61 +++++++++++++++++++++++++++++++---------------- test.html | 16 +++++++++++++ 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/jquery.timeago.js b/jquery.timeago.js index b88c41f..e32fd84 100644 --- a/jquery.timeago.js +++ b/jquery.timeago.js @@ -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); diff --git a/test.html b/test.html index a13c8ce..6ca1904 100644 --- a/test.html +++ b/test.html @@ -9,6 +9,22 @@