locale.pages.inc 5.53 KB
Newer Older
1 2 3
<?php

/**
4 5
 * @file
 * Interface translation summary, editing and deletion user interfaces.
6 7
 */

8
use Drupal\Component\Utility\SafeMarkup;
9
use Drupal\Core\Render\Element;
10 11
use Drupal\locale\SourceString;
use Drupal\locale\TranslationString;
12
use Symfony\Component\HttpFoundation\RedirectResponse;
13 14
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

15
/**
16
 * Page callback: Checks for translation updates and displays the status.
17
 *
18
 * Manually checks the translation status without the use of cron.
19 20
 *
 * @see locale_menu()
21
 */
22 23
function locale_translation_manual_status() {
  module_load_include('compare.inc', 'locale');
24

25 26 27 28 29
  // Check the translation status of all translatable projects in all languages.
  // First we clear the cached list of projects. Although not strictly
  // necessary, this is helpful in case the project list is out of sync.
  locale_translation_flush_projects();
  locale_translation_check_projects();
30

31 32 33 34
  // Execute a batch if required. A batch is only used when remote files
  // are checked.
  if (batch_get()) {
    return batch_process('admin/reports/translations');
35
  }
36
  return new RedirectResponse(url('admin/reports/translations', array('absolute' => TRUE)));
37 38
}

39 40 41 42 43 44 45 46 47
/**
 * Returns HTML for translation edit form.
 *
 * @param array $variables
 *   An associative array containing:
 *   - form: The form that contains the language information.
 *
 * @see locale_translate_edit_form()
 * @ingroup themeable
48
 */
49 50 51 52 53 54 55 56
function theme_locale_translate_edit_form_strings($variables) {
  $output = '';
  $form = $variables['form'];
  $header = array(
    t('Source string'),
    t('Translation for @language', array('@language' => $form['#language'])),
  );
  $rows = array();
57
  foreach (Element::children($form) as $lid) {
58 59 60 61 62 63 64 65 66
    $string = $form[$lid];
    if ($string['plural']['#value']) {
      $source = drupal_render($string['original_singular']) . '<br />' . drupal_render($string['original_plural']);
    }
    else {
      $source = drupal_render($string['original']);
    }
    $source .= empty($string['context']) ? '' : '<br /><small>' . t('In Context') . ':&nbsp;' . $string['context']['#value'] . '</small>';
    $rows[] = array(
67
      array('data' => SafeMarkup::set($source)),
68 69
      array('data' => $string['translations']),
    );
70
  }
71
  $table = array(
72
    '#type' => 'table',
73 74 75 76 77 78 79 80
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('No strings available.'),
    '#attributes' => array('class' => array('locale-translate-edit-table')),
  );
  $output .= drupal_render($table);
  $pager = array('#theme' => 'pager');
  $output .= drupal_render($pager);
81
  return $output;
82
}
83 84

/**
85 86 87 88 89
 * Prepares variables for translation status information templates.
 *
 * Translation status information is displayed per language.
 *
 * Default template: locale-translate-edit-form-strings.html.twig.
90 91 92 93 94 95
 *
 * @param array $variables
 *   An associative array containing:
 *   - updates: The projects which have updates.
 *   - not_found: The projects which updates are not found.
 *
96
 * @see \Drupal\locale\Form\TranslationStatusForm
97
 */
98 99
function template_preprocess_locale_translation_update_info(&$variables) {
  $details = array();
100 101 102

  // Build output for available updates.
  if (isset($variables['updates'])) {
103
    $releases = array();
104 105 106 107 108
    if ($variables['updates']) {
      foreach ($variables['updates'] as $update) {
        $modules[] = $update['name'];
        $releases[] = t('@module (@date)', array('@module' => $update['name'], '@date' => format_date($update['timestamp'], 'html_date')));
      }
109
      $variables['modules'] = $modules;
110
    }
111 112 113 114
    $details['available_updates_list'] = array(
      '#theme' => 'item_list',
      '#items' => $releases,
    );
115 116 117 118
  }

  // Build output for updates not found.
  if (isset($variables['not_found'])) {
119
    $releases = array();
120
    $variables['missing_updates_status'] = format_plural(count($variables['not_found']), 'Missing translations for one project', 'Missing translations for @count projects');
121 122 123 124 125 126
    if ($variables['not_found']) {
      foreach ($variables['not_found'] as $update) {
        $version = $update['version'] ? $update['version'] : t('no version');
        $releases[] = t('@module (@version).', array('@module' => $update['name'], '@version' => $version)) . ' ' . $update['info'];
      }
    }
127 128 129 130 131 132 133 134
    $details['missing_updates_list'] = array(
      '#theme' => 'item_list',
      '#items' => $releases,
    );
    // Prefix the missing updates list if there is an available updates lists
    // before it.
    if (!empty($details['available_updates_list']['#items'])) {
      $details['missing_updates_list']['#prefix'] = t('Missing translations for:');
135 136
    }
  }
137
  $variables['details'] = $details;
138 139 140
}

/**
141 142 143 144 145 146
 * Prepares variables for most recent translation update templates.
 *
 * Displays the last time we checked for locale update data. In addition to
 * properly formatting the given timestamp, this function also provides a "Check
 * manually" link that refreshes the available update and redirects back to the
 * same page.
147
 *
148
 * Default template: locale-translation-last-check.html.twig.
149 150 151 152 153
 *
 * @param $variables
 *   An associative array containing:
 *   - last: The timestamp when the site last checked for available updates.
 *
154
 * @see \Drupal\locale\Form\TranslationStatusForm
155
 */
156
function template_preprocess_locale_translation_last_check(&$variables) {
157
  $last = $variables['last'];
158
  $variables['last_checked'] = ($last != NULL);
159
  $variables['time'] = \Drupal::service('date.formatter')->formatInterval(REQUEST_TIME - $last);
160
  $variables['link'] = l(t('Check manually'), 'admin/reports/translations/check', array('query' => drupal_get_destination()));
161
}