Skip to content

Commit

Permalink
Merge pull request #3 from unRob/master
Browse files Browse the repository at this point in the history
Added basic i18n support
  • Loading branch information
poshboytl committed Aug 7, 2012
2 parents c01a00a + 3312fdb commit 1db3988
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 38 deletions.
26 changes: 26 additions & 0 deletions lib/timeago.en.js
@@ -0,0 +1,26 @@
(function() {

$.fn.timeago.defaults.lang = {
units: {
second: "second",
seconds: "seconds",
minute: "minute",
minutes: "minutes",
hour: "hour",
hours: "hours",
day: "day",
days: "days",
month: "month",
months: "months",
year: "year",
years: "years"
},
prefixes: {
lt: "less than a",
about: "about",
over: "over",
almost: "almost"
}
};

}).call(this);
26 changes: 26 additions & 0 deletions lib/timeago.es.js
@@ -0,0 +1,26 @@
(function() {

$.fn.timeago.defaults.lang = {
units: {
second: "segundo",
seconds: "segundos",
minute: "minuto",
minutes: "minutos",
hour: "hora",
hours: "horas",
day: "día",
days: "días",
month: "mes",
months: "meses",
year: "año",
years: "años"
},
prefixes: {
lt: "hace menos de un",
about: "cerca de",
over: "poco más de",
almost: "casi"
}
};

}).call(this);
26 changes: 26 additions & 0 deletions lib/timeago.fr.js
@@ -0,0 +1,26 @@
(function() {

$.fn.timeago.defaults.lang = {
units: {
second: "seconde",
seconds: "secondes",
minute: "minute",
minutes: "minutes",
hour: "heure",
hours: "heures",
day: "jour",
days: "jours",
month: "mois",
months: "mois",
year: "an",
years: "ans"
},
prefixes: {
lt: "moins d'un",
about: "environ",
over: "plus de ",
almost: "presque"
}
};

}).call(this);
63 changes: 39 additions & 24 deletions lib/timeago.js
@@ -1,4 +1,3 @@
// Generated by CoffeeScript 1.3.3
(function() {
var TimeAgo;

Expand Down Expand Up @@ -101,31 +100,31 @@
var dim;
dim = this.getTimeDistanceInMinutes(absolutTime);
if (dim === 0) {
return "less than a minute";
return "" + this.options.lang.prefixes.lt + " " + this.options.lang.units.minute;
} else if (dim === 1) {
return "1 minute";
return "1 " + this.options.lang.units.minute;
} else if (dim >= 2 && dim <= 44) {
return "" + dim + " minutes";
return "" + dim + " " + this.options.lang.units.minutes;
} else if (dim >= 45 && dim <= 89) {
return "about 1 hour";
return "" + this.options.lang.prefixes.about + " 1 " + this.options.lang.units.hour;
} else if (dim >= 90 && dim <= 1439) {
return "about " + (Math.round(dim / 60)) + " hours";
return "" + this.options.lang.prefixes.about + " " + (Math.round(dim / 60)) + " " + this.options.lang.units.hours;
} else if (dim >= 1440 && dim <= 2519) {
return "1 day";
return "1 " + this.options.lang.units.day;
} else if (dim >= 2520 && dim <= 43199) {
return "" + (Math.round(dim / 1440)) + " days";
return "" + (Math.round(dim / 1440)) + " " + this.options.lang.units.days;
} else if (dim >= 43200 && dim <= 86399) {
return "about 1 month";
return "" + this.options.lang.prefixes.about + " 1 " + this.options.lang.units.month;
} else if (dim >= 86400 && dim <= 525599) {
return "" + (Math.round(dim / 43200)) + " months";
return "" + (Math.round(dim / 43200)) + " " + this.options.lang.units.months;
} else if (dim >= 525600 && dim <= 655199) {
return "about 1 year";
return "" + this.options.lang.prefixes.about + " 1 " + this.options.lang.units.year;
} else if (dim >= 655200 && dim <= 914399) {
return "over 1 year";
return "" + this.options.lang.prefixes.over + " 1 " + this.options.lang.units.year;
} else if (dim >= 914400 && dim <= 1051199) {
return "almost 2 years";
return "" + this.options.lang.prefixes.almost + " 2 " + this.options.lang.units.years;
} else {
return "about " + (Math.round(dim / 525600)) + " years";
return "" + this.options.lang.prefixes.about + " " + (Math.round(dim / 525600)) + " " + this.options.lang.units.years;
}
};

Expand All @@ -134,19 +133,13 @@
})();

$.fn.timeago = function(options) {
if (options == null) {
options = {};
}
if (options == null) options = {};
return this.each(function() {
var $this, data;
$this = $(this);
data = $this.data("timeago");
if (!data) {
$this.data("timeago", new TimeAgo(this, options));
}
if (typeof options === 'string') {
return data[options]();
}
if (!data) $this.data("timeago", new TimeAgo(this, options));
if (typeof options === 'string') return data[options]();
});
};

Expand All @@ -160,7 +153,29 @@
selector: 'time.timeago',
attr: 'datetime',
dir: 'up',
suffix: 'ago'
suffix: 'ago',
lang: {
units: {
second: "second",
seconds: "seconds",
minute: "minute",
minutes: "minutes",
hour: "hour",
hours: "hours",
day: "day",
days: "days",
month: "month",
months: "months",
year: "year",
years: "years"
},
prefixes: {
lt: "less than a",
about: "about",
over: "over",
almost: "almost"
}
}
};

}).call(this);
49 changes: 35 additions & 14 deletions src/timeago.coffee
Expand Up @@ -6,10 +6,11 @@
# https://github.com/pragmaticly/smart-time-ago/blob/master/LICENSE

class TimeAgo

constructor: (element, options) ->
@startInterval = 60000
@init(element, options)

init: (element, options) ->
@$element = $(element)
@options = $.extend({}, $.fn.timeago.defaults, options)
Expand Down Expand Up @@ -83,31 +84,31 @@ class TimeAgo
dim = @getTimeDistanceInMinutes(absolutTime) #distance in minutes

if dim == 0
"less than a minute"
"#{ @options.lang.prefixes.lt } #{ @options.lang.units.minute }"
else if dim == 1
"1 minute"
"1 #{ @options.lang.units.minute }"
else if dim >= 2 and dim <= 44
"#{ dim } minutes"
"#{ dim } #{ @options.lang.units.minutes }"
else if dim >= 45 and dim <= 89
"about 1 hour"
"#{ @options.lang.prefixes.about } 1 #{ @options.lang.units.hour }"
else if dim >= 90 and dim <= 1439
"about #{ Math.round(dim / 60) } hours"
"#{ @options.lang.prefixes.about } #{ Math.round(dim / 60) } #{ @options.lang.units.hours }"
else if dim >= 1440 and dim <= 2519
"1 day"
"1 #{ @options.lang.units.day }"
else if dim >= 2520 and dim <= 43199
"#{ Math.round(dim / 1440) } days"
"#{ Math.round(dim / 1440) } #{ @options.lang.units.days }"
else if dim >= 43200 and dim <= 86399
"about 1 month"
"#{ @options.lang.prefixes.about } 1 #{ @options.lang.units.month }"
else if dim >= 86400 and dim <= 525599 #1 yr
"#{ Math.round(dim / 43200) } months"
"#{ Math.round(dim / 43200) } #{ @options.lang.units.months }"
else if dim >= 525600 and dim <= 655199 #1 yr, 3 months
"about 1 year"
"#{ @options.lang.prefixes.about } 1 #{ @options.lang.units.year }"
else if dim >= 655200 and dim <= 914399 #1 yr, 9 months
"over 1 year"
"#{ @options.lang.prefixes.over } 1 #{ @options.lang.units.year }"
else if dim >= 914400 and dim <= 1051199 #2 yr minus half minute
"almost 2 years"
"#{ @options.lang.prefixes.almost } 2 #{ @options.lang.units.years }"
else
"about #{ Math.round(dim / 525600) } years"
"#{ @options.lang.prefixes.about } #{ Math.round(dim / 525600) } #{ @options.lang.units.years }"

$.fn.timeago = (options = {}) ->
@each ->
Expand All @@ -128,3 +129,23 @@ $.fn.timeago.defaults =
attr: 'datetime'
dir: 'up'
suffix: 'ago'
lang:
units:
second: "second"
seconds: "seconds"
minute: "minute"
minutes: "minutes"
hour: "hour"
hours: "hours"
day: "day"
days: "days"
month: "month"
months: "months"
year: "year"
years: "years"
prefixes:
lt: "less than a"
about: "about"
over: "over"
almost: "almost"

19 changes: 19 additions & 0 deletions src/timeago.en.coffee
@@ -0,0 +1,19 @@
$.fn.timeago.defaults.lang =
units:
second: "second"
seconds: "seconds"
minute: "minute"
minutes: "minutes"
hour: "hour"
hours: "hours"
day: "day"
days: "days"
month: "month"
months: "months"
year: "year"
years: "years"
prefixes:
lt: "less than a"
about: "about"
over: "over"
almost: "almost"
20 changes: 20 additions & 0 deletions src/timeago.es.coffee
@@ -0,0 +1,20 @@
$.fn.timeago.defaults.lang =
units:
second: "segundo"
seconds: "segundos"
minute: "minuto"
minutes: "minutos"
hour: "hora"
hours: "horas"
day: "día"
days: "días"
month: "mes"
months: "meses"
year: "año"
years: "años"
prefixes:
lt: "hace menos de un"
about: "cerca de"
over: "poco más de"
almost: "casi"

19 changes: 19 additions & 0 deletions src/timeago.fr.coffee
@@ -0,0 +1,19 @@
$.fn.timeago.defaults.lang =
units:
second: "seconde"
seconds: "secondes"
minute: "minute"
minutes: "minutes"
hour: "heure"
hours: "heures"
day: "jour"
days: "jours"
month: "mois"
months: "mois"
year: "an"
years: "ans"
prefixes:
lt: "moins d'un"
about: "environ"
over: "plus de "
almost: "presque"
2 changes: 2 additions & 0 deletions test/index.html
Expand Up @@ -9,6 +9,8 @@
<script src="lib/jquery.js" type="text/javascript" charset="utf-8"></script>

<script src="../lib/timeago.js" type="text/javascript" charset="utf-8"></script>
<!-- Optional: for language testing -->
<!--script src="../lib/timeago.es.js" type="text/javascript" charset="utf-8"></script-->

<script src="specs/timeago_spec.js" type="text/javascript" charset="utf-8"></script>
</head>
Expand Down

0 comments on commit 1db3988

Please sign in to comment.