Added the ability to pass a function as a locale override [DimaD]

* Good for cyrillic languages, like Russian
This commit is contained in:
Ryan McGeary 2009-02-14 14:09:53 -05:00
parent d9e4ff342c
commit 579b5c6f05
3 changed files with 71 additions and 23 deletions

View file

@ -63,17 +63,17 @@
var days = hours / 24; var days = hours / 24;
var years = days / 365; var years = days / 365;
var words = seconds < 45 && sprintf($l.seconds, Math.round(seconds)) || var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
seconds < 90 && $l.minute || seconds < 90 && $l.minute ||
minutes < 45 && sprintf($l.minutes, Math.round(minutes)) || minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
minutes < 90 && $l.hour || minutes < 90 && $l.hour ||
hours < 24 && sprintf($l.hours, Math.round(hours)) || hours < 24 && substitute($l.hours, Math.round(hours)) ||
hours < 48 && $l.day || hours < 48 && $l.day ||
days < 30 && sprintf($l.days, Math.floor(days)) || days < 30 && substitute($l.days, Math.floor(days)) ||
days < 60 && $l.month || days < 60 && $l.month ||
days < 365 && sprintf($l.months, Math.floor(days / 30)) || days < 365 && substitute($l.months, Math.floor(days / 30)) ||
years < 2 && $l.year || years < 2 && $l.year ||
sprintf($l.years, Math.floor(years)); substitute($l.years, Math.floor(years));
return $.trim([prefix, words, suffix].join(" ")); return $.trim([prefix, words, suffix].join(" "));
}, },
@ -113,8 +113,8 @@
return (new Date().getTime() - date.getTime()); return (new Date().getTime() - date.getTime());
} }
// lame sprintf implementation function substitute(stringOrFunction, value) {
function sprintf(string, value) { var string = $.isFunction(stringOrFunction) ? stringOrFunction(value) : stringOrFunction;
return string.replace(/%d/i, value); return string.replace(/%d/i, value);
} }

View file

@ -9,21 +9,9 @@
<script src="test.js" type="text/javascript"></script> <script src="test.js" type="text/javascript"></script>
<script type="text/javascript"> <script type="text/javascript">
jQuery.timeago.settings.allowFuture = true; jQuery.timeago.settings.allowFuture = true;
// jQuery.timeago.settings.strings = { //loadPigLatin();
// ago: "geleden", //loadRussian();
// fromNow: "vanaf nu", //loadYoungOldYears();
// seconds: "iets minder dan een minute",
// minute: "ongeveer een minuut",
// minutes: "%d minuten",
// hour: "ongeveer een uur",
// hours: "ongeveer %d uren",
// day: "een dag",
// days: "%d dagen",
// month: "ongeveer een maand",
// months: "%d maanden",
// year: "ongeveer een jaar",
// years: "%d jaar"
// };
jQuery(document).ready(function($) { jQuery(document).ready(function($) {
// functional tests // functional tests

60
test.js
View file

@ -14,3 +14,63 @@ jQuery(document).ready(function($) {
$('abbr[class*=loaded]').attr("title", iso8601(new Date())); $('abbr[class*=loaded]').attr("title", iso8601(new Date()));
$('abbr[class*=modified]').attr("title", iso8601(new Date(document.lastModified))); $('abbr[class*=modified]').attr("title", iso8601(new Date(document.lastModified)));
}); });
function loadPigLatin() {
jQuery.timeago.settings.strings = {
suffixAgo: "ago-hay",
suffixFromNow: "omNow-fray",
seconds: "ess-lay an-thay a-hay inute-may",
minute: "about-hay a-hay inute-may",
minutes: "%d inutes-may",
hour: "about-hay an-hay hour-hay",
hours: "about-hay %d hours-hay",
day: "a-hay ay-day",
days: "%d ays-day",
month: "about-hay a-hay onth-may",
months: "%d onths-may",
year: "about-hay a-hay ear-yay",
years: "%d years-yay"
};
}
function loadRussian() {
(function() {
function numpf(n, f, s, t) {
// f - 1, 21, 31, ...
// s - 2-4, 22-24, 32-34 ...
// t - 5-20, 25-30, ...
var n10 = n % 10;
if ( (n10 == 1) && ( (n == 1) || (n > 20) ) ) {
return f;
} else if ( (n10 > 1) && (n10 < 5) && ( (n > 20) || (n < 10) ) ) {
return s;
} else {
return t;
}
}
jQuery.timeago.settings.strings = {
prefixAgo: null,
prefixFromNow: "через",
suffixAgo: "назад",
suffixFromNow: null,
seconds: "меньше минуты",
minute: "минуту",
minutes: function(value) { return numpf(value, "%d минута", "%d минуты", "%d минут"); },
hour: "час",
hours: function(value) { return numpf(value, "%d час", "%d часа", "%d часов"); },
day: "день",
days: function(value) { return numpf(value, "%d день", "%d дня", "%d дней"); },
month: "месяц",
months: function(value) { return numpf(value, "%d месяц", "%d месяца", "%d месяцев"); },
year: "год",
years: function(value) { return numpf(value, "%d год", "%d года", "%d лет"); }
};
})();
}
function loadYoungOldYears() {
jQuery.extend(jQuery.timeago.settings.strings, {
years: function(value) { return (value < 21) ? "%d young years" : "%d old years"; }
});
}