diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/lib/Drupal/Component/Gettext/PoItem.php | 2 | ||||
-rw-r--r-- | core/tests/Drupal/Tests/Component/Gettext/PoItemTest.php | 85 |
2 files changed, 86 insertions, 1 deletions
diff --git a/core/lib/Drupal/Component/Gettext/PoItem.php b/core/lib/Drupal/Component/Gettext/PoItem.php index 7cc32568ad23..89dee6cc0858 100644 --- a/core/lib/Drupal/Component/Gettext/PoItem.php +++ b/core/lib/Drupal/Component/Gettext/PoItem.php @@ -271,7 +271,7 @@ class PoItem { private function formatSingular() { $output = ''; $output .= 'msgid ' . $this->formatString($this->source); - $output .= 'msgstr ' . (isset($this->translation) ? $this->formatString($this->translation) : '""'); + $output .= 'msgstr ' . (isset($this->translation) ? $this->formatString($this->translation) : '""' . "\n"); return $output; } diff --git a/core/tests/Drupal/Tests/Component/Gettext/PoItemTest.php b/core/tests/Drupal/Tests/Component/Gettext/PoItemTest.php new file mode 100644 index 000000000000..7026a2ceac5a --- /dev/null +++ b/core/tests/Drupal/Tests/Component/Gettext/PoItemTest.php @@ -0,0 +1,85 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Tests\Component\Gettext; + +use Drupal\Component\Gettext\PoItem; +use PHPUnit\Framework\TestCase; + +/** + * @coversDefaultClass \Drupal\Component\Gettext\PoItem + * @group Gettext + */ +class PoItemTest extends TestCase { + + /** + * @return array + * - Source string + * - Context (optional) + * - Translated string (optional) + * - Expected value + */ + public static function providerStrings(): array { + // cSpell:disable + return [ + [ + '', + NULL, + NULL, + 'msgid ""' . "\n" . 'msgstr ""' . "\n\n", + ], + // Translated String without contesxt. + [ + 'Next', + NULL, + 'Suivant', + 'msgid "Next"' . "\n" . 'msgstr "Suivant"' . "\n\n", + ], + // Translated string with context. + [ + 'Apr', + 'Abbreviated month name', + 'Avr', + 'msgctxt "Abbreviated month name"' . "\n" . 'msgid "Apr"' . "\n" . 'msgstr "Avr"' . "\n\n", + ], + // Translated string with placeholder. + [ + '%email is not a valid email address.', + NULL, + '%email n\'est pas une adresse de courriel valide.', + 'msgid "%email is not a valid email address."' . "\n" . 'msgstr "%email n\'est pas une adresse de courriel valide."' . "\n\n", + ], + // Translated Plural String without context. + [ + ['Installed theme', 'Installed themes'], + NULL, + ['Thème installé', 'Thèmes installés'], + 'msgid "Installed theme"' . "\n" . 'msgid_plural "Installed themes"' . "\n" . 'msgstr[0] "Thème installé"' . "\n" . 'msgstr[1] "Thèmes installés"' . "\n\n", + ], + ]; + // cSpell:enable + } + + /** + * @dataProvider providerStrings + */ + public function testFormat($source, $context, $translation, $expected): void { + $item = new PoItem(); + + $item->setSource($source); + + if (is_array($source)) { + $item->setPlural(TRUE); + } + if (!empty($context)) { + $item->setContext($context); + } + if (!empty($translation)) { + $item->setTranslation($translation); + } + + $this->assertEquals($expected, (string) $item); + } + +} |