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

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

8 9
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

10 11 12
/**
 * String search screen.
 */
13 14 15 16 17
function locale_translate_page() {
  return array(
    'filter' => drupal_get_form('locale_translate_filter_form'),
    'form' => drupal_get_form('locale_translate_edit_form'),
  );
18 19 20
}

/**
21
 * Build a string search query.
22
 */
23 24
function locale_translate_query() {
  $filter_values = locale_translate_filter_values();
25 26

  $sql_query = db_select('locales_source', 's');
27 28 29
  // Language is sanitized to be one of the possible options in
  // locale_translate_filter_values().
  $sql_query->leftJoin('locales_target', 't', "t.lid = s.lid AND t.language = :langcode", array(':langcode' => $filter_values['langcode']));
30
  $sql_query->fields('s', array('source', 'location', 'context', 'lid'));
31
  $sql_query->fields('t', array('translation', 'language', 'customized'));
32

33 34 35 36 37 38 39 40 41
  if (!empty($filter_values['string'])) {
    $sql_query->condition(db_or()
      ->condition('s.source', '%' . db_like($filter_values['string']) . '%', 'LIKE')
      ->condition('t.translation', '%' . db_like($filter_values['string']) . '%', 'LIKE')
    );
  }

  // Add translation status conditions.
  switch ($filter_values['translation']) {
42
    case 'translated':
43 44 45
      $sql_query->isNotNull('t.translation');
      if ($filter_values['customized'] != 'all') {
        $sql_query->condition('t.customized', $filter_values['customized']);
46
      }
47
      break;
48

49
    case 'untranslated':
50
      $sql_query->isNull('t.translation');
51 52 53 54
      break;

  }

55 56
  $sql_query = $sql_query->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(30);
  return $sql_query->execute();
57 58 59
}

/**
60
 * Build array out of search criteria specified in request variables.
61
 */
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
function locale_translate_filter_values() {
  $filter_values = &drupal_static(__FUNCTION__);
  if (!isset($filter_values)) {
    $filter_values = array();
    $filters = locale_translate_filters();
    foreach ($filters as $key => $filter) {
      $filter_values[$key] = $filter['default'];
      // Let the filter defaults be overwritten by parameters in the URL.
      if (isset($_GET[$key])) {
        // Only allow this value if it was among the options, or
        // if there were no fixed options to filter for.
        if (!isset($filter['options']) || isset($filter['options'][$_GET[$key]])) {
          $filter_values[$key] = $_GET[$key];
        }
      }
      elseif (isset($_SESSION['locale_translate_filter'][$key])) {
        // Only allow this value if it was among the options, or
        // if there were no fixed options to filter for.
        if (!isset($filter['options']) || isset($filter['options'][$_SESSION['locale_translate_filter'][$key]])) {
          $filter_values[$key] = $_SESSION['locale_translate_filter'][$key];
        }
83 84 85
      }
    }
  }
86
  return $filter_values;
87 88 89 90 91
}

/**
 * List locale translation filters that can be applied.
 */
92
function locale_translate_filters() {
93 94
  $filters = array();

95
  // Get all languages, except English.
96
  drupal_static_reset('language_list');
97
  $languages = language_list();
98 99 100 101 102
  $language_options = array();
  foreach ($languages as $langcode => $language) {
    if ($langcode != 'en' || locale_translate_english()) {
      $language_options[$langcode] = $language->name;
    }
103
  }
104

105
  // Pick the current interface language code for the filter.
106
  $default_langcode = language(LANGUAGE_TYPE_INTERFACE)->langcode;
107 108 109 110 111
  if (!isset($language_options[$default_langcode])) {
    $available_langcodes = array_keys($language_options);
    $default_langcode = array_shift($available_langcodes);
  }

112 113 114
  $filters['string'] = array(
    'title' => t('String contains'),
    'description' => t('Leave blank to show all strings. The search is case sensitive.'),
115
    'default' => '',
116 117
  );

118 119 120 121
  $filters['langcode'] = array(
    'title' => t('Translation language'),
    'options' => $language_options,
    'default' => $default_langcode,
122 123 124 125
  );

  $filters['translation'] = array(
    'title' => t('Search in'),
126 127 128
    'options' => array(
      'all' => t('Both translated and untranslated strings'),
      'translated' => t('Only translated strings'),
129
      'untranslated' => t('Only untranslated strings'),
130
    ),
131
    'default' => 'all',
132 133 134 135 136 137 138 139 140 141 142 143
  );

  $filters['customized'] = array(
    'title' => t('Translation type'),
    'options' => array(
      'all' => t('All'),
      LOCALE_NOT_CUSTOMIZED => t('Non-customized translation'),
      LOCALE_CUSTOMIZED => t('Customized translation'),
    ),
    'states' => array(
      'visible' => array(
        ':input[name=translation]' => array('value' => 'translated'),
144
      ),
145
    ),
146
    'default' => 'all',
147 148 149 150 151 152 153 154 155 156
  );

  return $filters;
}

/**
 * Return form for locale translation filters.
 *
 * @ingroup forms
 */
157 158 159 160 161 162 163
function locale_translate_filter_form($form, &$form_state) {
  $filters = locale_translate_filters();
  $filter_values = locale_translate_filter_values();

  $form['#attached']['css'] = array(
    drupal_get_path('module', 'locale') . '/locale.admin.css',
  );
164 165 166 167 168 169 170 171 172 173 174

  $form['filters'] = array(
    '#type' => 'fieldset',
    '#title' => t('Filter translatable strings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  foreach ($filters as $key => $filter) {
    // Special case for 'string' filter.
    if ($key == 'string') {
      $form['filters']['status']['string'] = array(
175
        '#type' => 'search',
176 177
        '#title' => $filter['title'],
        '#description' => $filter['description'],
178
        '#default_value' => $filter_values[$key],
179 180 181
      );
    }
    else {
182
      $empty_option = isset($filter['options'][$filter['default']]) ? $filter['options'][$filter['default']] : '<none>';
183 184 185
      $form['filters']['status'][$key] = array(
        '#title' => $filter['title'],
        '#type' => 'select',
186 187
        '#empty_value' => $filter['default'],
        '#empty_option' => $empty_option,
188 189
        '#size' => 0,
        '#options' => $filter['options'],
190
        '#default_value' => $filter_values[$key],
191
      );
192 193 194
      if (isset($filter['states'])) {
        $form['filters']['status'][$key]['#states'] = $filter['states'];
      }
195 196 197 198 199 200 201 202 203 204 205
    }
  }

  $form['filters']['actions'] = array(
    '#type' => 'actions',
    '#attributes' => array('class' => array('container-inline')),
  );
  $form['filters']['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Filter'),
  );
206
  if (!empty($_SESSION['locale_translate_filter'])) {
207 208
    $form['filters']['actions']['reset'] = array(
      '#type' => 'submit',
209
      '#value' => t('Reset'),
210 211 212 213 214 215 216 217 218
    );
  }

  return $form;
}

/**
 * Process result from locale translation filter form.
 */
219
function locale_translate_filter_form_submit($form, &$form_state) {
220
  $op = $form_state['values']['op'];
221
  $filters = locale_translate_filters();
222 223 224 225
  switch ($op) {
    case t('Filter'):
      foreach ($filters as $name => $filter) {
        if (isset($form_state['values'][$name])) {
226
          $_SESSION['locale_translate_filter'][$name] = $form_state['values'][$name];
227 228 229
        }
      }
      break;
230

231
    case t('Reset'):
232
      $_SESSION['locale_translate_filter'] = array();
233
      break;
234

235 236 237 238 239 240
  }

  $form_state['redirect'] = 'admin/config/regional/translate/translate';
}

/**
241
 * User interface for string editing as one table.
242 243
 *
 * @ingroup forms
244
 */
245 246 247 248 249 250 251 252 253 254 255 256
function locale_translate_edit_form($form, &$form_state) {
  $filter_values = locale_translate_filter_values();
  $langcode = $filter_values['langcode'];

  drupal_static_reset('language_list');
  $languages = language_list();

  $langname = isset($langcode) ? $languages[$langcode]->name : "<none>";

  $path = drupal_get_path('module', 'locale');
  $form['#attached']['css'] = array(
    $path . '/locale.admin.css',
257
  );
258 259
  $form['#attached']['js'] = array(
    $path . '/locale.admin.js',
260 261
  );

262 263 264 265
  $form['langcode'] = array(
    '#type' => 'value',
    '#value' => $filter_values['langcode'],
  );
266

267 268 269 270 271
  $form['strings'] = array(
    '#type' => 'item',
    '#tree' => TRUE,
    '#language' => $langname,
    '#theme' => 'locale_translate_edit_form_strings',
272 273
  );

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
  if (isset($langcode)) {
    $strings = locale_translate_query();

    $plural_formulas = variable_get('locale_translation_plurals', array());

    foreach ($strings as $string) {
      // Split source to work with plural values.
      $source_array = explode(LOCALE_PLURAL_DELIMITER, $string->source);
      $translation_array = explode(LOCALE_PLURAL_DELIMITER, $string->translation);
      if (count($source_array) == 1) {
        // Add original string value and mark as non-plural.
        $form['strings'][$string->lid]['plural'] = array(
          '#type' => 'value',
          '#value' => 0,
        );
        $form['strings'][$string->lid]['original'] = array(
          '#type' => 'item',
          '#title' => t('Source string'),
          '#title_display' => 'invisible',
          '#markup' => check_plain($source_array[0]),
        );
295 296
      }
      else {
297 298 299 300 301 302 303
        // Add original string value and mark as plural.
        $form['strings'][$string->lid]['plural'] = array(
          '#type' => 'value',
          '#value' => 1,
        );
        $form['strings'][$string->lid]['original_singular'] = array(
          '#type' => 'item',
304
          '#title' => t('Singular form'),
305
          '#markup' => check_plain($source_array[0]),
306
        );
307 308
        $form['strings'][$string->lid]['original_plural'] = array(
          '#type' => 'item',
309
          '#title' => t('Plural form'),
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
          '#markup' => check_plain($source_array[1]),
        );
      }
      if (!empty($string->context)) {
        $form['strings'][$string->lid]['context'] = array(
          '#type' => 'value',
          '#value' => check_plain($string->context),
        );
      }
      $form['strings'][$string->lid]['location'] = array(
        '#type' => 'value',
        '#value' => $string->location,
      );

      // Approximate the number of rows to use in the default textarea.
      $rows = min(ceil(str_word_count($source_array[0]) / 12), 10);
      if (empty($form['strings'][$string->lid]['plural']['#value'])) {
        $form['strings'][$string->lid]['translations'][0] = array(
          '#type' => 'textarea',
          '#title' => t('Translated string'),
          '#title_display' => 'invisible',
331
          '#rows' => $rows,
332
          '#default_value' => $translation_array[0],
333 334
        );
      }
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
      else {
        // Dealing with plural strings.
        if (isset($plural_formulas[$langcode]['plurals']) && $plural_formulas[$langcode]['plurals'] > 2) {
          // Add a textarea for each plural variant.
          for ($i = 0; $i < $plural_formulas[$langcode]['plurals']; $i++) {
            $form['strings'][$string->lid]['translations'][$i] = array(
              '#type' => 'textarea',
              '#title' => ($i == 0 ? t('Singular form') : format_plural($i, 'First plural form', '@count. plural form')),
              '#rows' => $rows,
              '#default_value' => isset($translation_array[$i]) ? $translation_array[$i] : '',
            );
          }
        }
        else {
          // Fallback for unknown number of plurals.
          $form['strings'][$string->lid]['translations'][0] = array(
            '#type' => 'textarea',
            '#title' => t('Singular form'),
            '#rows' => $rows,
            '#default_value' => $translation_array[0],
          );
          $form['strings'][$string->lid]['translations'][1] = array(
            '#type' => 'textarea',
            '#title' => t('Plural form'),
            '#rows' => $rows,
            '#default_value' => isset($translation_array[1]) ? $translation_array[1] : '',
          );
        }
      }
364
    }
365 366 367
    if (count(element_children($form['strings']))) {
      $form['actions'] = array('#type' => 'actions');
      $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save translations'));
368
    }
369 370 371 372 373 374 375 376
  }
  return $form;
}

/**
 * Validate string editing form submissions.
 */
function locale_translate_edit_form_validate($form, &$form_state) {
377 378 379
  $langcode = $form_state['values']['langcode'];
  foreach ($form_state['values']['strings'] as $lid => $translations) {
    foreach ($translations['translations'] as $key => $value) {
380
      if (!locale_string_is_safe($value)) {
381
        form_set_error("strings][$lid][translations][$key", t('The submitted string contains disallowed HTML: %string', array('%string' => $value)));
382 383 384
        form_set_error("translations][$langcode][$key", t('The submitted string contains disallowed HTML: %string', array('%string' => $value)));
        watchdog('locale', 'Attempted submission of a translation string with disallowed HTML: %string', array('%string' => $value), WATCHDOG_WARNING);
      }
385 386 387 388 389 390 391 392
    }
  }
}

/**
 * Process string editing form submissions.
 */
function locale_translate_edit_form_submit($form, &$form_state) {
393 394
  $langcode = $form_state['values']['langcode'];
  foreach ($form_state['values']['strings'] as $lid => $translations) {
395
    // Serialize plural variants in one string by LOCALE_PLURAL_DELIMITER.
396 397
    $translation_new = implode(LOCALE_PLURAL_DELIMITER, $translations['translations']);
    $translation_old = db_query("SELECT translation FROM {locales_target} WHERE lid = :lid AND language = :language", array(':lid' => $lid, ':language' => $langcode))->fetchField();
398 399
    // No translation when all strings are empty.
    $has_translation = FALSE;
400
    foreach ($translations['translations'] as $string) {
401 402 403 404 405 406
      if (!empty($string)) {
        $has_translation = TRUE;
        break;
      }
    }
    if ($has_translation) {
407
      // Only update or insert if we have a value to use.
408
      if (!empty($translation_old) && $translation_old != $translation_new) {
409 410
        db_update('locales_target')
          ->fields(array(
411
            'translation' => $translation_new,
412
            'customized' => LOCALE_CUSTOMIZED,
413 414
          ))
          ->condition('lid', $lid)
415
          ->condition('language', $langcode)
416 417
          ->execute();
      }
418
      if (empty($translation_old)) {
419 420 421
        db_insert('locales_target')
          ->fields(array(
            'lid' => $lid,
422
            'translation' => $translation_new,
423
            'language' => $langcode,
424
            'customized' => LOCALE_CUSTOMIZED,
425 426 427 428
          ))
          ->execute();
      }
    }
429
    elseif (!empty($translation_old)) {
430 431 432
      // Empty translation entered: remove existing entry from database.
      db_delete('locales_target')
        ->condition('lid', $lid)
433
        ->condition('language', $langcode)
434 435 436 437 438
        ->execute();
    }

  }

439 440 441 442 443 444
  drupal_set_message(t('The strings have been saved.'));

  // Keep the user on the current pager page.
  if (isset($_GET['page'])) {
    $form_state['redirect'] = array('admin/config/regional/translate', array('query' => array('page' => $_GET['page'])));
  }
445

446 447
  // Force JavaScript translation file recreation for this language.
  _locale_invalidate_js($langcode);
448
  // Clear locale cache.
449
  cache()->deletePrefix('locale:');
450 451 452
}

/**
453
 * Default theme function for translatione edit form.
454
 */
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
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();
  foreach (element_children($form) as $lid) {
    $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(
      array('data' => $source),
      array('data' => $string['translations']),
    );
476
  }
477 478 479 480 481 482 483 484
  $output .= theme('table', array(
    'header' => $header,
    'rows' => $rows,
    'empty' => t('No strings available.'),
    'attributes' => array('class' => array('locale-translate-edit-table')),
  ));
  $output .= theme('pager');
  return $output;
485
}