Skip to content

Commit 579b5c6

Browse files
committed
Added the ability to pass a function as a locale override [DimaD]
* Good for cyrillic languages, like Russian
1 parent d9e4ff3 commit 579b5c6

File tree

3 files changed

+71
-23
lines changed

3 files changed

+71
-23
lines changed

jquery.timeago.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,17 @@
6363
var days = hours / 24;
6464
var years = days / 365;
6565

66-
var words = seconds < 45 && sprintf($l.seconds, Math.round(seconds)) ||
66+
var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
6767
seconds < 90 && $l.minute ||
68-
minutes < 45 && sprintf($l.minutes, Math.round(minutes)) ||
68+
minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
6969
minutes < 90 && $l.hour ||
70-
hours < 24 && sprintf($l.hours, Math.round(hours)) ||
70+
hours < 24 && substitute($l.hours, Math.round(hours)) ||
7171
hours < 48 && $l.day ||
72-
days < 30 && sprintf($l.days, Math.floor(days)) ||
72+
days < 30 && substitute($l.days, Math.floor(days)) ||
7373
days < 60 && $l.month ||
74-
days < 365 && sprintf($l.months, Math.floor(days / 30)) ||
74+
days < 365 && substitute($l.months, Math.floor(days / 30)) ||
7575
years < 2 && $l.year ||
76-
sprintf($l.years, Math.floor(years));
76+
substitute($l.years, Math.floor(years));
7777

7878
return $.trim([prefix, words, suffix].join(" "));
7979
},
@@ -113,8 +113,8 @@
113113
return (new Date().getTime() - date.getTime());
114114
}
115115

116-
// lame sprintf implementation
117-
function sprintf(string, value) {
116+
function substitute(stringOrFunction, value) {
117+
var string = $.isFunction(stringOrFunction) ? stringOrFunction(value) : stringOrFunction;
118118
return string.replace(/%d/i, value);
119119
}
120120

test.html

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,9 @@
99
<script src="test.js" type="text/javascript"></script>
1010
<script type="text/javascript">
1111
jQuery.timeago.settings.allowFuture = true;
12-
// jQuery.timeago.settings.strings = {
13-
// ago: "geleden",
14-
// fromNow: "vanaf nu",
15-
// seconds: "iets minder dan een minute",
16-
// minute: "ongeveer een minuut",
17-
// minutes: "%d minuten",
18-
// hour: "ongeveer een uur",
19-
// hours: "ongeveer %d uren",
20-
// day: "een dag",
21-
// days: "%d dagen",
22-
// month: "ongeveer een maand",
23-
// months: "%d maanden",
24-
// year: "ongeveer een jaar",
25-
// years: "%d jaar"
26-
// };
12+
//loadPigLatin();
13+
//loadRussian();
14+
//loadYoungOldYears();
2715

2816
jQuery(document).ready(function($) {
2917
// functional tests

test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,63 @@ jQuery(document).ready(function($) {
1414
$('abbr[class*=loaded]').attr("title", iso8601(new Date()));
1515
$('abbr[class*=modified]').attr("title", iso8601(new Date(document.lastModified)));
1616
});
17+
18+
function loadPigLatin() {
19+
jQuery.timeago.settings.strings = {
20+
suffixAgo: "ago-hay",
21+
suffixFromNow: "omNow-fray",
22+
seconds: "ess-lay an-thay a-hay inute-may",
23+
minute: "about-hay a-hay inute-may",
24+
minutes: "%d inutes-may",
25+
hour: "about-hay an-hay hour-hay",
26+
hours: "about-hay %d hours-hay",
27+
day: "a-hay ay-day",
28+
days: "%d ays-day",
29+
month: "about-hay a-hay onth-may",
30+
months: "%d onths-may",
31+
year: "about-hay a-hay ear-yay",
32+
years: "%d years-yay"
33+
};
34+
}
35+
36+
function loadRussian() {
37+
(function() {
38+
function numpf(n, f, s, t) {
39+
// f - 1, 21, 31, ...
40+
// s - 2-4, 22-24, 32-34 ...
41+
// t - 5-20, 25-30, ...
42+
var n10 = n % 10;
43+
if ( (n10 == 1) && ( (n == 1) || (n > 20) ) ) {
44+
return f;
45+
} else if ( (n10 > 1) && (n10 < 5) && ( (n > 20) || (n < 10) ) ) {
46+
return s;
47+
} else {
48+
return t;
49+
}
50+
}
51+
52+
jQuery.timeago.settings.strings = {
53+
prefixAgo: null,
54+
prefixFromNow: "через",
55+
suffixAgo: "назад",
56+
suffixFromNow: null,
57+
seconds: "меньше минуты",
58+
minute: "минуту",
59+
minutes: function(value) { return numpf(value, "%d минута", "%d минуты", "%d минут"); },
60+
hour: "час",
61+
hours: function(value) { return numpf(value, "%d час", "%d часа", "%d часов"); },
62+
day: "день",
63+
days: function(value) { return numpf(value, "%d день", "%d дня", "%d дней"); },
64+
month: "месяц",
65+
months: function(value) { return numpf(value, "%d месяц", "%d месяца", "%d месяцев"); },
66+
year: "год",
67+
years: function(value) { return numpf(value, "%d год", "%d года", "%d лет"); }
68+
};
69+
})();
70+
}
71+
72+
function loadYoungOldYears() {
73+
jQuery.extend(jQuery.timeago.settings.strings, {
74+
years: function(value) { return (value < 21) ? "%d young years" : "%d old years"; }
75+
});
76+
}

0 commit comments

Comments
 (0)