Qual è l'equivalente di php drupal format_interval per javascript?


8

Voglio usare con JavaScript e Drupal.t()l'equivalente di format_interval().

Con PHP userei il seguente codice.

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

Quale sarebbe l'equivalente in JavaScript?


Il tmetodo è un testo Drupal che disinfetta e traduce equivalente alla t()funzione PHP del core Drupal.
AyeshK,

Risposte:


4

Drupal non implementa una versione JS di format_interval(); questa è una porta approssimativa (minimamente testata):

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 });
}

Alcuni risultati casuali utilizzando quanto sopra (sembrano corrispondere alla funzione PHP come previsto):

  • 3643 => 1 ora 43 sec
  • 92900 => 1 giorno 1 ora
  • 2592000 => 1 mese
  • 9331200 => 3 mesi 2 settimane
  • 297605232 => 9 anni 5 mesi

1

L'implementazione di Clives sta andando bene. Tuttavia, l'aggregatore javascript di Drupals deve analizzare tutti i file javascript per stringhe traducibili. Poiché Clive utilizza valori dinamici per Drupal.formatPlural, questo non funzionerà qui.

Quindi ecco un'altra implementazione con traduzione funzionante:

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');
}
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.