Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

deceze
Copy link

@deceze deceze commented Feb 10, 2011

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".

@ashchan
Copy link

ashchan commented Dec 6, 2011

+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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants