/**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $filter_values = $this->translateFilterValues();
     $langcode = $filter_values['langcode'];
     $this->languageManager->reset();
     $languages = $this->languageManager->getLanguages();
     $langname = isset($langcode) ? $languages[$langcode]->getName() : "- None -";
     $form['#attached']['library'][] = 'locale/drupal.locale.admin';
     $form['langcode'] = array('#type' => 'value', '#value' => $filter_values['langcode']);
     $form['strings'] = array('#type' => 'table', '#tree' => TRUE, '#language' => $langname, '#header' => [$this->t('Source string'), $this->t('Translation for @language', ['@language' => $langname])], '#empty' => $this->t('No strings available.'), '#attributes' => ['class' => ['locale-translate-edit-table']]);
     if (isset($langcode)) {
         $strings = $this->translateFilterLoadStrings();
         $plural_formulas = $this->state->get('locale.translation.plurals') ?: array();
         foreach ($strings as $string) {
             // Cast into source string, will do for our purposes.
             $source = new SourceString($string);
             // Split source to work with plural values.
             $source_array = $source->getPlurals();
             $translation_array = $string->getPlurals();
             if (count($source_array) == 1) {
                 // Add original string value and mark as non-plural.
                 $plural = FALSE;
                 $form['strings'][$string->lid]['original'] = array('#type' => 'item', '#title' => $this->t('Source string (@language)', array('@language' => $this->t('Built-in English'))), '#title_display' => 'invisible', '#markup' => '<span lang="en">' . String::checkPlain($source_array[0]) . '</span>');
             } else {
                 // Add original string value and mark as plural.
                 $plural = TRUE;
                 $original_singular = ['#type' => 'item', '#title' => $this->t('Singular form'), '#markup' => '<span lang="en">' . String::checkPlain($source_array[0]) . '</span>', '#prefix' => '<span class="visually-hidden">' . $this->t('Source string (@language)', array('@language' => $this->t('Built-in English'))) . '</span>'];
                 $original_plural = ['#type' => 'item', '#title' => $this->t('Plural form'), '#markup' => '<span lang="en">' . String::checkPlain($source_array[1]) . '</span>'];
                 $form['strings'][$string->lid]['original'] = [$original_singular, ['#markup' => '<br>'], $original_plural];
             }
             if (!empty($string->context)) {
                 $form['strings'][$string->lid]['original'][] = ['#type' => 'inline_template', '#template' => '<br><small>{{ context_title }}: <span lang="en">{{ context }}</span></small>', '#context' => ['context_title' => $this->t('In Context'), 'context' => $string->context]];
             }
             // Approximate the number of rows to use in the default textarea.
             $rows = min(ceil(str_word_count($source_array[0]) / 12), 10);
             if (!$plural) {
                 $form['strings'][$string->lid]['translations'][0] = array('#type' => 'textarea', '#title' => $this->t('Translated string (@language)', array('@language' => $langname)), '#title_display' => 'invisible', '#rows' => $rows, '#default_value' => $translation_array[0], '#attributes' => array('lang' => $langcode));
             } 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 ? $this->t('Singular form') : $this->formatPlural($i, 'First plural form', '@count. plural form'), '#rows' => $rows, '#default_value' => isset($translation_array[$i]) ? $translation_array[$i] : '', '#attributes' => array('lang' => $langcode), '#prefix' => $i == 0 ? '<span class="visually-hidden">' . $this->t('Translated string (@language)', array('@language' => $langname)) . '</span>' : '');
                     }
                 } else {
                     // Fallback for unknown number of plurals.
                     $form['strings'][$string->lid]['translations'][0] = array('#type' => 'textarea', '#title' => $this->t('Singular form'), '#rows' => $rows, '#default_value' => $translation_array[0], '#attributes' => array('lang' => $langcode), '#prefix' => '<span class="visually-hidden">' . $this->t('Translated string (@language)', array('@language' => $langname)) . '</span>');
                     $form['strings'][$string->lid]['translations'][1] = array('#type' => 'textarea', '#title' => $this->t('Plural form'), '#rows' => $rows, '#default_value' => isset($translation_array[1]) ? $translation_array[1] : '', '#attributes' => array('lang' => $langcode));
                 }
             }
         }
         if (count(Element::children($form['strings']))) {
             $form['actions'] = array('#type' => 'actions');
             $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save translations'));
         }
     }
     $form['pager']['#type'] = 'pager';
     return $form;
 }
 /**
  * {@inheritdoc}
  */
 public function findString(array $conditions)
 {
     $values = $this->dbStringSelect($conditions)->execute()->fetchAssoc();
     if (!empty($values)) {
         $string = new SourceString($values);
         $string->setStorage($this);
         return $string;
     }
 }
Exemple #3
0
 /**
  * @covers ::delete
  * @expectedException \Drupal\locale\StringStorageException
  * @expectedExceptionMessage The string cannot be deleted because its not bound to a storage: test
  */
 public function testDeleteWithoutStorage()
 {
     $string = new SourceString(['lid' => 1, 'source' => 'test']);
     $string->delete();
 }