removed the possibility to pass a message to the trans tag

The trans tag should only be used with static texts as automatic output escaping does not occur.
This commit is contained in:
Fabien Potencier 2011-04-21 09:04:16 +02:00
parent cad6643e77
commit 286c45733e
4 changed files with 22 additions and 23 deletions

View File

@ -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:

View File

@ -53,7 +53,7 @@ class TranslationExtension extends \Twig_Extension
public function getTokenParsers()
{
return array(
// {% trans "Symfony is great!" %}
// {% trans %}Symfony is great!{% endtrans %}
new TransTokenParser(),
// {% transchoice count %}

View File

@ -36,20 +36,14 @@ class TransTokenParser extends \Twig_TokenParser
$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)) {
@ -57,11 +51,9 @@ class TransTokenParser extends \Twig_TokenParser
}
}
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');

View File

@ -47,16 +47,9 @@ class TranslationExtensionTest extends TestCase
{
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')),