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

specify locale when calling timeago #192

Open
devth opened this issue Jul 25, 2014 · 2 comments
Open

specify locale when calling timeago #192

devth opened this issue Jul 25, 2014 · 2 comments

Comments

@devth
Copy link

devth commented Jul 25, 2014

Sometimes I like to use en and others I need en-short. Is there a way to specify this when I'm calling timeago? e.g.

var locales = {short: {...}, long: {....}}; // assuming these are proper jquery.timeago locales
$('time.short').timeago({locale: locales.short});
$('time.long').timeago({locale: locales.long});
@yairEO
Copy link

yairEO commented Dec 4, 2014

yes!!!! this is so important!!!

@yairEO
Copy link

yairEO commented Dec 4, 2014

managed to add support for this. Below is the beginning of the code and you can see all place where I used the variable locale and do the same in your plugin: (just pass a settings object like so {locale:'en-short'

(function (factory) {
  if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define(['jquery'], factory);
  } else {
    // Browser globals
    factory(jQuery);
  }
}(function ($) {
  var locale = 'en'; // default locale

  $.timeago = function(timestamp, settings) {
    if( settings && settings.locale && $.timeago.settings.locales[settings.locale] )
        locale = settings.locale;

    if (timestamp instanceof Date) {
      return inWords(timestamp, settings);
    } else if (typeof timestamp === "string") {
      return inWords($.timeago.parse(timestamp), settings);
    } else if (typeof timestamp === "number") {
      return inWords(new Date(timestamp), settings);
    } else {
      return inWords($.timeago.datetime(timestamp), settings);
    }
  };
  var $t = $.timeago;

  $.extend($.timeago, {

    settings: {
      refreshMillis: 60000,
      allowPast: true,
      allowFuture: false,
      localeTitle: false,
      cutoff: 0,
      locales: {
        'en' : {
          prefixAgo     : null,
          prefixFromNow : null,
          suffixAgo     : "ago",
          suffixFromNow : "from now",
          inPast        : 'any moment now',
          seconds       : "less than a minute",
          minute        : "about a minute",
          minutes       : "%d minutes",
          hour          : "about an hour",
          hours         : "about %d hours",
          day           : "a day",
          days          : "%d days",
          month         : "about a month",
          months        : "%d months",
          year          : "about a year",
          years         : "%d years",
          wordSeparator : " ",
          numbers       : []
        },
        'short-en' : {
          prefixAgo     : null,
          prefixFromNow : null,
          suffixAgo     : "",
          suffixFromNow : "",
          seconds       : "1m",
          minute        : "1m",
          minutes       : "%dm",
          hour          : "1h",
          hours         : "%dh",
          day           : "1d",
          days          : "%dd",
          month         : "1mo",
          months        : "%dmo",
          year          : "1yr",
          years         : "%dyr",
          wordSeparator : " ",
          numbers       : []
          }
      }
    },

    inWords: function(distanceMillis) {
      if(!this.settings.allowPast && ! this.settings.allowFuture) {
          throw 'timeago allowPast and allowFuture settings can not both be set to false.';
      }

      var $l = this.settings.locales[locale];
      var prefix = $l.prefixAgo;
      var suffix = $l.suffixAgo;
      if (this.settings.allowFuture) {
        if (distanceMillis < 0) {
          prefix = $l.prefixFromNow;
          suffix = $l.suffixFromNow;
        }
      }

      if(!this.settings.allowPast && distanceMillis >= 0) {
        return this.settings.locales[locale].inPast;
      }
   ....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants