3 namespace Drupal\locale\Form;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Render\Element;
7 use Drupal\locale\SourceString;
10 * Defines a translation edit form.
12 class TranslateEditForm extends TranslateFormBase {
17 public function getFormId() {
18 return 'locale_translate_edit_form';
24 public function buildForm(array $form, FormStateInterface $form_state) {
25 $filter_values = $this->translateFilterValues();
26 $langcode = $filter_values['langcode'];
28 $this->languageManager->reset();
29 $languages = $this->languageManager->getLanguages();
31 $langname = isset($langcode) ? $languages[$langcode]->getName() : "- None -";
33 $form['#attached']['library'][] = 'locale/drupal.locale.admin';
37 '#value' => $filter_values['langcode'],
43 '#language' => $langname,
45 $this->t('Source string'),
46 $this->t('Translation for @language', ['@language' => $langname]),
48 '#empty' => $this->t('No strings available.'),
49 '#attributes' => ['class' => ['locale-translate-edit-table']],
52 if (isset($langcode)) {
53 $strings = $this->translateFilterLoadStrings();
55 $plurals = $this->getNumberOfPlurals($langcode);
57 foreach ($strings as $string) {
58 // Cast into source string, will do for our purposes.
59 $source = new SourceString($string);
60 // Split source to work with plural values.
61 $source_array = $source->getPlurals();
62 $translation_array = $string->getPlurals();
63 if (count($source_array) == 1) {
64 // Add original string value and mark as non-plural.
66 $form['strings'][$string->lid]['original'] = [
68 '#title' => $this->t('Source string (@language)', ['@language' => $this->t('Built-in English')]),
69 '#title_display' => 'invisible',
70 '#plain_text' => $source_array[0],
71 '#preffix' => '<span lang="en">',
72 '#suffix' => '</span>',
76 // Add original string value and mark as plural.
78 $original_singular = [
80 '#title' => $this->t('Singular form'),
81 '#plain_text' => $source_array[0],
82 '#prefix' => '<span class="visually-hidden">' . $this->t('Source string (@language)', ['@language' => $this->t('Built-in English')]) . '</span><span lang="en">',
83 '#suffix' => '</span>',
87 '#title' => $this->t('Plural form'),
88 '#plain_text' => $source_array[1],
89 '#preffix' => '<span lang="en">',
90 '#suffix' => '</span>',
92 $form['strings'][$string->lid]['original'] = [
94 ['#markup' => '<br>'],
98 if (!empty($string->context)) {
99 $form['strings'][$string->lid]['original'][] = [
100 '#type' => 'inline_template',
101 '#template' => '<br><small>{{ context_title }}: <span lang="en">{{ context }}</span></small>',
103 'context_title' => $this->t('In Context'),
104 'context' => $string->context,
108 // Approximate the number of rows to use in the default textarea.
109 $rows = min(ceil(str_word_count($source_array[0]) / 12), 10);
111 $form['strings'][$string->lid]['translations'][0] = [
112 '#type' => 'textarea',
113 '#title' => $this->t('Translated string (@language)', ['@language' => $langname]),
114 '#title_display' => 'invisible',
116 '#default_value' => $translation_array[0],
117 '#attributes' => ['lang' => $langcode],
121 // Add a textarea for each plural variant.
122 for ($i = 0; $i < $plurals; $i++) {
123 $form['strings'][$string->lid]['translations'][$i] = [
124 '#type' => 'textarea',
125 // @todo Should use better labels https://www.drupal.org/node/2499639
126 '#title' => ($i == 0 ? $this->t('Singular form') : $this->formatPlural($i, 'First plural form', '@count. plural form')),
128 '#default_value' => isset($translation_array[$i]) ? $translation_array[$i] : '',
129 '#attributes' => ['lang' => $langcode],
130 '#prefix' => $i == 0 ? ('<span class="visually-hidden">' . $this->t('Translated string (@language)', ['@language' => $langname]) . '</span>') : '',
134 // Simplify interface text for the most common case.
135 $form['strings'][$string->lid]['translations'][1]['#title'] = $this->t('Plural form');
139 if (count(Element::children($form['strings']))) {
140 $form['actions'] = ['#type' => 'actions'];
141 $form['actions']['submit'] = [
143 '#value' => $this->t('Save translations'),
147 $form['pager']['#type'] = 'pager';
154 public function validateForm(array &$form, FormStateInterface $form_state) {
155 $langcode = $form_state->getValue('langcode');
156 foreach ($form_state->getValue('strings') as $lid => $translations) {
157 foreach ($translations['translations'] as $key => $value) {
158 if (!locale_string_is_safe($value)) {
159 $form_state->setErrorByName("strings][$lid][translations][$key", $this->t('The submitted string contains disallowed HTML: %string', ['%string' => $value]));
160 $form_state->setErrorByName("translations][$langcode][$key", $this->t('The submitted string contains disallowed HTML: %string', ['%string' => $value]));
161 $this->logger('locale')->warning('Attempted submission of a translation string with disallowed HTML: %string', ['%string' => $value]);
170 public function submitForm(array &$form, FormStateInterface $form_state) {
171 $langcode = $form_state->getValue('langcode');
174 // Preload all translations for strings in the form.
175 $lids = array_keys($form_state->getValue('strings'));
176 $existing_translation_objects = [];
177 foreach ($this->localeStorage->getTranslations(['lid' => $lids, 'language' => $langcode, 'translated' => TRUE]) as $existing_translation_object) {
178 $existing_translation_objects[$existing_translation_object->lid] = $existing_translation_object;
181 foreach ($form_state->getValue('strings') as $lid => $new_translation) {
182 $existing_translation = isset($existing_translation_objects[$lid]);
184 // Plural translations are saved in a delimited string. To be able to
185 // compare the new strings with the existing strings a string in the same
186 // format is created.
187 $new_translation_string_delimited = implode(LOCALE_PLURAL_DELIMITER, $new_translation['translations']);
189 // Generate an imploded string without delimiter, to be able to run
191 $new_translation_string = implode('', $new_translation['translations']);
195 if ($existing_translation && $existing_translation_objects[$lid]->translation != $new_translation_string_delimited) {
196 // If there is an existing translation in the DB and the new translation
197 // is not the same as the existing one.
200 elseif (!$existing_translation && !empty($new_translation_string)) {
201 // Newly entered translation.
206 // Only update or insert if we have a value to use.
207 $target = isset($existing_translation_objects[$lid]) ? $existing_translation_objects[$lid] : $this->localeStorage->createTranslation(['lid' => $lid, 'language' => $langcode]);
208 $target->setPlurals($new_translation['translations'])
211 $updated[] = $target->getId();
213 if (empty($new_translation_string) && isset($existing_translation_objects[$lid])) {
214 // Empty new translation entered: remove existing entry from database.
215 $existing_translation_objects[$lid]->delete();
220 drupal_set_message($this->t('The strings have been saved.'));
222 // Keep the user on the current pager page.
223 $page = $this->getRequest()->query->get('page');
225 $form_state->setRedirect(
226 'locale.translate_page',
233 // Clear cache and force refresh of JavaScript translations.
234 _locale_refresh_translations([$langcode], $updated);
235 _locale_refresh_configuration([$langcode], $updated);