Permalink
Browse files

added multilanguage ability

  • Loading branch information...
1 parent 07e77f5 commit 42d0711c97af1e24a40876b0a0e5733f9107f2c3 @jney committed Sep 13, 2009
Showing with 39 additions and 8 deletions.
  1. +12 −0 jquery.relatize_date.en.js
  2. +27 −8 jquery.relatize_date.js
View
@@ -0,0 +1,12 @@
+if(typeof $relatizeDateTranslation == "undefined")
+ $relatizeDateTranslation = {};
+
+$relatizeDateTranslation.en = [
+ "less than a minute ago",
+ "about a minute ago",
+ "%d minutes ago",
+ "about an hour ago",
+ "about %d hours ago",
+ "1 day ago",
+ "%d days ago"
+];
View
@@ -1,6 +1,24 @@
// All credit goes to Rick Olson.
(function($) {
- $.fn.relatizeDate = function() {
+ $.fn.relatizeDate = function(params) {
+ if(typeof $.relatizeDate.language == "undefined")
+ if(typeof params == "undefined")
+ for(key in $relatizeDateTranslation)
+ var language = key;
+ else if(typeof params.availableLanguages == "object") {
+ var currentLanguage = (typeof navigator.browserLanguage == "undefined") ?
+ navigator.language : navigator.browserLanguage;
+ currentLanguage.toLowerCase().match(/((\w+)-\w+)/);
+ if($relatizeDateTranslation[RegExp.$1])
+ var language = RegExp.$1;
+ else if(RegExp.$1 != RegExp.$2 && $relatizeDateTranslation[RegExp.$2])
+ var language = RegExp.$2;
+ else
+ var language = params.defaultLanguage;
+ } else
+ var language = params.defaultLanguage;
+
+ $.relatizeDate.translation = $relatizeDateTranslation[language];
return $(this).each(function() {
$(this).text($.relatizeDate(this));
});
@@ -64,26 +82,27 @@
*/
distanceOfTimeInWords: function(fromTime, toTime, includeTime) {
var delta = parseInt((toTime.getTime() - fromTime.getTime()) / 1000, 10);
+ var translation = $.relatizeDate.translation;
if (delta < 60) {
- return 'less than a minute ago';
+ return translation[0];
} else if (delta < 120) {
- return 'about a minute ago';
+ return translation[1];
} else if (delta < (45*60)) {
- return (parseInt(delta / 60, 10)).toString() + ' minutes ago';
+ return translation[2].replace("%d", parseInt(delta / 60, 10));
} else if (delta < (120*60)) {
- return 'about an hour ago';
+ return translation[3];
} else if (delta < (24*60*60)) {
- return 'about ' + (parseInt(delta / 3600, 10)).toString() + ' hours ago';
+ return translation[4].replace("%d", parseInt(delta / 3600, 10));
} else if (delta < (48*60*60)) {
- return '1 day ago';
+ return translation[5];
} else {
var days = (parseInt(delta / 86400, 10)).toString();
if (days > 5) {
var fmt = '%B %d, %Y';
if (includeTime) fmt += ' %I:%M %p';
return $.relatizeDate.strftime(fromTime, fmt);
} else
- return days + " days ago";
+ return translation[6].replace("%d", days);
}
}
});

0 comments on commit 42d0711

Please sign in to comment.