Form submission handler for locale_translate_edit_form().
locale_translate_edit_form_validate()
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']);
$existing_translation_objects = array();
foreach (Drupal::service('locale.storage')
->getTranslations(array(
'lid' => $lids,
'language' => $langcode,
'translated' => TRUE,
)) as $existing_translation_object) {
$existing_translation_objects[$existing_translation_object->lid] = $existing_translation_object;
}
foreach ($form_state['values']['strings'] as $lid => $new_translation) {
$existing_translation = isset($existing_translation_objects[$lid]);
// Plural translations are saved in a delimited string. To be able to
// compare the new strings with the existing strings a string in the same format is created.
$new_translation_string_delimited = implode(LOCALE_PLURAL_DELIMITER, $new_translation['translations']);
// Generate an imploded string without delimiter, to be able to run
// empty() on it.
$new_translation_string = implode('', $new_translation['translations']);
$is_changed = FALSE;
if ($existing_translation && $existing_translation_objects[$lid]->translation != $new_translation_string_delimited) {
// If there is an existing translation in the DB and the new translation
// is not the same as the existing one.
$is_changed = TRUE;
}
elseif (!$existing_translation && !empty($new_translation_string)) {
// Newly entered translation.
$is_changed = TRUE;
}
if ($is_changed) {
// Only update or insert if we have a value to use.
$target = isset($existing_translation_objects[$lid]) ? $existing_translation_objects[$lid] : Drupal::service('locale.storage')
->createTranslation(array(
'lid' => $lid,
'language' => $langcode,
));
$target
->setPlurals($new_translation['translations'])
->setCustomized()
->save();
$updated[] = $target
->getId();
}
if (empty($new_translation_string) && isset($existing_translation_objects[$lid])) {
// Empty new translation entered: remove existing entry from database.
$existing_translation_objects[$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 refresh configuration and JavaScript translations.
_locale_refresh_translations(array(
$langcode,
), $updated);
_locale_refresh_configuration(array(
$langcode,
), $updated);
}
}