Skip to content

Commit 42a69ec

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

15 files changed

+198
-115
lines changed

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
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Formatter;
13+
14+
/**
15+
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
16+
*/
17+
class LegacyIntlMessageFormatter implements MessageFormatterInterface
18+
{
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function format($locale, $id, array $parameters = array())
23+
{
24+
if (!$parameters) {
25+
return $id;
26+
}
27+
28+
$formatter = new \MessageFormatter($locale, $id);
29+
if (null === $formatter) {
30+
return $this->fallbackToLegacyFormatter($message, $parameters);
31+
}
32+
33+
$message = $formatter->format($parameters);
34+
if ($formatter->getErrorCode() !== U_ZERO_ERROR) {
35+
return $this->fallbackToLegacyFormatter($message, $parameters);
36+
}
37+
38+
if (!$formatter->parse($message) && $formatter->getErrorCode() === U_ZERO_ERROR) {
39+
return $this->fallbackToLegacyFormatter($message, $parameters);
40+
}
41+
42+
return $message;
43+
}
44+
45+
private function fallbackToLegacyFormatter($message, $parameters)
46+
{
47+
return strtr($message, $parameters);
48+
}
49+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ interface MessageFormatterInterface
2121
/**
2222
* Formats a lozalized message pattern with given arguments.
2323
*
24-
* @param string $locale The message locale
25-
* @param string $id The message id (may also be an object that can be cast to string)
26-
* @param array $arguments An array of parameters for the message
24+
* @param string $locale The message locale
25+
* @param string $id The message id (may also be an object that can be cast to string)
26+
* @param array $parameters An array of parameters for the message
2727
*
2828
* @return string
2929
*
3030
* @throws \InvalidArgumentException
3131
*/
32-
public function format($locale, $id, array $arguments = array());
32+
public function format($locale, $id, array $parameters = array());
3333
}

src/Symfony/Component/Translation/IdentityTranslator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Translation;
1313

1414
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
15-
use Symfony\Component\Translation\Formatter\DefaultMessageFormatter;
15+
use Symfony\Component\Translation\Formatter\LegacyIntlMessageFormatter;
1616

1717
/**
1818
* IdentityTranslator does not translate anything.
@@ -37,12 +37,12 @@ public function __construct($formatter = null)
3737
if ($formatter instanceof MessageSelector) {
3838
@trigger_error('Passing a MessageSelector instance into the '.__METHOD__.' is deprecated since version 2.8 and will be removed in 3.0. Inject a MessageFormatterInterface instance instead.', E_USER_DEPRECATED);
3939
$this->selector = $formatter;
40-
$formatter = new DefaultMessageFormatter();
40+
$formatter = new LegacyIntlMessageFormatter();
4141
} else {
4242
$this->selector = new MessageSelector();
4343
}
4444

45-
$this->formatter = $formatter ?: new DefaultMessageFormatter();
45+
$this->formatter = $formatter ?: new LegacyIntlMessageFormatter();
4646
if (!$this->formatter instanceof MessageFormatterInterface) {
4747
throw new \InvalidArgumentException(sprintf('The message formatter "%s" must implement MessageFormatterInterface.', get_class($this->formatter)));
4848
}
@@ -89,6 +89,7 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
8989
*/
9090
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
9191
{
92+
@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);
9293
if (!$locale) {
9394
$locale = $this->getLocale();
9495
}

src/Symfony/Component/Translation/LoggingTranslator.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->log($id, $domain, $locale);
6364

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

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Translation\Translator;
1515
use Symfony\Component\Translation\DataCollectorTranslator;
1616
use Symfony\Component\Translation\Loader\ArrayLoader;
17+
use Symfony\Component\Translation\Formatter\IntlMessageFormatter;
1718

1819
class DataCollectorTranslatorTest extends \PHPUnit_Framework_TestCase
1920
{
@@ -23,15 +24,44 @@ protected function setUp()
2324
$this->markTestSkipped('The "DataCollector" is not available');
2425
}
2526
}
26-
public function testCollectMessages()
27+
28+
/**
29+
* @group legacy
30+
*/
31+
public function testCollectLegacyMessages()
2732
{
2833
$collector = $this->createCollector();
2934
$collector->setFallbackLocales(array('fr', 'ru'));
35+
$collector->transChoice('choice', 0);
36+
37+
$expectedMessages = array(
38+
array(
39+
'id' => 'choice',
40+
'translation' => 'choice',
41+
'locale' => 'en',
42+
'domain' => 'messages',
43+
'state' => DataCollectorTranslator::MESSAGE_MISSING,
44+
'parameters' => array(),
45+
'transChoiceNumber' => 0,
46+
),
47+
);
48+
49+
$this->assertEquals($expectedMessages, $collector->getCollectedMessages());
50+
}
51+
52+
public function testCollectMessages()
53+
{
54+
$resources = array(
55+
array('array', array('foo' => 'foo (en)'), 'en'),
56+
array('array', array('bar' => 'bar (fr)'), 'fr'),
57+
array('array', array('bar_ru' => '{foo} (ru)'), 'ru'),
58+
);
59+
60+
$collector = $this->createCollector($resources);
61+
$collector->setFallbackLocales(array('fr', 'ru'));
3062

3163
$collector->trans('foo');
3264
$collector->trans('bar');
33-
$collector->transChoice('choice', 0);
34-
$collector->trans('bar_ru');
3565
$collector->trans('bar_ru', array('foo' => 'bar'));
3666

3767
$expectedMessages = array();
@@ -53,24 +83,6 @@ public function testCollectMessages()
5383
'parameters' => array(),
5484
'transChoiceNumber' => null,
5585
);
56-
$expectedMessages[] = array(
57-
'id' => 'choice',
58-
'translation' => 'choice',
59-
'locale' => 'en',
60-
'domain' => 'messages',
61-
'state' => DataCollectorTranslator::MESSAGE_MISSING,
62-
'parameters' => array(),
63-
'transChoiceNumber' => 0,
64-
);
65-
$expectedMessages[] = array(
66-
'id' => 'bar_ru',
67-
'translation' => 'bar (ru)',
68-
'locale' => 'ru',
69-
'domain' => 'messages',
70-
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
71-
'parameters' => array(),
72-
'transChoiceNumber' => null,
73-
);
7486
$expectedMessages[] = array(
7587
'id' => 'bar_ru',
7688
'translation' => 'bar (ru)',
@@ -84,13 +96,13 @@ public function testCollectMessages()
8496
$this->assertEquals($expectedMessages, $collector->getCollectedMessages());
8597
}
8698

87-
private function createCollector()
99+
private function createCollector($resources = array())
88100
{
89-
$translator = new Translator('en');
101+
$translator = new Translator('en', new IntlMessageFormatter());
90102
$translator->addLoader('array', new ArrayLoader());
91-
$translator->addResource('array', array('foo' => 'foo (en)'), 'en');
92-
$translator->addResource('array', array('bar' => 'bar (fr)'), 'fr');
93-
$translator->addResource('array', array('bar_ru' => 'bar (ru)'), 'ru');
103+
foreach ($resources as $resource) {
104+
$translator->addResource($resource[0], $resource[1], $resource[2], isset($resource[3]) ? $resource[3] : null);
105+
}
94106

95107
$collector = new DataCollectorTranslator($translator);
96108

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function setUp()
2929
*/
3030
public function testFormat($expected, $message, $arguments)
3131
{
32-
$formatter = new IntlMessageFormatter();
32+
$formatter = $this->getMessageFormatter();
3333

3434
$this->assertEquals($expected, trim($formatter->format('en', $message, $arguments)));
3535
}
@@ -59,7 +59,7 @@ public function testFormatWithNamedArguments()
5959
other {{host} invites {guest} as one of the # people invited to their party.}}}}
6060
_MSG_;
6161

62-
$formatter = new IntlMessageFormatter();
62+
$formatter = $this->getMessageFormatter();
6363
$message = $formatter->format('en', $chooseMessage, array(
6464
'gender_of_host' => 'male',
6565
'num_guests' => 10,
@@ -85,4 +85,9 @@ public function provideDataForFormat()
8585
),
8686
);
8787
}
88+
89+
protected function getMessageFormatter()
90+
{
91+
return new IntlMessageFormatter();
92+
}
8893
}

src/Symfony/Component/Translation/Tests/Formatter/DefaultMessageFormatterTest.php renamed to src/Symfony/Component/Translation/Tests/Formatter/LegacyIntlMessageFormatterTest.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111

1212
namespace Symfony\Component\Translation\Tests\Formatter;
1313

14-
use Symfony\Component\Translation\Formatter\DefaultMessageFormatter;
14+
use Symfony\Component\Translation\Formatter\LegacyIntlMessageFormatter;
1515

16-
class DefaultMessageFormatterTest extends \PHPUnit_Framework_TestCase
16+
class LegacyIntlMessageFormatterTest extends IntlMessageFormatterTest
1717
{
1818
/**
1919
* @dataProvider provideDataForFormat
2020
*/
2121
public function testFormat($expected, $message, $arguments)
2222
{
23-
$formatter = new DefaultMessageFormatter();
23+
$formatter = $this->getMessageFormatter();
2424

2525
$this->assertEquals($expected, $formatter->format('en', $message, $arguments));
2626
}
@@ -41,14 +41,8 @@ public function provideDataForFormat()
4141
);
4242
}
4343

44-
private function mockMessageSelector($willCallChoose)
44+
protected function getMessageFormatter()
4545
{
46-
$mock = $this->getMock('Symfony\Component\Translation\MessageSelector');
47-
48-
$mock->expects($willCallChoose ? $this->once() : $this->never())
49-
->method('choose')
50-
->will($this->returnValue('Message'));
51-
52-
return $mock;
46+
return new LegacyIntlMessageFormatter();
5347
}
5448
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy