8000 [Translator] Deprecated transChoice and moved it away from contracts · symfony/symfony@e014558 · GitHub
[go: up one dir, main page]

Skip to content

Commit e014558

Browse files
committed
[Translator] Deprecated transChoice and moved it away from contracts
1 parent c51592c commit e014558

File tree

15 files changed

+343
-346
lines changed

15 files changed

+343
-346
lines changed

UPGRADE-4.2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ Translation
199199
-----------
200200

201201
* The `TranslatorInterface` has been deprecated in favor of `Symfony\Contracts\Translation\TranslatorInterface`
202+
* The `Translator::transChoice()` has been deprecated in favor of using `Translator::trans()` with intl message format
202203
* The `MessageSelector`, `Interval` and `PluralizationRules` classes have been deprecated, use `IdentityTranslator` instead
203204

204205
Validator

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

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,25 @@ 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->transChoice('choice', 0));
5655
$this->assertEquals('no translation', $translator->trans('no translation'));
5756
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
58-
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
5957
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
6058
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
6159
}
6260

61+
/**
62+
* @group legacy
63+
*/
64+
public function testTransChoiceWithoutCaching()
65+
{
66+
$translator = $this->getTranslator($this->getLoader());
67+
$translator->setLocale('fr');
68+
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin'));
69+
70+
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0));
71+
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
72+
}
73+
6374
public function testTransWithCaching()
6475
{
6576
// prime the cache
@@ -70,10 +81,8 @@ public function testTransWithCaching()
7081
$this->assertEquals('foo (FR)', $translator->trans('foo'));
7182
$this->assertEquals('bar (EN)', $translator->trans('bar'));
7283
$this->assertEquals('foobar (ES)', $translator->trans('foobar'));
73-
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0));
7484
$this->assertEquals('no translation', $translator->trans('no translation'));
7585
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
76-
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
7786
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
7887
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
7988

@@ -88,14 +97,37 @@ public function testTransWithCaching()
8897
$this->assertEquals('foo (FR)', $translator->trans('foo'));
8998
$this->as A3E2 sertEquals('bar (EN)', $translator->trans('bar'));
9099
$this->assertEquals('foobar (ES)', $translator->trans('foobar'));
91-
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0));
92100
$this->assertEquals('no translation', $translator->trans('no translation'));
93101
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
94-
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
95102
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
96103
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
97104
}
98105

106+
/**
107+
* @group legacy
108+
*/
109+
public function testTransChoiceWithCaching()
110+
{
111+
// prime the cache
112+
$translator = $this->getTranslator($this->getLoader(), array('cache_dir' => $this->tmpDir));
113+
$translator->setLocale('fr');
114+
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin'));
115+
116+
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0));
117+
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
118+
119+
// do it another time as the cache is primed now
120+
$loader = $this->getMockBuilder('Symfony\Component\Translation\Loader\LoaderInterface')->getMock();
121+
$loader->expects($this->never())->method('load');
122+
123+
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir));
124+
$translator->setLocale('fr');
125+
$translator->setFallbackLocales(array('en', 'es', 'pt-PT', 'pt_BR', 'fr.UTF-8', 'sr@latin'));
126+
127+
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0));
128+
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
129+
}
130+
99131
/**
100132
* @expectedException \InvalidArgumentException
101133
* @expectedExceptionMessage Invalid "invalid locale" locale.

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +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
89
* deprecated `TranslatorInterface` in favor of `Symfony\Contracts\Translation\TranslatorInterface`
910
* deprecated `MessageSelector`, `Interval` and `PluralizationRules`; use `IdentityTranslator` instead
1011
* Added `IntlMessageFormatter` and `FallbackMessageFormatter`

src/Symfony/Component/Translation/IdentityTranslator.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
*/
2121
class IdentityTranslator implements TranslatorInterface
2222
{
23-
use TranslatorTrait {
24-
transChoice as private doTransChoice;
25-
}
23+
use TranslatorTrait;
2624

2725
private $selector;
2826

@@ -33,7 +31,7 @@ public function __construct(MessageSelector $selector = null)
3331
{
3432
$this->selector = $selector;
3533

36-
if (\get_class($this) !== __CLASS__) {
34+
if (__CLASS__ !== \get_class($this)) {
3735
@trigger_error(sprintf('Calling "%s()" is deprecated since Symfony 4.2.'), E_USER_DEPRECATED);
3836
}
3937
}
@@ -43,15 +41,11 @@ public function __construct(MessageSelector $selector = null)
4341
*/
4442
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
4543
{
46-
if ($this->selector) {
47-
return strtr($this->selector->choose((string) $id, (int) $number, $locale ?: $this->getLocale()), $parameters);
44+
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the "trans()" method with intl formatted messages instead.', __METHOD__), E_USER_DEPRECATED);
45+
if (null === $this->selector) {
46+
$this->selector = new MessageSelector();
4847
}
4948

50-
return $this->doTransChoice($id, $number, $parameters, $domain, $locale);
51-
}
52-
53-
private function getPluralizationRule(int $number, string $locale): int
54-
{
55-
return PluralizationRules::get($number, $locale, false);
49+
return strtr($this->selector->choose((string) $id, $number, $locale ?: $this->getLocale()), $parameters);
5650
}
5751
}

src/Symfony/Component/Translation/MessageSelector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ class MessageSelector
4343
* The two methods can also be mixed:
4444
* {0} There are no apples|one: There is one apple|more: There are %count% apples
4545
*
46-
* @param string $message The message being translated
47-
* @param int $number The number of items represented for the message
48-
* @param string $locale The locale to use for choosing
46+
* @param string $message The message being translated
47+
* @param int|float $number The number of items represented for the message
48+
* @param string $locale The locale to use for choosing
4949
*
5050
* @return string
5151
*

src/Symfony/Component/Translation/Tests/DataCollectorTranslatorTest.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public function testCollectMessages()
2525

2626
$collector->trans('foo');
2727
$collector->trans('bar');
28-
$collector->transChoice('choice', 0);
2928
$collector->trans('bar_ru');
3029
$collector->trans('bar_ru', array('foo' => 'bar'));
3130

@@ -48,15 +47,6 @@ public function testCollectMessages()
4847
'parameters' => array(),
4948
'transChoiceNumber' => null,
5049
);
51-
$expectedMessages[] = array(
52-
'id' => 'choice',
53-
'translation' => 'choice',
54-
'locale' => 'en',
55-
'domain' => 'messages',
56-
'state' => DataCollectorTranslator::MESSAGE_MISSING,
57-
'parameters' => array(),
58-
'transChoiceNumber' => 0,
59-
);
6050
$expectedMessages[] = array(
6151
'id' => 'bar_ru',
6252
'translation' => 'bar (ru)',
@@ -79,6 +69,30 @@ public function testCollectMessages()
7969
$this->assertEquals($expectedMessages, $collector->getCollectedMessages());
8070
}
8171

72+
/**
73+
* @group legacy
74+
*/
75+
public function testCollectMessagesTransChoice()
76+
{
77+
$collector = $this->createCollector();
78+
$collector->setFallbackLocales(array('fr', 'ru'));
79+
$collector->transChoice('choice', 0);
80+
81+
$expectedMessages = array();
82+
83+
$expectedMessages[] = array(
84+
'id' => 'choice',
85+
'translation' => 'choice',
86+
'locale' => 'en',
87+
'domain' => 'messages',
88+
'state' => DataCollectorTranslator::MESSAGE_MISSING,
89+
'parameters' => array(),
90+
'transChoiceNumber' => 0,
91+
);
92+
93+
$this->assertEquals($expectedMessages, $collector->getCollectedMessages());
94+
}
95+
8296
private function createCollector()
8397
{
8498
$translator = new Translator('en');

src/Symfony/Component/Translation/Tests/Formatter/MessageFormatterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function testFormat($expected, $message, $parameters = array())
2626

2727
/**
2828
* @dataProvider getTransChoiceMessages
29+
* @group legacy
2930
*/
3031
public function testFormatPlural($expected, $message, $number, $parameters)
3132
{

0 commit comments

Comments
 (0)
0