You are here

function timeago_add_js in Timeago 7.2

Same name and namespace in other branches
  1. 6.2 timeago.module \timeago_add_js()

Overrides the default translation of Timeago dates if necessary.

2 calls to timeago_add_js()
timeago_format_date in ./timeago.module
Converts a timestamp into a Timeago date.
timeago_init in ./timeago.module
Implements hook_init().

File

./timeago.module, line 475
Adds support for the Timeago jQuery library.

Code

function timeago_add_js() {

  // Add the Timeago library, the module's helper JS, and the default
  // translation of Timeago date terms.
  $library_path = drupal_get_path('module', 'timeago');
  if (!drupal_add_library('timeago', 'timeago') && module_exists('libraries') && function_exists('libraries_load')) {
    $library_path = libraries_get_path('timeago');
    libraries_load('timeago');
    $path = drupal_get_path('module', 'timeago') . '/timeago.js';
    drupal_add_js($path);
  }

  // Build the settings array structure.
  $settings = array(
    'refreshMillis' => (int) variable_get('timeago_js_refresh_millis', 60000),
    'allowFuture' => (bool) variable_get('timeago_js_allow_future', 1),
    'localeTitle' => (bool) variable_get('timeago_js_locale_title', 0),
    'cutoff' => (int) variable_get('timeago_js_cutoff', ''),
  );

  // Some languages (Arabic, Polish, Russian, Ukranian, etc.) have different
  // suffixes depending on the numbers used in the dates, so we may need to
  // have more complex translations than Drupal allows. To support these cases,
  // we allow adding a script that will override the translations. Examples
  // are available at https://github.com/rmm5t/jquery-timeago
  $path = $library_path . '/locales/jquery.timeago.' . $GLOBALS['language']->language . '.js';
  if (!file_exists($path)) {
    $path = $library_path . '/jquery.timeago.' . $GLOBALS['language']->language . '.js';
  }
  if (file_exists($path)) {
    drupal_add_js($path, array(
      'weight' => 1,
    ));
  }
  else {

    // If the JavaScript translation files are not in use, we can pass the
    // string settings in.
    $settings_vars = timeago_get_settings_variables();
    $settings['strings'] = array();

    // If the variable module exists, use it!
    if (module_exists('variable')) {
      foreach ($settings_vars as $js_var => $variable) {
        $settings['strings'][$js_var] = variable_get_value($variable['variable_name']);
      }
    }
    else {
      foreach ($settings_vars as $js_var => $variable) {
        $settings['strings'][$js_var] = variable_get($variable['variable_name'], $variable['default']);
      }
    }

    // Check plain the strings.
    foreach ($settings['strings'] as $k => $string) {
      if ($string == '') {
        continue;
      }
      $settings['strings'][$k] = check_plain($string);
    }

    // Tack in the last one we don't want t'ed.
    $settings['strings']['wordSeparator'] = check_plain(variable_get('timeago_js_strings_word_separator', ' '));
  }
  drupal_add_js(array(
    'timeago' => $settings,
  ), 'setting');
}