From 41db242cf0fffacc1479a1876c4bf49a55977af0 Mon Sep 17 00:00:00 2001 From: Mark Carver Date: Wed, 24 Jul 2013 18:18:36 -0500 Subject: Issue #2049241 by Mark Carver, penyaskito: Context support for the Twig {% trans %} tag extension --- core/lib/Drupal/Core/Template/TwigNodeTrans.php | 28 +++++++++++++++------- .../Drupal/Core/Template/TwigTransTokenParser.php | 13 +++++++--- .../Drupal/system/Tests/Theme/TwigTransTest.php | 20 ++++++++++++++++ .../templates/twig_theme_test.trans.html.twig | 19 +++++++++++++++ 4 files changed, 69 insertions(+), 11 deletions(-) diff --git a/core/lib/Drupal/Core/Template/TwigNodeTrans.php b/core/lib/Drupal/Core/Template/TwigNodeTrans.php index ee32e98..bd704c7 100644 --- a/core/lib/Drupal/Core/Template/TwigNodeTrans.php +++ b/core/lib/Drupal/Core/Template/TwigNodeTrans.php @@ -22,11 +22,12 @@ class TwigNodeTrans extends \Twig_Node { /** * {@inheritdoc} */ - public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $plural = NULL, \Twig_Node_Expression $count = NULL, $lineno, $tag = NULL) { + 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( 'count' => $count, 'body' => $body, - 'plural' => $plural + 'plural' => $plural, + 'context' => $context, ), array(), $lineno, $tag); } @@ -36,6 +37,8 @@ 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')); $plural = NULL; @@ -60,12 +63,18 @@ public function compile(\Twig_Compiler $compiler) { $compiler->raw(', ')->subcompile($plural); } - // Write any tokens found as an associative array parameter. - if (!empty($tokens)) { - $compiler->raw(', array('); - foreach ($tokens as $token) { - $compiler->string($token->getAttribute('placeholder'))->raw(' => ')->subcompile($token)->raw(', '); - } + // 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(')'); + + // Write the array with the context to pass, if necessary. + if (!empty($context)) { + $compiler->raw(', array("context" => '); + $compiler->subcompile($context); $compiler->raw(')'); } @@ -79,6 +88,9 @@ public function compile(\Twig_Compiler $compiler) { if (!empty($plural)) { $compiler->raw(', PLURAL: ')->subcompile($plural); } + if (!empty($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 44f6d21..3d64311 100644 --- a/core/lib/Drupal/Core/Template/TwigTransTokenParser.php +++ b/core/lib/Drupal/Core/Template/TwigTransTokenParser.php @@ -27,13 +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; - if (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) { + if (!$stream->test(\Twig_Token::BLOCK_END_TYPE) && $stream->test(\Twig_Token::STRING_TYPE)) { $body = $this->parser->getExpressionParser()->parseExpression(); } - else { + 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 (!$body) { $stream->expect(\Twig_Token::BLOCK_END_TYPE); $body = $this->parser->subparse(array($this, 'decideForFork')); if ('plural' === $stream->next()->getValue()) { @@ -47,7 +54,7 @@ public function parse(\Twig_Token $token) { $this->checkTransString($body, $lineno); - $node = new TwigNodeTrans($body, $plural, $count, $lineno, $this->getTag()); + $node = new TwigNodeTrans($body, $plural, $count, $context, $lineno, $this->getTag()); return $node; } 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 a1ae6d8..12496c3 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Theme/TwigTransTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Theme/TwigTransTest.php @@ -160,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); } @@ -192,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) { @@ -246,6 +258,10 @@ 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." @@ -268,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 f487e5a..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 #} +
+ {% trans 'Hello sun.' context = 'Lolspeak' %} +
+ {# Test trans filter. #}
{{ 'Hello Earth.'|trans }} @@ -61,3 +66,17 @@ This {{ token.name }} has a length of: {{ count }}. It contains: {{ token.numbers|placeholder }} and {{ token.bad_text }}. Lets pass the bad text through: {{ token.bad_text|passthrough }}. {% endtrans %}
+ +{# Test trans tag but with a context only msgid. #} +
+ {% trans %} + I have context. + {% endtrans %} +
+ +{# Test trans tag with context. #} +
+ {% trans context = 'Lolspeak' %} + I have context. + {% endtrans %} +
-- 1.8.2