function locale_translate_edit_form_submit

Form submission handler for locale_translate_edit_form().

See also

locale_translate_edit_form_validate()

File

drupal/core/modules/locale/locale.pages.inc, line 399
Interface translation summary, editing and deletion user interfaces.

Code

function locale_translate_edit_form_submit($form, &$form_state) {
  $langcode = $form_state['values']['langcode'];
  $updated = array();

  // Preload all translations for strings in the form.
  $lids = array_keys($form_state['values']['strings']);
  $strings = array();
  foreach (locale_storage()
    ->getTranslations(array(
    'lid' => $lids,
    'language' => $langcode,
    'translated' => TRUE,
  )) as $string) {
    $strings[$string->lid] = $string;
  }
  foreach ($form_state['values']['strings'] as $lid => $translations) {

    // No translation when all strings are empty.
    $has_translation = FALSE;
    foreach ($translations['translations'] as $string) {
      if (!empty($string)) {
        $has_translation = TRUE;
        break;
      }
    }
    if ($has_translation) {

      // Only update or insert if we have a value to use.
      $target = isset($strings[$lid]) ? $strings[$lid] : locale_storage()
        ->createTranslation(array(
        'lid' => $lid,
        'language' => $langcode,
      ));
      $target
        ->setPlurals($translations['translations'])
        ->setCustomized()
        ->save();
      $updated[] = $target
        ->getId();
    }
    elseif (isset($strings[$lid])) {

      // Empty translation entered: remove existing entry from database.
      $strings[$lid]
        ->delete();
      $updated[] = $lid;
    }
  }
  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'],
        ),
      ),
    );
  }
  if ($updated) {

    // Clear cache and force refresh of JavaScript translations.
    _locale_refresh_translations(array(
      $langcode,
    ), $updated);
  }
}