diff --git a/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php b/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php index 0946709..1d7592b 100644 --- a/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php +++ b/core/lib/Drupal/Core/StringTranslation/StringTranslationTrait.php @@ -64,7 +64,7 @@ protected function formatPluralTranslated($count, $translated, array $args = arr } /** - * Returns number of plurals supported by a given language. + * Returns the number of plurals supported by a given language. * * See the * \Drupal\Core\StringTranslation\TranslationInterface::getNumberOfPlurals() diff --git a/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php index f505dd8..55866eb 100644 --- a/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php +++ b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php @@ -121,11 +121,12 @@ public function formatPlural($count, $singular, $plural, array $args = array(), public function formatPluralTranslated($count, $translation, array $args = array(), array $options = array()); /** - * Returns number of plurals supported by a given language. + * Returns the number of plurals supported by a given language. * - * @param null $langcode + * @param null|string $langcode * (optional) The language code. If not provided, the current language * will be used. + * * @return int * Number of plural variants supported by the given language. */ diff --git a/core/modules/views/src/Plugin/views/field/NumericField.php b/core/modules/views/src/Plugin/views/field/NumericField.php index 3f895f7..60a91fb 100644 --- a/core/modules/views/src/Plugin/views/field/NumericField.php +++ b/core/modules/views/src/Plugin/views/field/NumericField.php @@ -97,9 +97,8 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { '#default_value' => $this->options['format_plural_string'], ); - // @todo Figure out how to pass in the language of the view. $plural_array = explode(LOCALE_PLURAL_DELIMITER, $this->options['format_plural_string']); - $plurals = $this->getNumberOfPlurals(); + $plurals = $this->getNumberOfPlurals($this->view->storage->get('langcode')); if ($plurals > 2) { for ($i = 0; $i < $plurals; $i++) { $form['format_plural_values'][$i] = array( @@ -192,7 +191,8 @@ public function render(ResultRow $values) { return ''; } - // Should we format as a plural. + // If we should format as plural, take the (possibly) translated plural + // setting and format with the current language. if (!empty($this->options['format_plural'])) { $value = $this->formatPluralTranslated($value, $this->options['format_plural_string']); } diff --git a/core/modules/views/src/Tests/Plugin/NumericFormatPluralTest.php b/core/modules/views/src/Tests/Plugin/NumericFormatPluralTest.php index a673ef4..22b0ad5 100644 --- a/core/modules/views/src/Tests/Plugin/NumericFormatPluralTest.php +++ b/core/modules/views/src/Tests/Plugin/NumericFormatPluralTest.php @@ -95,13 +95,14 @@ function testNumericFormatPlural() { $formula = 'nplurals=4; plural=(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3);'; $header = new PoHeader(); list($nplurals, $formula) = $header->parsePluralForms($formula); - debug($formula); \Drupal::state()->set('locale.translation.plurals', ['sl' => ['plurals' => $nplurals, 'formula' => $formula]]); - // @todo change the language of the view here once the handler considers that + // Change the view to Slovenian. + $config = $this->config('views.view.numeric_test'); + $config->set('langcode', 'sl')->save(); // Assert that the user interface has controls with more inputs now. - $this->drupalGet('sl/admin/structure/views/nojs/handler/numeric_test/page_1/field/count'); + $this->drupalGet('admin/structure/views/nojs/handler/numeric_test/page_1/field/count'); $this->assertFieldByName('options[format_plural_values][0]', '1 time'); $this->assertFieldByName('options[format_plural_values][1]', '@count times'); $this->assertFieldByName('options[format_plural_values][2]', ''); @@ -129,6 +130,19 @@ function testNumericFormatPlural() { $this->assertRaw('3 time2'); $this->assertRaw('4 time2'); $this->assertRaw('42 time3'); + + // Add an English configuration translation with English plurals. + $english = \Drupal::languageManager()->getLanguageConfigOverride('en', 'views.view.numeric_test'); + $english->set('display.default.display_options.fields.count.format_plural_string', '1 time' . LOCALE_PLURAL_DELIMITER . '@count times')->save(); + + // The view displayed in English should use the English translation. + $this->drupalGet('numeric-test'); + $this->assertRaw('0 times'); + $this->assertRaw('1 time'); + $this->assertRaw('2 times'); + $this->assertRaw('3 times'); + $this->assertRaw('4 times'); + $this->assertRaw('42 times'); } /**