Skip to content

Commit 210c7a9

Browse files
committed
deprecate transChoice method + [IntlFormater] fallback the lagacy massages
1 parent fae8625 commit 210c7a9

21 files changed

+309
-154
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,13 @@ public function trans($message, array $arguments = array(), $domain = null, $loc
9393
return $this->translator->trans($message, $arguments, $domain, $locale);
9494
}
9595

96+
/**
97+
* @deprecated since version 2.8, to be removed in 3.0. Use the {@link trans} method instead.
98+
*/
9699
public function transchoice($message, $count, array $arguments = array(), $domain = null, $locale = null)
97100
{
101+
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Use trans() method instead.', E_USER_DEPRECATED);
102+
98103
return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
99104
}
100105

src/Symfony/Bridge/Twig/Tests/Extension/TranslationExtensionTest.php

Lines changed: 82 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ public function testTrans($template, $expected, array $variables = array())
4242
$this->assertEquals($expected, $this->getTemplate($template)->render($variables));
4343
}
4444

45+
/**
46+
* @dataProvider getTranschoiceTests
47+
* @group legacy
48+
*/
49+
public function testTranschoice($template, $expected, array $variables = array())
50+
{
51+
$this->testTrans($template, $expected, $variables);
52+
}
53+
4554
/**
4655
* @expectedException \Twig_Error_Syntax
4756
* @expectedExceptionMessage Unexpected token. Twig was looking for the "with", "from", or "into" keyword in "index" at line 3.
@@ -84,6 +93,18 @@ public function getTransTests()
8493

8594
array('{% trans into "fr"%}Hello{% endtrans %}', 'Hello'),
8695

96+
// trans filter
97+
array('{{ "Hello"|trans }}', 'Hello'),
98+
array('{{ name|trans }}', 'Symfony', array('name' => 'Symfony')),
99+
array('{{ hello|trans({ \'%name%\': \'Symfony\' }) }}', 'Hello Symfony', array('hello' => 'Hello %name%')),
100+
array('{% set vars = { \'%name%\': \'Symfony\' } %}{{ hello|trans(vars) }}', 'Hello Symfony', array('hello' => 'Hello %name%')),
101+
array('{{ "Hello"|trans({}, "messages", "fr") }}', 'Hello'),
102+
);
103+
}
104+
105+
public function getTranschoiceTests()
106+
{
107+
return array(
87108
// transchoice
88109
array('{% transchoice count from "messages" %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
89110
'There is no apples', array('count' => 0),),
@@ -98,21 +119,14 @@ public function getTransTests()
98119
array('{% transchoice 5 into "fr"%}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
99120
'There is 5 apples',),
100121

101-
// trans filter
102-
array('{{ "Hello"|trans }}', 'Hello'),
103-
array('{{ name|trans }}', 'Symfony', array('name' => 'Symfony')),
104-
array('{{ hello|trans({ \'%name%\': \'Symfony\' }) }}', 'Hello Symfony', array('hello' => 'Hello %name%')),
105-
array('{% set vars = { \'%name%\': \'Symfony\' } %}{{ hello|trans(vars) }}', 'Hello Symfony', array('hello' => 'Hello %name%')),
106-
array('{{ "Hello"|trans({}, "messages", "fr") }}', 'Hello'),
107-
108122
// transchoice filter
109123
array('{{ "{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples"|transchoice(count) }}', 'There is 5 apples', array('count' => 5)),
110124
array('{{ text|transchoice(5, {\'%name%\': \'Symfony\'}) }}', 'There is 5 apples (Symfony)', array('text' => '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%)')),
111125
array('{{ "{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples"|transchoice(count, {}, "messages", "fr") }}', 'There is 5 apples', array('count' => 5)),
112126
);
113127
}
114128

115-
public function testDefaultTranslationDomain()
129+
public function testTransDefaultTranslationDomain()
116130
{
117131
$templates = array(
118132
'index' => '
@@ -125,6 +139,31 @@ public function testDefaultTranslationDomain()
125139
{%- trans from "custom" %}foo{% endtrans %}
126140
{{- "foo"|trans }}
127141
{{- "foo"|trans({}, "custom") }}
142+
{% endblock %}
143+
',
144+
145+
'base' => '
146+
{%- block content "" %}
147+
',
148+
);
149+
150+
$template = $this->getTemplate($templates, $this->getTranslator());
151+
152+
$this->assertEquals('foo (foo)foo (custom)foo (foo)foo (custom)', trim($template->render(array())));
153+
}
154+
155+
/**
156+
* @group legacy
157+
*/
158+
public function testTransChoiceDefaultTranslationDomain()
159+
{
160+
$templates = array(
161+
'index' => '
162+
{%- extends "base" %}
163+
164+
{%- trans_default_domain "foo" %}
165+
166+
{%- block content %}
128167
{{- "foo"|transchoice(1) }}
129168
{{- "foo"|transchoice(1, {}, "custom") }}
130169
{% endblock %}
@@ -135,15 +174,9 @@ public function testDefaultTranslationDomain()
135174
',
136175
);
137176

138-
$translator = new Translator('en');
139-
$translator->addLoader('array', new ArrayLoader());
140-
$translator->addResource('array', array('foo' => 'foo (messages)'), 'en');
141-
$translator->addResource('array', array('foo' => 'foo (custom)'), 'en', 'custom');
142-
$translator->addResource('array', array('foo' => 'foo (foo)'), 'en', 'foo');
143-
144-
$template = $this->getTemplate($templates, $translator);
177+
$template = $this->getTemplate($templates, $this->getTranslator());
145178

146-
$this->assertEquals('foo (foo)foo (custom)foo (foo)foo (custom)foo (foo)foo (custom)', trim($template->render(array())));
179+
$this->assertEquals('foo (foo)foo (custom)', trim($template->render(array())));
147180
}
148181

149182
public function testDefaultTranslationDomainWithNamedArguments()
@@ -154,10 +187,33 @@ public function testDefaultTranslationDomainWithNamedArguments()
154187
155188
{%- block content %}
156189
{{- "foo"|trans(arguments = {}, domain = "custom") }}
157-
{{- "foo"|transchoice(count = 1) }}
158-
{{- "foo"|transchoice(count = 1, arguments = {}, domain = "custom") }}
159190
{{- "foo"|trans({}, domain = "custom") }}
160191
{{- "foo"|trans({}, "custom", locale = "fr") }}
192+
{% endblock %}
193+
',
194+
195+
'base' => '
196+
{%- block content "" %}
197+
',
198+
);
199+
200+
$template = $this->getTemplate($templates, $this->getTranslator());
201+
202+
$this->assertEquals('foo (custom)foo (custom)foo (fr)', trim($template->render(array())));
203+
}
204+
205+
/**
206+
* @group legacy
207+
*/
208+
public function testTransChoiceDefaultTranslationDomainWithNamedArguments()
209+
{
210+
$templates = array(
211+
'index' => '
212+
{%- trans_default_domain "foo" %}
213+
214+
{%- block content %}
215+
{{- "foo"|transchoice(count = 1) }}
216+
{{- "foo"|transchoice(count = 1, arguments = {}, domain = "custom") }}
161217
{{- "foo"|transchoice(1, arguments = {}, domain = "custom") }}
162218
{{- "foo"|transchoice(1, {}, "custom", locale = "fr") }}
163219
{% endblock %}
@@ -168,16 +224,21 @@ public function testDefaultTranslationDomainWithNamedArguments()
168224
',
169225
);
170226

227+
$template = $this->getTemplate($templates, $this->getTranslator());
228+
229+
$this->assertEquals('foo (foo)foo (custom)foo (custom)foo (fr)', trim($template->render(array())));
230+
}
231+
232+
protected function getTranslator()
233+
{
171234
$translator = new Translator('en');
172235
$translator->addLoader('array', new ArrayLoader());
173236
$translator->addResource('array', array('foo' => 'foo (messages)'), 'en');
174237
$translator->addResource('array', array('foo' => 'foo (custom)'), 'en', 'custom');
175238
$translator->addResource('array', array('foo' => 'foo (foo)'), 'en', 'foo');
176239
$translator->addResource('array', array('foo' => 'foo (fr)'), 'fr', 'custom');
177240

178-
$template = $this->getTemplate($templates, $translator);
179-
180-
$this->assertEquals('foo (custom)foo (foo)foo (custom)foo (custom)foo (fr)foo (custom)foo (fr)', trim($template->render(array())));
241+
return $translator;
181242
}
182243

183244
protected function getTemplate($template, $translator = null)

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
584584
->defaultValue(array('en'))
585585
->end()
586586
->booleanNode('logging')->defaultValue($this->debug)->end()
587-
->scalarNode('formatter')->defaultValue('translator.formatter.default')->end()
587+
->scalarNode('formatter')->defaultValue('translator.formatter.legacy_intl')->end()
588588
->arrayNode('paths')
589589
->prototype('scalar')->end()
590590
->end()

src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@
5757
<argument type="service" id="translator.formatter" />
5858
</service>
5959

60-
<service id="translator.formatter" alias="translator.formatter.default" />
60+
<service id="translator.formatter" alias="translator.formatter.legacy_intl" />
6161
<service id="translator.formatter.intl" class="Symfony\Component\Translation\Formatter\IntlMessageFormatter" public="false" />
6262

63-
<service id="translator.formatter.default" class="Symfony\Component\Translation\Formatter\DefaultMessageFormatter" public="false">
63+
<service id="translator.formatter.legacy_intl" class="Symfony\Component\Translation\Formatter\LegacyIntlMessageFormatter" public="false">
6464
<argument type="service" id="translator.selector" />
6565
</service>
6666

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ protected static function getBundleDefaultConfig()
155155
),
156156
'translator' => array(
157157
'enabled' => false,
158-
'formatter' => 'translator.formatter.default',
158+
'formatter' => 'translator.formatter.legacy_intl',
159159
'fallbacks' => array('en'),
160160
'logging' => true,
161161
'paths' => array(),

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
1515
use Symfony\Component\Translation\MessageCatalogue;
1616
use Symfony\Component\Filesystem\Filesystem;
17-
use Symfony\Component\Translation\Formatter\DefaultMessageFormatter;
17+
use Symfony\Component\Translation\Formatter\IntlMessageFormatter;
1818

1919
class TranslatorTest extends \PHPUnit_Framework_TestCase
2020
{
@@ -50,10 +50,10 @@ public function testTransWithoutCaching()
5050
$this->assertEquals('foo (FR)', $translator->trans('foo'));
5151
$this->assertEquals('bar (EN)', $translator->trans('bar'));
5252
$this->assertEquals('foobar (ES)', $translator->trans('foobar'));
53-
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0));
53+
$this->assertEquals('choice 0 (EN)', $translator->trans('choice', array('count' => 0)));
5454
$this->assertEquals('no translation', $translator->trans('no translation'));
5555
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
56-
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
56+
$this->assertEquals('other choice 1 (PT-BR)', $translator->trans('other choice', array('count' => 1)));
5757
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
5858
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
5959
}
@@ -68,10 +68,10 @@ public function testTransWithCaching()
6868
$this->assertEquals('foo (FR)', $translator->trans('foo'));
6969
$this->assertEquals('bar (EN)', $translator->trans('bar'));
7070
$this->assertEquals('foobar (ES)', $translator->trans('foobar'));
71-
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0));
71+
$this->assertEquals('choice 0 (EN)', $translator->trans('choice', array('count' => 0)));
7272
$this->assertEquals('no translation', $translator->trans('no translation'));
7373
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
74-
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
74+
$this->assertEquals('other choice 1 (PT-BR)', $translator->trans('other choice', array('count' => 1)));
7575
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
7676
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
7777

@@ -86,10 +86,10 @@ public function testTransWithCaching()
8686
$this->assertEquals('foo (FR)', $translator->trans('foo'));
8787
$this->assertEquals('bar (EN)', $translator->trans('bar'));
8888
$this->assertEquals('foobar (ES)', $translator->trans('foobar'));
89-
$this->assertEquals('choice 0 (EN)', $translator->transChoice('choice', 0));
89+
$this->assertEquals('choice 0 (EN)', $translator->trans('choice', array('count' => 0)));
9090
$this->assertEquals('no translation', $translator->trans('no translation'));
9191
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
92-
$this->assertEquals('other choice 1 (PT-BR)', $translator->transChoice('other choice', 1));
92+
$this->assertEquals('other choice 1 (PT-BR)', $translator->trans('other choice', array('count' => 1)));
9393
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
9494
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
9595
}
@@ -157,7 +157,7 @@ public function testGetDefaultLocale()
157157
->will($this->returnValue('en'))
158158
;
159159

160-
$translator = new Translator($container, new DefaultMessageFormatter());
160+
$translator = new Translator($container, new IntlMessageFormatter());
161161

162162
$this->assertSame('en', $translator->getLocale());
163163
}
@@ -191,7 +191,8 @@ protected function getLoader()
191191
->will($this->returnValue($this->getCatalogue('en', array(
192192
'foo' => 'foo (EN)',
193193
'bar' => 'bar (EN)',
194-
'choice' => '{0} choice 0 (EN)|{1} choice 1 (EN)|]1,Inf] choice inf (EN)',
194+
'choice' => '{count, plural, =0 {choice 0 (EN)} =1 {choice 1 (EN)} other {# choice inf (EN)}}'
195+
// {0} choice 0 (EN)|{1} choice 1 (EN)|]1,Inf] choice inf (EN)',
195196
))))
196197
;
197198
$loader
@@ -212,7 +213,7 @@ protected function getLoader()
212213
->expects($this->at(4))
213214
->method('load')
214215
->will($this->returnValue($this->getCatalogue('pt_BR', array(
215-
'other choice' => '{0} other choice 0 (PT-BR)|{1} other choice 1 (PT-BR)|]1,Inf] other choice inf (PT-BR)',
216+
'other choice' => '{count, plural, =0 {other choice 0 (PT-BR)} =1 {other choice 1 (PT-BR)} other {# other choice inf (PT-BR)}}',
216217
))))
217218
;
218219
$loader
@@ -290,7 +291,7 @@ private function createTranslator($loader, $options, $translatorClass = '\Symfon
290291
{
291292
return new $translatorClass(
292293
$this->getContainer($loader),
293-
new DefaultMessageFormatter(),
294+
new IntlMessageFormatter(),
294295
array($loaderFomat => array($loaderFomat)),
295296
$options
296297
);

src/Symfony/Component/Translation/DataCollectorTranslator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
5858
*/
5959
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
6060
{
61+
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Rely on the MessageFormatterInterface and TranslatorInterface::trans() method instead.', E_USER_DEPRECATED);
6162
$trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
6263
$this->collectMessage($locale, $domain, $id, $trans, $parameters, $number);
6364

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

Lines changed: 0 additions & 26 deletions
This file was deleted.

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,28 @@ class IntlMessageFormatter implements MessageFormatterInterface
1919
/**
2020
* {@inheritdoc}
2121
*/
22-
public function format($locale, $id, array $arguments = array())
22+
public function format($locale, $id, array $parameters = array())
2323
{
24-
$formatter = new \MessageFormatter($locale, $id);
24+
if (!$parameters) {
25+
return $id;
26+
}
2527

28+
$formatter = new \MessageFormatter($locale, $id);
2629
if (null === $formatter) {
2730
throw new \InvalidArgumentException(sprintf('Invalid message format. Reason: %s (error #%d)', intl_get_error_message(), intl_get_error_code()));
2831
}
2932

30-
$message = $formatter->format($arguments);
31-
33+
$message = $formatter->format($parameters);
3234
if ($formatter->getErrorCode() !== U_ZERO_ERROR) {
3335
throw new \InvalidArgumentException(sprintf('Unable to format message. Reason: %s (error #%s)', $formatter->getErrorMessage(), $formatter->getErrorCode()));
3436
}
3537

38+
if (!$formatter->parse($message) && $formatter->getErrorCode() === U_ZERO_ERROR) {
39+
@trigger_error('Passing a MessageSelector instance into the '.__METHOD__.' as a second argument is deprecated since version 2.8 and will be removed in 3.0. Inject a MessageFormatterInterface instance instead.', E_USER_DEPRECATED);
40+
41+
return strtr($message, $parameters);
42+
}
43+
3644
return $message;
3745
}
3846
}

0 commit comments

Comments
 (0)