Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Improving l10n-ability by adding callbacks for numbers and formatter #34

Open
wants to merge 2 commits into from

2 participants

@deceze

Allowing jQuery.timeago.settings.strings.numbers to be a callback function and adding optional jQuery.timeago.settings.strings.formatter callback, which can be used as a general post processing callback instead of the default $.trim([prefix, words, suffix].join(" ")).

This allows 100% localized Japanese for example:

jQuery.timeago.settings.strings = {
  prefixAgo: "",
  prefixFromNow: "今から",
  suffixAgo: "前",
  suffixFromNow: "後",
  seconds: "ほんの数秒",
  minute: "約一分",
  minutes: "%d分",
  hour: "大体一時間",
  hours: "大体%d時間位",
  day: "一日",
  days: "%d日ほど",
  month: "大体一ヶ月",
  months: "%dヶ月ほど",
  year: "丁度一年",
  years: "%d年",
  numbers: function (num) {
    if (num > 99999) {
      return num;
    }

    num = num.toString();
    var numbers = ["〇", "一", "二", "三", "四", "五", "六", "七", "八", "九"],
      units = ["", "十", "百", "千", "万"],
      str = "";

      for (var i = 0; i < num.length; i++) {
        var scale = num.length - i - 1;
        var digit = num[i];
        if (digit > 0) {
          if (digit > 1 || (scale != 1 && scale != 2)) {
            str += numbers[digit];
          }
          str += units[scale];
        }
      }

    return str || numbers[0];
  },
  formatter: function (prefix, words, suffix) { return [prefix, words, suffix].join(""); }
}

Returns 二十六日ほど前 for "about 26 days ago".

David Zentgraf added some commits
@ashchan

+1 for this, really useful for Japanese and Chinese.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 10, 2011
  1. improving l10n functionality by allowing number callback function and…

    David Zentgraf authored
    … adding formatter callback
  2. Fixing oops

    David Zentgraf authored
This page is out of date. Refresh to see the latest.
Showing with 4 additions and 3 deletions.
  1. +4 −3 jquery.timeago.js
View
7 jquery.timeago.js
@@ -45,7 +45,8 @@
months: "%d months",
year: "about a year",
years: "%d years",
- numbers: []
+ numbers: [],
+ formatter: null
}
},
inWords: function(distanceMillis) {
@@ -68,7 +69,7 @@
function substitute(stringOrFunction, number) {
var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction;
- var value = ($l.numbers && $l.numbers[number]) || number;
+ var value = $.isFunction($l.numbers) ? $l.numbers(number) : (($l.numbers && $l.numbers[number]) || number);
return string.replace(/%d/i, value);
}
@@ -84,7 +85,7 @@
years < 2 && substitute($l.year, 1) ||
substitute($l.years, Math.floor(years));
- return $.trim([prefix, words, suffix].join(" "));
+ return $.isFunction($l.formatter) ? $l.formatter(prefix, words, suffix) : $.trim([prefix, words, suffix].join(" "));
},
parse: function(iso8601) {
var s = $.trim(iso8601);
Something went wrong with that request. Please try again.