Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit562bdff

Browse files
Make trans + %count% parameter resolve plurals
1 parent640a887 commit562bdff

File tree

25 files changed

+696
-404
lines changed

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 injectioninstead.
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()` oneinstead 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()` withintl 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 injectioninstead.
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()` oneinstead 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
useSymfony\Bridge\Twig\TokenParser\TransChoiceTokenParser;
1717
useSymfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser;
1818
useSymfony\Bridge\Twig\TokenParser\TransTokenParser;
19-
useSymfony\Component\Translation\LegacyTranslatorTrait;
2019
useSymfony\Component\Translation\TranslatorInterfaceasLegacyTranslatorInterface;
2120
useSymfony\Contracts\Translation\TranslatorInterface;
2221
useSymfony\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 TranslationExtensionextends AbstractExtension
3435
{
@@ -38,15 +39,17 @@ class TranslationExtension extends AbstractExtension
3839
transasprivate doTrans;
3940
}
4041

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

48-
publicfunction__construct(TranslatorInterface$translator =null,NodeVisitorInterface$translationNodeVisitor =null)
45+
/**
46+
* @param TranslatorInterface|null $translator
47+
*/
48+
publicfunction__construct($translator =null,NodeVisitorInterface$translationNodeVisitor =null)
4949
{
50+
if (null !==$translator && !$translatorinstanceof LegacyTranslatorInterface && !$translatorinstanceof TranslatorInterface) {
51+
thrownew \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
returnarray(
6568
newTwigFilter('trans',array($this,'trans')),
66-
newTwigFilter('transchoice',array($this,'transchoice')),
69+
newTwigFilter('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
publicfunctiontranschoice($message,$count,array$arguments =array(),$domain =null,$locale =null)
114120
{
115-
if (null ===$this->translator || !$this->translatorinstanceof 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->translatorinstanceof 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 =newArrayExpression(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
namespaceSymfony\Bundle\FrameworkBundle\Templating\Helper;
1313

1414
useSymfony\Component\Templating\Helper\Helper;
15-
useSymfony\Component\Translation\LegacyTranslatorTrait;
1615
useSymfony\Component\Translation\TranslatorInterfaceasLegacyTranslatorInterface;
1716
useSymfony\Contracts\Translation\TranslatorInterface;
1817
useSymfony\Contracts\Translation\TranslatorTrait;
@@ -28,14 +27,16 @@ class TranslatorHelper extends Helper
2827
transasprivate doTrans;
2928
}
3029

31-
use LegacyTranslatorTrait {
32-
transChoiceasprivate doTransChoice;
33-
}
34-
3530
protected$translator;
3631

37-
publicfunction__construct(TranslatorInterface$translator =null)
32+
/**
33+
* @param TranslatorInterface $translator
34+
*/
35+
publicfunction__construct($translator =null)
3836
{
37+
if (null !==$translator && !$translatorinstanceof LegacyTranslatorInterface && !$translatorinstanceof TranslatorInterface) {
38+
thrownew \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
publicfunctiontransChoice($id,$number,array$parameters =array(),$domain ='messages',$locale =null)
5860
{
59-
if (null ===$this->translator || !$this->translatorinstanceof 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->translatorinstanceof 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()` withintl message format
8+
* deprecated`Translator::transChoice()` in favor of using`Translator::trans()` witha`%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 DataCollectorTranslatorimplements LegacyTranslatorInterface, TranslatorBagInterface
21+
class DataCollectorTranslatorimplements LegacyTranslatorInterface,TranslatorInterface,TranslatorBagInterface
2222
{
23-
use LegacyTranslatorTrait {
24-
transChoiceasprivate doTransChoice;
25-
}
26-
2723
constMESSAGE_DEFINED =0;
2824
constMESSAGE_MISSING =1;
2925
constMESSAGE_EQUALS_FALLBACK =2;
@@ -38,8 +34,11 @@ class DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorBa
3834
/**
3935
* @param TranslatorInterface $translator The translator must implement TranslatorBagInterface
4036
*/
41-
publicfunction__construct(TranslatorInterface$translator)
37+
publicfunction__construct($translator)
4238
{
39+
if (!$translatorinstanceof LegacyTranslatorInterface && !$translatorinstanceof TranslatorInterface) {
40+
thrownew \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 (!$translatorinstanceof TranslatorBagInterface) {
4443
thrownewInvalidArgumentException(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
publicfunctiontransChoice($id,$number,array$parameters =array(),$domain =null,$locale =null)
6566
{
66-
if ($this->translatorinstanceof 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->translatorinstanceof 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-
privatefunctioncollectMessage($locale,$domain,$id,$translation,$parameters =array(),$number =null)
139+
privatefunctioncollectMessage($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
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp