From 1bb29c81079ea9bc4f9018869d799aa1648b1422 Mon Sep 17 00:00:00 2001 From: Antonin Bourguignon Date: Mon, 25 Jun 2012 17:51:46 +0200 Subject: [PATCH 1/2] [IMP] add a better support for third party applications i18n support if you include timeago.js inside a large application that already handles i18n, it's now made easy to give timeago.js your own translator() function. and if you don't, timeago will continue to behave the same way than before. --- jquery.timeago.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/jquery.timeago.js b/jquery.timeago.js index 14177473..1388ac99 100644 --- a/jquery.timeago.js +++ b/jquery.timeago.js @@ -49,7 +49,8 @@ years: "%d years", wordSeparator: " ", numbers: [] - } + }, + translator: null }, inWords: function(distanceMillis) { var $l = this.settings.strings; @@ -70,8 +71,9 @@ function substitute(stringOrFunction, number) { var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction; - var value = ($l.numbers && $l.numbers[number]) || number; - return string.replace(/%d/i, value); + + // return the proper string and the numeric value that goes in it + return stringAndNumber = {'string': string, 'value': ($l.numbers && $l.numbers[number]) || number}; } var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) || @@ -85,9 +87,18 @@ days < 365 && substitute($l.months, Math.round(days / 30)) || years < 1.5 && substitute($l.year, 1) || substitute($l.years, Math.round(years)); - + + var string = stringAndNumber.string; + var value = stringAndNumber.value; var separator = $l.wordSeparator === undefined ? " " : $l.wordSeparator; - return $.trim([prefix, words, suffix].join(separator)); + + // compose and translate the final string + var fullString = $.trim([prefix, string, suffix].join(separator)); + var translatedString = $t.settings.translator ? + $t.settings.translator(fullString) : + fullString; + + return translatedString.replace(/%d/i, value); }, parse: function(iso8601) { var s = $.trim(iso8601); From 6fc5d8613ba2aabd4f62ea1337f1d6ff9623f88e Mon Sep 17 00:00:00 2001 From: Antonin Bourguignon Date: Mon, 25 Jun 2012 19:24:29 +0200 Subject: [PATCH 2/2] [IMP] continue refactoring timeago.js, following the conversation on this pull request: https://github.com/rmm5t/jquery-timeago/pull/83 - remove bad usage of global variable stringAndNumber - use a consistent name for the substitute() function which was actully not substituing anything anymore --- jquery.timeago.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/jquery.timeago.js b/jquery.timeago.js index 1388ac99..d97bcc14 100644 --- a/jquery.timeago.js +++ b/jquery.timeago.js @@ -69,24 +69,24 @@ var days = hours / 24; var years = days / 365; - function substitute(stringOrFunction, number) { + function convert(stringOrFunction, number) { var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction; // return the proper string and the numeric value that goes in it - return stringAndNumber = {'string': string, 'value': ($l.numbers && $l.numbers[number]) || number}; + return {'string': string, 'value': ($l.numbers && $l.numbers[number]) || number}; } - var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) || - seconds < 90 && substitute($l.minute, 1) || - minutes < 45 && substitute($l.minutes, Math.round(minutes)) || - minutes < 90 && substitute($l.hour, 1) || - hours < 24 && substitute($l.hours, Math.round(hours)) || - hours < 42 && substitute($l.day, 1) || - days < 30 && substitute($l.days, Math.round(days)) || - days < 45 && substitute($l.month, 1) || - days < 365 && substitute($l.months, Math.round(days / 30)) || - years < 1.5 && substitute($l.year, 1) || - substitute($l.years, Math.round(years)); + var stringAndNumber = seconds < 45 && convert($l.seconds, Math.round(seconds)) || + seconds < 90 && convert($l.minute, 1) || + minutes < 45 && convert($l.minutes, Math.round(minutes)) || + minutes < 90 && convert($l.hour, 1) || + hours < 24 && convert($l.hours, Math.round(hours)) || + hours < 42 && convert($l.day, 1) || + days < 30 && convert($l.days, Math.round(days)) || + days < 45 && convert($l.month, 1) || + days < 365 && convert($l.months, Math.round(days / 30)) || + years < 1.5 && convert($l.year, 1) || + convert($l.years, Math.round(years)); var string = stringAndNumber.string; var value = stringAndNumber.value;