diff --git a/core/modules/config_translation/src/FormElement/PluralVariants.php b/core/modules/config_translation/src/FormElement/PluralVariants.php index c7c6a88..a71d8fc 100644 --- a/core/modules/config_translation/src/FormElement/PluralVariants.php +++ b/core/modules/config_translation/src/FormElement/PluralVariants.php @@ -38,7 +38,7 @@ protected function getSourceElement(LanguageInterface $source_language, $source_ )), '#markup' => SafeMarkup::format('@value', array( '@langcode' => $source_language->getId(), - '@value' => $values[$i] ?: $this->t('(Empty)'), + '@value' => isset($values[$i]) ? $values[$i] : $this->t('(Empty)'), )), ); } diff --git a/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php b/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php index 172bc8e..4bbb408 100644 --- a/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php +++ b/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php @@ -611,6 +611,49 @@ public function testViewsTranslationUI() { } /** + * Test the number of source elements for plural strings in config translation forms. + */ + public function testPluralConfigStringsSourceElements() { + $this->drupalLogin($this->adminUser); + + // Languages to test, with various number of plural forms. + $languages = array( + 'vi' => array('plurals' => 1, 'expected' => array(TRUE, FALSE, FALSE, FALSE)), + 'fr' => array('plurals' => 2, 'expected' => array(TRUE, TRUE, FALSE, FALSE)), + 'sl' => array('plurals' => 4, 'expected' => array(TRUE, TRUE, TRUE, TRUE)), + ); + + foreach ($languages as $langcode => $data) { + // Import a .po file to add a new language with a given number of plural forms + $name = tempnam('temporary://', $langcode . '_') . '.po'; + file_put_contents($name, $this->getPoFile($data['plurals'])); + $this->drupalPostForm('admin/config/regional/translate/import', array( + 'langcode' => $langcode, + 'files[file]' => $name, + ), t('Import')); + + // Change the config langcode of the 'files' view. + $config = \Drupal::service('config.factory')->getEditable('views.view.files'); + $config->set('langcode', $langcode); + $config->save(); + + // Go to the translation page of the 'files' view. + $translation_url = 'admin/structure/views/view/files/translate/' . $langcode . '/add'; + $this->drupalGet($translation_url); + + // Check if the expected number of source elements are present. + foreach ($data['expected'] as $index => $expected) { + if ($expected) { + $this->assertRaw('edit-source-config-names-viewsviewfiles-display-default-display-options-fields-count-format-plural-string-' . $index); + } + else { + $this->assertNoRaw('edit-source-config-names-viewsviewfiles-display-default-display-options-fields-count-format-plural-string-' . $index); + } + } + } + } + + /** * Test translation of plural strings with multiple plural forms in config. */ public function testPluralConfigStrings() { @@ -619,7 +662,7 @@ public function testPluralConfigStrings() { // First import a .po file with multiple plural forms. // This will also automatically add the 'sl' language. $name = tempnam('temporary://', "sl_") . '.po'; - file_put_contents($name, $this->getPoFile()); + file_put_contents($name, $this->getPoFile(4)); $this->drupalPostForm('admin/config/regional/translate/import', array( 'langcode' => 'sl', 'files[file]' => $name, @@ -985,10 +1028,32 @@ protected function assertDisabledTextarea($id) { } /** - * Helper function that returns a .po file with multiple plural forms. + * Helper function that returns a .po file with a given number of plural forms. */ - public function getPoFile() { - return <<< EOF + public function getPoFile($plurals) { + $po_file = array(); + + $po_file[1] = <<< EOF +msgid "" +msgstr "" +"Project-Id-Version: Drupal 8\\n" +"MIME-Version: 1.0\\n" +"Content-Type: text/plain; charset=UTF-8\\n" +"Content-Transfer-Encoding: 8bit\\n" +"Plural-Forms: nplurals=1; plural=0;\\n" +EOF; + + $po_file[2] = <<< EOF +msgid "" +msgstr "" +"Project-Id-Version: Drupal 8\\n" +"MIME-Version: 1.0\\n" +"Content-Type: text/plain; charset=UTF-8\\n" +"Content-Transfer-Encoding: 8bit\\n" +"Plural-Forms: nplurals=2; plural=(n>1);\\n" +EOF; + + $po_file[4] = <<< EOF msgid "" msgstr "" "Project-Id-Version: Drupal 8\\n" @@ -997,6 +1062,8 @@ public function getPoFile() { "Content-Transfer-Encoding: 8bit\\n" "Plural-Forms: nplurals=4; plural=(((n%100)==1)?(0):(((n%100)==2)?(1):((((n%100)==3)||((n%100)==4))?(2):3)));\\n" EOF; + + return $po_file[$plurals]; } }