diff --git a/core/lib/Drupal/Core/StringTranslation/TranslationManager.php b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php index 547bc96..c653dad 100644 --- a/core/lib/Drupal/Core/StringTranslation/TranslationManager.php +++ b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php @@ -245,7 +245,8 @@ public function reset() { */ public function getNumberOfPlurals($langcode = NULL) { // If the state service is not injected, we assume 2 plural variants are - // allowed. This may happen in the installer for simplicity. + // allowed. This may happen in the installer for simplicity. We also assume + // 2 plurals if there is no explicit information yet. if (isset($this->state)) { $langcode = $langcode ?: $this->languageManager->getCurrentLanguage()->getId(); $plural_formulas = $this->state->get('locale.translation.plurals') ?: array(); diff --git a/core/modules/locale/src/Form/TranslateEditForm.php b/core/modules/locale/src/Form/TranslateEditForm.php index 9a16fb7..f8e9938 100644 --- a/core/modules/locale/src/Form/TranslateEditForm.php +++ b/core/modules/locale/src/Form/TranslateEditForm.php @@ -58,7 +58,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { if (isset($langcode)) { $strings = $this->translateFilterLoadStrings(); - $plural_formulas = $this->state->get('locale.translation.plurals') ?: array(); + $plurals = $this->getNumberOfPlurals($langcode); foreach ($strings as $string) { // Cast into source string, will do for our purposes. @@ -119,38 +119,21 @@ public function buildForm(array $form, FormStateInterface $form_state) { ); } 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 ? ('' . $this->t('Translated string (@language)', array('@language' => $langname)) . '') : '', - ); - } - } - 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' => '' . $this->t('Translated string (@language)', array('@language' => $langname)) . '', - ); - $form['strings'][$string->lid]['translations'][1] = array( + // Add a textarea for each plural variant. + for ($i = 0; $i < $plurals; $i++) { + $form['strings'][$string->lid]['translations'][$i] = array( '#type' => 'textarea', - '#title' => $this->t('Plural form'), + '#title' => ($i == 0 ? $this->t('Singular form') : $this->formatPlural($i, 'First plural form', '@count. plural form')), '#rows' => $rows, - '#default_value' => isset($translation_array[1]) ? $translation_array[1] : '', + '#default_value' => isset($translation_array[$i]) ? $translation_array[$i] : '', '#attributes' => array('lang' => $langcode), + '#prefix' => $i == 0 ? ('' . $this->t('Translated string (@language)', array('@language' => $langname)) . '') : '', ); } + if ($plurals == 2) { + // Simplify user interface text for the most common case. + $form['strings'][$string->lid]['translations'][1]['title'] = $this->t('Plural form'); + } } } if (count(Element::children($form['strings']))) { diff --git a/core/modules/views/src/Plugin/views/field/NumericField.php b/core/modules/views/src/Plugin/views/field/NumericField.php index 60a91fb..dbf02d9 100644 --- a/core/modules/views/src/Plugin/views/field/NumericField.php +++ b/core/modules/views/src/Plugin/views/field/NumericField.php @@ -99,39 +99,12 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { $plural_array = explode(LOCALE_PLURAL_DELIMITER, $this->options['format_plural_string']); $plurals = $this->getNumberOfPlurals($this->view->storage->get('langcode')); - if ($plurals > 2) { - for ($i = 0; $i < $plurals; $i++) { - $form['format_plural_values'][$i] = array( - '#type' => 'textfield', - '#title' => ($i == 0 ? $this->t('Singular form') : $this->formatPlural($i, 'First plural form', '@count. plural form')), - '#default_value' => isset($plural_array[$i]) ? $plural_array[$i] : '', - '#description' => $this->t('Text to use for this variant, @count will be replaced with the value.'), - '#states' => array( - 'visible' => array( - ':input[name="options[format_plural]"]' => array('checked' => TRUE), - ), - ), - ); - } - } - else { - // Fallback for unknown number of plurals. - $form['format_plural_values'][0] = array( - '#type' => 'textfield', - '#title' => $this->t('Singular form'), - '#default_value' => $plural_array[0], - '#description' => $this->t('Text to use for the singular form.'), - '#states' => array( - 'visible' => array( - ':input[name="options[format_plural]"]' => array('checked' => TRUE), - ), - ), - ); - $form['format_plural_values'][1] = array( + for ($i = 0; $i < $plurals; $i++) { + $form['format_plural_values'][$i] = array( '#type' => 'textfield', - '#title' => $this->t('Plural form'), - '#default_value' => isset($plural_array[1]) ? $plural_array[1] : '', - '#description' => $this->t('Text to use for the plural form, @count will be replaced with the value.'), + '#title' => ($i == 0 ? $this->t('Singular form') : $this->formatPlural($i, 'First plural form', '@count. plural form')), + '#default_value' => isset($plural_array[$i]) ? $plural_array[$i] : '', + '#description' => $this->t('Text to use for this variant, @count will be replaced with the value.'), '#states' => array( 'visible' => array( ':input[name="options[format_plural]"]' => array('checked' => TRUE), @@ -139,6 +112,12 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { ), ); } + if ($plurals == 2) { + // Simplify user interface text for the most common case. + $form['format_plural_values'][0]['description'] = $this->t('Text to use for the singular form, @count will be replaced with the value.'); + $form['format_plural_values'][1]['title'] = $this->t('Plural form'); + $form['format_plural_values'][1]['description'] = $this->t('Text to use for the plural form, @count will be replaced with the value.'); + } $form['prefix'] = array( '#type' => 'textfield', diff --git a/core/modules/views/src/Tests/Plugin/NumericFormatPluralTest.php b/core/modules/views/src/Tests/Plugin/NumericFormatPluralTest.php index 22b0ad5..870342b 100644 --- a/core/modules/views/src/Tests/Plugin/NumericFormatPluralTest.php +++ b/core/modules/views/src/Tests/Plugin/NumericFormatPluralTest.php @@ -8,8 +8,6 @@ namespace Drupal\views\Tests\Plugin; use Drupal\Component\Gettext\PoHeader; -use Drupal\Component\Utility\Unicode; -use Drupal\language\Entity\ConfigurableLanguage; use Drupal\views\Tests\ViewTestBase; /**