Skip to content

Commit

Permalink
removed the possibility to pass a message to the trans tag
Browse files Browse the repository at this point in the history
The trans tag should only be used with static texts as automatic output escaping does not occur.
  • Loading branch information
fabpot committed Apr 21, 2011
1 parent cad6643 commit 286c457
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
18 changes: 16 additions & 2 deletions UPDATE.md
Expand Up @@ -6,8 +6,22 @@ one. It only discusses changes that need to be done when using the "public"
API of the framework. If you "hack" the core, you should probably follow the
timeline closely anyway.

PR12 to PR13
------------
PR12 to beta1
-------------

* The `trans` tag does not accept a message as an argument anymore:

{% trans "foo" %}
{% trans foo %}

Use the long version the tags or the filter instead:

{% trans %}foo{% endtrans %}
{{ foo|trans }}

This has been done to clarify the usage of the tag and filter and also to
make it clearer when the automatic output escaping rules are applied (see
the doc for more information).

* Some methods in the DependencyInjection component's ContainerBuilder and
Definition classes have been renamed to be more specific and consistent:
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/Extension/TranslationExtension.php
Expand Up @@ -53,7 +53,7 @@ public function getFilters()
public function getTokenParsers()
{
return array(
// {% trans "Symfony is great!" %}
// {% trans %}Symfony is great!{% endtrans %}
new TransTokenParser(),

// {% transchoice count %}
Expand Down
18 changes: 5 additions & 13 deletions src/Symfony/Bridge/Twig/TokenParser/TransTokenParser.php
Expand Up @@ -36,32 +36,24 @@ public function parse(\Twig_Token $token)
$vars = new \Twig_Node_Expression_Array(array(), $lineno);
$domain = new \Twig_Node_Expression_Constant('messages', $lineno);
if (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
if (!$stream->test('from') && !$stream->test('with')) {
// {% trans "message" %}
// {% trans message %}
$body = $this->parser->getExpressionParser()->parseExpression();
}

if ($stream->test('with')) {
// {% trans "message" with vars %}
// {% trans with vars %}
$stream->next();
$vars = $this->parser->getExpressionParser()->parseExpression();
}

if ($stream->test('from')) {
// {% trans "message" from "messages" %}
// {% trans from "messages" %}
$stream->next();
$domain = $this->parser->getExpressionParser()->parseExpression();
} elseif (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
throw new \Twig_Error_Syntax('Unexpected token. Twig was looking for the "with" or "from" keyword.');
}
}

if (null === $body) {
// {% trans %}message{% endtrans %}
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(array($this, 'decideTransFork'), true);
}
// {% trans %}message{% endtrans %}
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(array($this, 'decideTransFork'), true);

if (!$body instanceof \Twig_Node_Text && !$body instanceof \Twig_Node_Expression) {
throw new \Twig_Error_Syntax('A message must be a simple text');
Expand Down
Expand Up @@ -47,16 +47,9 @@ public function getTransTests()
{
return array(
// trans tag
array('{% trans "Hello" %}', 'Hello'),
array('{% trans "Hello %name%" %}', 'Hello Symfony2', array('name' => 'Symfony2')),
array('{% trans name %}', 'Symfony2', array('name' => 'Symfony2')),
array('{% trans hello with { \'%name%\': \'Symfony2\' } %}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
array('{% set vars = { \'%name%\': \'Symfony2\' } %}{% trans hello with vars %}', 'Hello Symfony2', array('hello' => 'Hello %name%')),

array('{% trans %}Hello{% endtrans %}', 'Hello'),
array('{% trans %}%name%{% endtrans %}', 'Symfony2', array('name' => 'Symfony2')),

array('{% trans "Hello" from elsewhere %}', 'Hello'),
array('{% trans from elsewhere %}Hello{% endtrans %}', 'Hello'),

array('{% trans %}Hello %name%{% endtrans %}', 'Hello Symfony2', array('name' => 'Symfony2')),
Expand Down

1 comment on commit 286c457

@willdurand
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should update form.html.twig in the TwigBundle and maybe other templates.

Please sign in to comment.