Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added basic i18n support #3

Merged
merged 1 commit into from

2 participants

@unRob

I'm using your code for a spanish-speaking project, so here's some code to do i18n. Hope it helps.

  • Using the default options, we can add units and prefixes
  • Added a spanish set
  • Added a non-native french set that should be corrected by a native speaker
  • I'm not familiar with testing, so I added the file to watch it fail "properly" :)

Great job, by the way!

@unRob unRob Added basic i18n support
Using the default options, we can add units and prefixes
Added a spanish set
Added a non-native french set that should be corrected by a native
speaker
I'm not familiar with testing, so I added the file to watch it fail
"properly" :)
3312fdb
@poshboytl
Owner

Thanks very much for your contribution. Sorry for merging it a little bit late. Cause a littble bit busy last week.
I will add the test if needed and the Chinese version.

Cheers :)

@poshboytl poshboytl merged commit 1db3988 into pragmaticly:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 1, 2012
  1. @unRob

    Added basic i18n support

    unRob authored
    Using the default options, we can add units and prefixes
    Added a spanish set
    Added a non-native french set that should be corrected by a native
    speaker
    I'm not familiar with testing, so I added the file to watch it fail
    "properly" :)
This page is out of date. Refresh to see the latest.
View
26 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);
View
26 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);
View
26 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);
View
63 lib/timeago.js
@@ -1,4 +1,3 @@
-// Generated by CoffeeScript 1.3.3
(function() {
var TimeAgo;
@@ -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;
}
};
@@ -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]();
});
};
@@ -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);
View
49 src/timeago.coffee
@@ -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)
@@ -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 ->
@@ -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"
+
View
19 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"
View
20 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"
+
View
19 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"
View
2  test/index.html
@@ -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>
Something went wrong with that request. Please try again.