diff --git a/core/lib/Drupal/Core/Template/TwigNodeTrans.php b/core/lib/Drupal/Core/Template/TwigNodeTrans.php index 98404b602fd4b9b262420253b42b31927f108b85..21006fd1ed382f07797396adad9f233023aab773 100644 --- a/core/lib/Drupal/Core/Template/TwigNodeTrans.php +++ b/core/lib/Drupal/Core/Template/TwigNodeTrans.php @@ -169,6 +169,9 @@ protected function compileString(\Twig_Node $body) { } } } + elseif (!$body->hasAttribute('data')) { + throw new \Twig_Error_Syntax('{% trans %} tag cannot be empty'); + } else { $text = $body->getAttribute('data'); } diff --git a/core/modules/system/src/Tests/Theme/TwigTransTest.php b/core/modules/system/src/Tests/Theme/TwigTransTest.php index 65d10a508ff6afca1dd4695b3ff36cc776a575fe..6473d6e4fcbe7f67f9162d8038f82c9a6306049c 100644 --- a/core/modules/system/src/Tests/Theme/TwigTransTest.php +++ b/core/modules/system/src/Tests/Theme/TwigTransTest.php @@ -91,6 +91,30 @@ public function testTwigTransTags() { $this->assertTwigTransTags(); } + /** + * Test empty Twig "trans" tags. + */ + public function testEmptyTwigTransTags() { + $elements = [ + '#type' => 'inline_template', + '#template' => '{% trans %}{% endtrans %}', + ]; + /** @var \Drupal\Core\Render\RendererInterface $renderer */ + $renderer = \Drupal::service('renderer'); + + try { + $renderer->renderPlain($elements); + + $this->fail('{% trans %}{% endtrans %} did not throw an exception.'); + } + catch (\Twig_Error_Syntax $e) { + $this->assertTrue(strstr($e->getMessage(), '{% trans %} tag cannot be empty'), '{% trans %}{% endtrans %} threw the expected exception.'); + } + catch (\Exception $e) { + $this->fail('{% trans %}{% endtrans %} threw an unexpected exception.'); + } + } + /** * Asserts Twig trans tags. */