javascript相当于PHP drupal format_interval吗?


8

我想与JavaScript和Drupal.t()等效使用format_interval()

对于PHP,我将使用以下代码。

print t("!date ago", array("!date" => format_interval(time() - $lastActivity, 1)));

JavaScript中的等效内容是什么?


t方法是Drupal文本的消毒和翻译,等效t()于Drupal核心的PHP函数。
AyeshK

Answers:


4

Drupal没有实现的JS版本format_interval();这是一个粗糙(经过最低测试)的端口:

Drupal.formatInterval = function(interval, granularity, langcode) {
  granularity = typeof granularity !== 'undefined' ? granularity : 2;
  langcode = typeof langcode !== 'undefined' ? langcode : null;

  var units = {
    '1 year|@count years': 31536000,
    '1 month|@count months': 2592000,
    '1 week|@count weeks': 604800,
    '1 day|@count days': 86400,
    '1 hour|@count hours': 3600,
    '1 min|@count min': 60,
    '1 sec|@count sec': 1
  },
  output = '';

  for (var key in units) {
    var keys = key.split('|'); 
    var value = units[key];
    if (interval >= value) {
      output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), keys[0], keys[1], {}, { langcode: langcode });
      interval %= value;
      granularity--;
    }

    if (granularity == 0) {
      break;
    }
  }

  return output.length ? output : Drupal.t('0 sec', {}, { langcode: langcode });
}

使用上面的一些随机结果(它们似乎与预期的PHP函数匹配):

  • 3643 => 1小时43秒
  • 92900 => 1天1小时
  • 2592000 => 1个月
  • 9331200 => 3个月2星期
  • 297605232 => 9年5个月

1

Clives的执行情况很好。但是,Drupals javascript聚合器需要解析所有javascript文件中的可翻译字符串。由于Clive为Drupal.formatPlural使用动态值,因此在这里不起作用。

所以这是工作翻译的另一个实现:

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');
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.