Clives implementation is doing well. However, Drupals javascript aggregator needs to parse all javascript files for translatable strings.
As Clive uses dynamic values for Drupal.formatPlural this won't work here.
So here's another implementation with working translation:
Drupal.formatInterval = function(interval, granularity) {
granularity = typeof granularity !== 'undefined' ? granularity : 2;
output = '';
while (granularity > 0) {
var value = 0;
if (interval >= 31536000) {
value = 31536000;
output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 year', '@count years');
}
else if (interval >= 2592000) {
value = 2592000;
output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 month', '@count months');
}
else if (interval >= 604800) {
value = 604800;
output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 week', '@count weeks');
}
else if (interval >= 86400) {
value = 86400;
output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 day', '@count days');
}
else if (interval >= 3600) {
value = 3600;
output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 hour', '@count hours');
}
else if (interval >= 60) {
value = 60;
output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 min', '@count min');
}
else if (interval >= 1) {
value = 1;
output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 sec', '@count sec');
}
interval %= value;
granularity--;
}
return output.length ? output : Drupal.t('0 sec');
}
t
method is a Drupal text sanitizing and translating equivalent to thet()
PHP function from Drupal core.