diff --git a/core/lib/Drupal/Core/Template/TwigNodeTrans.php b/core/lib/Drupal/Core/Template/TwigNodeTrans.php index 4842e74..bd704c7 100644 --- a/core/lib/Drupal/Core/Template/TwigNodeTrans.php +++ b/core/lib/Drupal/Core/Template/TwigNodeTrans.php @@ -24,10 +24,10 @@ class TwigNodeTrans extends \Twig_Node { */ public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $plural = NULL, \Twig_Node_Expression $count = NULL, \Twig_Node_Expression $context = NULL, $lineno, $tag = NULL) { parent::__construct(array( - 'context' => $context, 'count' => $count, 'body' => $body, - 'plural' => $plural + 'plural' => $plural, + 'context' => $context, ), array(), $lineno, $tag); } @@ -36,6 +36,7 @@ public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $plur */ public function compile(\Twig_Compiler $compiler) { $compiler->addDebugInfo($this); + $context = $this->getNode('context'); list($singular, $tokens) = $this->compileString($this->getNode('body')); @@ -62,20 +63,21 @@ public function compile(\Twig_Compiler $compiler) { $compiler->raw(', ')->subcompile($plural); } - // Write any tokens found as an associative array parameter, empty array if - // none are found. + // Write any tokens found as an associative array parameter, otherwise just + // leave as an empty array. $compiler->raw(', array('); foreach ($tokens as $token) { $compiler->string($token->getAttribute('placeholder'))->raw(' => ')->subcompile($token)->raw(', '); } $compiler->raw(')'); - // Add the context to the string if any. + // Write the array with the context to pass, if necessary. if (!empty($context)) { $compiler->raw(', array("context" => '); $compiler->subcompile($context); $compiler->raw(')'); } + // Write function closure. $compiler->raw(')'); @@ -87,7 +89,7 @@ public function compile(\Twig_Compiler $compiler) { $compiler->raw(', PLURAL: ')->subcompile($plural); } if (!empty($context)) { - $compiler->raw(', with CONTEXT: ')->subcompile($context); + $compiler->raw(', CONTEXT: ')->subcompile($context); } $compiler->raw(" -->\n'"); } diff --git a/core/lib/Drupal/Core/Template/TwigTransTokenParser.php b/core/lib/Drupal/Core/Template/TwigTransTokenParser.php index 737e7fb..3d64311 100644 --- a/core/lib/Drupal/Core/Template/TwigTransTokenParser.php +++ b/core/lib/Drupal/Core/Template/TwigTransTokenParser.php @@ -27,18 +27,20 @@ class TwigTransTokenParser extends \Twig_TokenParser { public function parse(\Twig_Token $token) { $lineno = $token->getLine(); $stream = $this->parser->getStream(); + $body = NULL; + $context = NULL; $count = NULL; $plural = NULL; - $context = NULL; - if ($stream->test(\Twig_Token::NAME_TYPE, 'context')) { + if (!$stream->test(\Twig_Token::BLOCK_END_TYPE) && $stream->test(\Twig_Token::STRING_TYPE)) { + $body = $this->parser->getExpressionParser()->parseExpression(); + } + if (!$stream->test(\Twig_Token::BLOCK_END_TYPE) && $stream->test(\Twig_Token::NAME_TYPE, 'context')) { $stream->next(); + $stream->expect(\Twig_Token::OPERATOR_TYPE, '='); $context = $this->parser->getExpressionParser()->parseExpression(); } - if (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) { - $body = $this->parser->getExpressionParser()->parseExpression(); - } - else { + if (!$body) { $stream->expect(\Twig_Token::BLOCK_END_TYPE); $body = $this->parser->subparse(array($this, 'decideForFork')); if ('plural' === $stream->next()->getValue()) { diff --git a/core/modules/system/lib/Drupal/system/Tests/Theme/TwigTransTest.php b/core/modules/system/lib/Drupal/system/Tests/Theme/TwigTransTest.php index 2c60985..12496c3 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Theme/TwigTransTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Theme/TwigTransTest.php @@ -131,11 +131,6 @@ public function testTwigTransTags() { ); $this->assertText( - 'OH HAI TEH MUUNZZZZZZZ', - '{% trans context "more zzz" %}Hello moon.{% endtrans %} was successfully translated.' - ); - - $this->assertText( 'O HAI STARRRRR', '{% trans %} with {% plural count = 1 %} was successfully translated.' ); @@ -165,6 +160,16 @@ public function testTwigTransTags() { '{{ complex.tokens }} were successfully translated with appropriate prefixes.' ); + $this->assertText( + 'I have context.', + '{% trans %} with a context only msgid was excluded from translation.' + ); + + $this->assertText( + 'I HAZ KONTEX.', + '{% trans context = "Lolspeak" %} was successfully translated with context.' + ); + // Ensure debug output does not print. $this->checkForDebugMarkup(FALSE); } @@ -197,12 +202,14 @@ public function testTwigTransDebug() { protected function checkForDebugMarkup($visible) { $tests = array( '{% trans "Hello sun." %}' => '', + '{% trans "Hello sun." context = "Lolspeak" %}' => '', '{{ "Hello moon."|trans }}' => '', '{% trans %} with {% plural %}' => '', '{{ token }}' => '', '{{ token|passthrough }}' => '', '{{ token|placeholder }}' => '', '{{ complex.tokens }}' => '', + '{% trans context = "Lolspeak" %}I have context.{% endtrans %}' => '', ); foreach ($tests as $test => $markup) { if ($visible) { @@ -251,16 +258,16 @@ protected function examplePoFile() { msgid "Hello sun." msgstr "OH HAI SUNZ" +msgctxt "Lolspeak" +msgid "Hello sun." +msgstr "OH HAI SUNZZZZZZZ" + msgid "Hello Earth." msgstr "O HERRO ERRRF." msgid "Hello moon." msgstr "OH HAI TEH MUUN" -msgctxt "more zzz" -msgid "Hello moon." -msgstr "OH HAI TEH MUUNZZZZZZZ" - msgid "Hello star." msgid_plural "Hello @count stars." msgstr[0] "O HAI STARRRRR" @@ -277,6 +284,10 @@ protected function examplePoFile() { msgid "This @name has a length of: @count. It contains: %numbers and @bad_text. Lets pass the bad text through: !bad_text." msgstr "DIS @name HAZ LENGTH OV: @count. IT CONTAYNZ: %numbers AN @bad_text. LETS PAS TEH BAD TEXT THRU: !bad_text." + +msgctxt "Lolspeak" +msgid "I have context." +msgstr "I HAZ KONTEX." EOF; } diff --git a/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.trans.html.twig b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.trans.html.twig index dcac7e2..1d1adce 100644 --- a/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.trans.html.twig +++ b/core/modules/system/tests/modules/twig_theme_test/templates/twig_theme_test.trans.html.twig @@ -3,6 +3,11 @@ {% trans 'Hello sun.' %} +{# Test trans tag with string argument and context #} +