8000 Make trans + %count% parameter resolve plurals · symfony/symfony@562bdff · GitHub
[go: up one dir, main page]

Skip to content

Commit 562bdff

Browse files
Make trans + %count% parameter resolve plurals
1 parent 640a887 commit 562bdff

25 files changed

+696
-404
lines changed

UPGRADE-4.2.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ FrameworkBundle
108108
serializer:
109109
id: your_messenger_service_id
110110
```
111-
* The `ContainerAwareCommand` class has been deprecated, use `Symfony\Component\Console\Command\Command`
112-
with dependency injection instead.
111+
* The `ContainerAwareCommand` class has been deprecated, use `Symfony\Component\Console\Command\Command` with dependency injection instead.
112+
* The `Templating\Helper\TranslatorHelper::transChoice()` method has been deprecated, use the `trans()` one instead with a `%count%` parameter.
113113

114114
Messenger
115115
---------
@@ -208,9 +208,14 @@ Translation
208208
-----------
209209

210210
* The `TranslatorInterface` has been deprecated in favor of `Symfony\Contracts\Translation\TranslatorInterface`
211-
* The `Translator::transChoice()` has been deprecated in favor of using `Translator::trans()` with intl message format
211+
* The `Translator::transChoice()` has been deprecated in favor of using `Translator::trans()` with "count" as the parameter driving plurals
212212
* The `MessageSelector`, `Interval` and `PluralizationRules` classes have been deprecated, use `IdentityTranslator` instead
213213

214+
TwigBundle
215+
----------
216+
217+
* The `transchoice` tag and filter have been deprecated, use the `trans` ones instead with a `%count%` parameter.
218+
214219
Validator
215220
---------
216221

UPGRADE-5.0.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ FrameworkBundle
114114
* The `RequestDataCollector` class has been removed. Use the `Symfony\Component\HttpKernel\DataCollector\RequestDataCollector` class instead.
115115
* Removed `Symfony\Bundle\FrameworkBundle\Controller\Controller`. Use `Symfony\Bundle\FrameworkBundle\Controller\AbstractController` instead.
116116
* Added support for the SameSite attribute for session cookies. It is highly recommended to set this setting (`framework.session.cookie_samesite`) to `lax` for increased security against CSRF attacks.
117-
* The `ContainerAwareCommand` class has been removed, use `Symfony\Component\Console\Command\Command`
118-
with dependency injection instead.
117+
* The `ContainerAwareCommand` class has been removed, use `Symfony\Component\Console\Command\Command` with dependency injection instead.
118+
* The `Templating\Helper\TranslatorHelper::transChoice()` method has been removed, use the `trans()` one instead with a `%count%` parameter.
119119

120120
HttpFoundation
121121
--------------
@@ -184,11 +184,13 @@ Translation
184184
* The `TranslationWriter::disableBackup()` method has been removed.
185185
* The `TranslatorInterface` has been removed in favor of `Symfony\Contracts\Translation\TranslatorInterface`
186186
* The `MessageSelector`, `Interval` and `PluralizationRules` classes have been removed, use `IdentityTranslator` instead
187+
* The `Translator::transChoice()` method has been removed in favor of using `Translator::trans()` with "count" as the parameter driving plurals
187188

188189
TwigBundle
189190
----------
190191

191192
* The default value (`false`) of the `twig.strict_variables` configuration option has been changed to `%kernel.debug%`.
193+
* The `transchoice` tag and filter have been removed, use the `trans` ones instead with a `%count%` parameter.
192194

193195
Validator
194196
--------

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ CHANGELOG
44
4.2.0
55
-----
66

7-
* add bundle name suggestion on wrongly overridden templates paths
8-
* added `name` argument in `debug:twig` command and changed `filter` argument as `--filter` option
7+
* add bundle name suggestion on wrongly overridden templates paths
8+
* added `name` argument in `debug:twig` command and changed `filter` argument as `--filter` option
9+
* deprecated the `transchoice` tag and filter, use the `trans` ones instead with a `%count%` parameter
910

1011
4.1.0
1112
-----

src/Symfony/Bridge/Twig/Extension/TranslationExtension.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Bridge\Twig\TokenParser\TransChoiceTokenParser;
1717
use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser;
1818
use Symfony\Bridge\Twig\TokenParser\TransTokenParser;
19-
use Symfony\Component\Translation\LegacyTranslatorTrait;
2019
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
2120
use Symfony\Contracts\Translation\TranslatorInterface;
2221
use Symfony\Contracts\Translation\TranslatorTrait;
@@ -29,6 +28,8 @@
2928
* Provides integration of the Translation component with Twig.
3029
*
3130
* @author Fabien Potencier <fabien@symfony.com>
31+
*
32+
* @deprecated since Symfony 4.2, use the "trans" tag with a "%count%" parameter instead
3233
*/
3334
class TranslationExtension extends AbstractExtension
3435
{
@@ -38,15 +39,17 @@ class TranslationExtension extends AbstractExtension
3839
trans as private doTrans;
3940
}
4041

41-
use LegacyTranslatorTrait {
42-
transChoice as private doTransChoice;
43-
}
44-
4542
private $translator;
4643
private $translationNodeVisitor;
4744

48-
public function __construct(TranslatorInterface $translator = null, NodeVisitorInterface $translationNodeVisitor = null)
45+
/**
46+
* @param TranslatorInterface|null $translator
47+
*/
48+
public function __construct($translator = null, NodeVisitorInterface $translationNodeVisitor = null)
4949
{
50+
if (null !== $translator && !$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
51+
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
52+
}
5053
$this->translator = $translator;
5154
$this->translationNodeVisitor = $translationNodeVisitor;
5255
}
@@ -63,7 +66,7 @@ public function getFilters()
6366
{
6467
return array(
6568
new TwigFilter('trans', array($this, 'trans')),
66-
new TwigFilter('transchoice', array($this, 'transchoice')),
69+
new TwigFilter('transchoice', array($this, 'transchoice'), array('deprecated' => '4.2', 'alternative' => 'trans with a %count% parameter')),
6770
);
6871
}
6972

@@ -110,10 +113,16 @@ public function trans($message, array $arguments = array(), $domain = null, $loc
110113
return $this->translator->trans($message, $arguments, $domain, $locale);
111114
}
112115

116+
/**
117+
* @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter
118+
*/
113119
public function transchoice($message, $count, array $arguments = array(), $domain = null, $locale = null)
114120
{
115-
if (null === $this->translator || !$this->translator instanceof LegacyTranslatorInterface) {
116-
return $this->doTransChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
121+
if (null === $this->translator) {
122+
return $this->doTrans($message, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
123+
}
124+
if ($this->translator instanceof TranslatorInterface) {
125+
return $this->translator->trans($message, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
117126
}
118127

119128
return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);

src/Symfony/Bridge/Twig/TokenParser/TransChoiceTokenParser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public function parse(Token $token)
3838
$lineno = $token->getLine();
3939
$stream = $this->parser->getStream();
4040

41+
@trigger_error(sprintf('The "transchoice" tag is deprecated since Symfony 4.2, use the "trans" one instead with a "%count%" parameter in %s line %d.', $stream->getSourceContext()->getName(), $lineno), E_USER_DEPRECATED);
42+
4143
$vars = new ArrayExpression(array(), $lineno);
4244

4345
$count = $this->parser->getExpressionParser()->parseExpression();

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CHANGELOG
1414
* Removed the `framework.messenger.encoder` and `framework.messenger.decoder` options. Use the `framework.messenger.serializer.id` option to replace the Messenger serializer.
1515
* Deprecated the `ContainerAwareCommand` class in favor of `Symfony\Component\Console\Command\Command`
1616
* Made `debug:container` and `debug:autowiring` ignore backslashes in service ids
17+
* Deprecated the `Templating\Helper\TranslatorHelper::transChoice()` method, use the `trans()` one instead with a `%count%` parameter
1718

1819
4.1.0
1920
-----

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/TranslatorHelper.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Templating\Helper;
1313

1414
use Symfony\Component\Templating\Helper\Helper;
15-
use Symfony\Component\Translation\LegacyTranslatorTrait;
1615
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
1716
use Symfony\Contracts\Translation\TranslatorInterface;
1817
use Symfony\Contracts\Translation\TranslatorTrait;
@@ -28,14 +27,16 @@ class TranslatorHelper extends Helper
2827
trans as private doTrans;
2928
}
3029

31-
10000 use LegacyTranslatorTrait {
32-
transChoice as private doTransChoice;
33-
}
34-
3530
protected $translator;
3631

37-
public function __construct(TranslatorInterface $translator = null)
32+
/**
33+
* @param TranslatorInterface $translator
34+
*/
35+
public function __construct($translator = null)
3836
{
37+
if (null !== $translator && !$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
38+
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
39+
}
3940
$this->translator = $translator;
4041
}
4142

@@ -53,11 +54,17 @@ public function trans($id, array $parameters = array(), $domain = 'messages', $l
5354

5455
/**
5556
* @see TranslatorInterface::transChoice()
57+
* @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter
5658
*/
5759
public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
5860
{
59-
if (null === $this->translator || !$this->translator instanceof LegacyTranslatorInterface) {
60-
return $this->doTransChoice($id, $number, $parameters, $domain, $locale);
61+
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the trans() one instead with a "%count%" parameter.', __METHOD__), E_USER_DEPRECATED);
62+
63+
if (null === $this->translator) {
64+
return $this->doTrans($id, array('%count%' => $number) + $parameters, $domain, $locale);
65+
}
66+
if ($this->translator instanceof TranslatorInterface) {
67+
return $this->translator->trans($id, array('%count%' => $number) + $parameters, $domain, $locale);
6168
}
6269

6370
return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);

src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ public function testTransWithoutCaching()
5252
$this->assertEquals('foo (FR)', $translator->trans('foo'));
5353
$this->assertEquals('bar (EN)', $translator->trans('bar'));
5454
$this->assertEquals('foobar (ES)', $translator->trans('foobar'));
55+
$this->assertEquals('choice 0 (EN)', $translator->trans('choice', array('%count%' => 0)));
5556
$this->assertEquals('no translation', $translator->trans('no translation'));
5657
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
58+
$this->assertEquals('other choice 1 (PT-BR)', $translator->trans('other choice', array('%count%' => 1)));
5759
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
5860
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
5961
}
@@ -81,8 +83,10 @@ public function testTransWithCaching()
8183
$this->assertEquals('foo (FR)', $translator->trans('foo'));
8284
$this->assertEquals('bar (EN)', $translator->trans('bar'));
8385
$this->assertEquals('foobar (ES)', $translator->trans('foobar'));
86+
$this->assertEquals('choice 0 (EN)', $translator->trans('choice', array('%count%' => 0)));
8487
$this->assertEquals('no translation', $translator->trans('no translation'));
8588
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
89+
$this->assertEquals('other choice 1 (PT-BR)', $translator->trans('other choice', array('%count%' => 1)));
8690
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
8791
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
8892

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CHANGELOG
55
-----
66

77
* Started using ICU parent locales as fallback locales.
8-
* deprecated `Translator::transChoice()` in favor of using `Translator::trans()` with intl message format
8+
* deprecated `Translator::transChoice()` in favor of using `Translator::trans()` with a `%count%` parameter
99
* deprecated `TranslatorInterface` in favor of `Symfony\Contracts\Translation\TranslatorInterface`
1010
* deprecated `MessageSelector`, `Interval` and `PluralizationRules`; use `IdentityTranslator` instead
1111
* Added `IntlMessageFormatter` and `FallbackMessageFormatter`

src/Symfony/Component/Translation/DataCollectorTranslator.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@
1818
/**
1919
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
2020
*/
21-
class DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorBagInterface
21+
class DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorInterface, TranslatorBagInterface
2222
{
23-
use LegacyTranslatorTrait {
24-
transChoice as private doTransChoice;
25-
}
26-
2723
const MESSAGE_DEFINED = 0;
2824
const MESSAGE_MISSING = 1;
2925
const MESSAGE_EQUALS_FALLBACK = 2;
@@ -38,8 +34,11 @@ class DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorBa
3834
/**
3935
* @param TranslatorInterface $translator The translator must implement TranslatorBagInterface
4036
*/
41-
public function __construct(TranslatorInterface $translator)
37+
public function __construct($translator)
4238
{
39+
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
40+
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
41+
}
4342
if (!$translator instanceof TranslatorBagInterface) {
4443
throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', \get_class($translator)));
4544
}
@@ -60,16 +59,18 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
6059

6160
/**
6261
* {@inheritdoc}
62+
*
63+
* @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter
6364
*/
6465
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
6566
{
66-
if ($this->translator instanceof LegacyTranslatorInterface) {
67-
$trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
68-
} else {
69-
$trans = $this->doTransChoice($id, $number, $parameters, $domain, $locale);
67+
if ($this->translator instanceof TranslatorInterface) {
68+
$trans = $this->translator->trans($id, array('%count%' => $number) + $parameters, $domain, $locale);
7069
}
7170

72-
$this->collectMessage($locale, $domain, $id, $trans, $parameters, $number);
71+
$trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
72+
73+
$this->collectMessage($locale, $domain, $id, $trans, array('%count%' => $number) + $parameters);
7374

7475
return $trans;
7576
}
@@ -134,9 +135,8 @@ public function getCollectedMessages()
134135
* @param string $id
135136
* @param string $translation
136137
* @param array|null $parameters
137-
* @param int|null $number
138138
*/
139-
private function collectMessage($locale, $domain, $id, $translation, $parameters = array(), $number = null)
139+
private function collectMessage($locale, $domain, $id, $translation, $parameters = array())
140140
{
141141
if (null === $domain) {
142142
$domain = 'messages';
@@ -169,8 +169,8 @@ private function collectMessage($locale, $domain, $id, $translation, $parameters
169169
'id' => $id,
170170
'translation' => $translation,
171171
'parameters' => $parameters,
172-
'transChoiceNumber' => $number,
173172
'state' => $state,
173+
'transChoiceNumber' => isset($parameters['%count%']) && is_numeric($parameters['%count%']) ? $parameters['%count%'] : null,
174174
);
175175
}
176176
}

src/Symfony/Component/Translation/Formatter/ChoiceMessageFormatterInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
/**
1515
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
16+
*
17+
* @deprecated since Symfony 4.2, use MessageFormatterInterface::format() with a %count% parameter instead
1618
*/
1719
interface ChoiceMessageFormatterInterface
1820
{

src/Symfony/Component/Translation/Formatter/MessageFormatter.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Translation\IdentityTranslator;
1515
use Symfony\Component\Translation\MessageSelector;
16+
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
1617
use Symfony\Contracts\Translation\TranslatorInterface;
1718

1819
/**
@@ -29,7 +30,7 @@ public function __construct($translator = null)
2930
{
3031
if ($translator instanceof MessageSelector) {
3132
$translator = new IdentityTranslator( 10000 $translator);
32-
} elseif (null !== $translator && !$translator instanceof TranslatorInterface) {
33+
} elseif (null !== $translator && !$translator instanceof TranslatorInterface && !$translator instanceof LegacyTranslatorInterface) {
3334
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
3435
}
3536

@@ -41,15 +42,27 @@ public function __construct($translator = null)
4142
*/
4243
public function format($message, $locale, array $parameters = array())
4344
{
45+
if ($this->translator instanceof TranslatorInterface) {
46+
return $this->translator->trans($message, $parameters, null, $locale);
47+
}
48+
4449
return strtr($message, $parameters);
4550
}
4651

4752
/**
4853
* {@inheritdoc}
54+
*
55+
* @deprecated since Symfony 4.2, use format() with a %count% parameter instead
4956
*/
5057
public function choiceFormat($message, $number, $locale, array $parameters = array())
5158
{
52-
$parameters = array_merge(array('%count%' => $number), $parameters);
59+
@trigger_error(sprintf('The %s() method is deprecated since Symfony 4.2, use the format() one instead with a %count% parameter.', __METHOD__), E_USER_DEPRECATED);
60+
61+
$parameters = array('%count%' => $number) + $parameters;
62+
63+
if ($this->translator instanceof TranslatorInterface) {
64+
return $this->format($message, $locale, $parameters);
65+
}
5366

5467
return $this->format($this->translator->transChoice($message, $number, array(), null, $locale), $locale, $parameters);
5568
}

0 commit comments

Comments
 (0)
0