Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42183b0

Browse files
committed
[Translation] Support adding custom message formatter
1 parent b1b6860 commit 42183b0

File tree

17 files changed

+282
-49
lines changed

17 files changed

+282
-49
lines changed

UPGRADE-3.4.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,27 @@ Translation
234234
and will be removed in 4.0, use `Symfony\Component\Translation\Writer\TranslationWriter::write`
235235
instead.
236236

237+
* Passing a `Symfony\Component\Translation\MessageSelector` to `Translator` has been
238+
deprecated. You should pass a message formatter instead
239+
240+
Before:
241+
242+
```php
243+
use Symfony\Component\Translation\Translator;
244+
use Symfony\Component\Translation\MessageSelector;
245+
246+
$translator = new Translator('fr_FR', new MessageSelector());
247+
```
248+
249+
After:
250+
251+
```php
252+
use Symfony\Component\Translation\Translator;
253+
use Symfony\Component\Translation\Formatter\MessageFormatter;
254+
255+
$translator = new Translator('fr_FR', new MessageFormatter());
256+
```
257+
237258
TwigBridge
238259
----------
239260

UPGRADE-4.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,9 @@ Translation
643643
* Removed `Symfony\Component\Translation\Writer\TranslationWriter::writeTranslations`,
644644
use `Symfony\Component\Translation\Writer\TranslationWriter::write` instead.
645645

646+
* Removed support for passing `Symfony\Component\Translation\MessageSelector` as a second argument to the
647+
`Translator::__construct()`. You should pass an instance of `Symfony\Component\Translation\Formatter\MessageFormatterInterface` instead.
648+
646649
TwigBundle
647650
----------
648651

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\Twig\Extension\TranslationExtension;
1616
use Symfony\Component\Translation\Translator;
17-
use Symfony\Component\Translation\MessageSelector;
1817
use Symfony\Component\Translation\Loader\ArrayLoader;
1918
use Twig\Environment;
2019
use Twig\Loader\ArrayLoader as TwigArrayLoader;
@@ -37,7 +36,7 @@ public function testTrans($template, $expected, array $variables = array())
3736
echo $template."\n";
3837
$loader = new TwigArrayLoader(array('index' => $template));
3938
$twig = new Environment($loader, array('debug' => true, 'cache' => false));
40-
$twig->addExtension(new TranslationExtension(new Translator('en', new MessageSelector())));
39+
$twig->addExtension(new TranslationExtension(new Translator('en')));
4140

4241
echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSourceContext('index'))))."\n\n";
4342
$this->assertEquals($expected, $this->getTemplate($template)->render($variables));
@@ -139,7 +138,7 @@ public function testDefaultTranslationDomain()
139138
',
140139
);
141140

142-
$translator = new Translator('en', new MessageSelector());
141+
$translator = new Translator('en');
143142
$translator->addLoader('array', new ArrayLoader());
144143
$translator->addResource('array', array('foo' => 'foo (messages)'), 'en');
145144
$translator->addResource('array', array('foo' => 'foo (custom)'), 'en', 'custom');
@@ -172,7 +171,7 @@ public function testDefaultTranslationDomainWithNamedArguments()
172171
',
173172
);
174173

175-
$translator = new Translator('en', new MessageSelector());
174+
$translator = new Translator('en');
176175
$translator->addLoader('array', new ArrayLoader());
177176
$translator->addResource('array', array('foo' => 'foo (messages)'), 'en');
178177
$translator->addResource('array', array('foo' => 'foo (custom)'), 'en', 'custom');
@@ -187,7 +186,7 @@ public function testDefaultTranslationDomainWithNamedArguments()
187186
protected function getTemplate($template, $translator = null)
188187
{
189188
if (null === $translator) {
190-
$translator = new Translator('en', new MessageSelector());
189+
$translator = new Translator('en');
191190
}
192191

193192
if (is_array($template)) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
668668
->defaultValue(array('en'))
669669
->end()
670670
->booleanNode('logging')->defaultValue($this->debug)->end()
671+
->scalarNode('formatter')->defaultValue('translator.formatter.default')->end()
671672
->arrayNode('paths')
672673
->prototype('scalar')->end()
673674
->end()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,7 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
10751075

10761076
// Use the "real" translator instead of the identity default
10771077
$container->setAlias('translator', 'translator.default');
1078+
$container->setAlias('translator.formatter', new Alias($config['formatter'], false));
10781079
$translator = $container->findDefinition('translator.default');
10791080
$translator->addMethodCall('setFallbackLocales', array($config['fallbacks']));
10801081

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
<xsd:attribute name="enabled" type="xsd:boolean" />
185185
<xsd:attribute name="fallback" type="xsd:string" />
186186
<xsd:attribute name="logging" type="xsd:boolean" />
187+
<xsd:attribute name="formatter" type="xsd:string" />
187188
</xsd:complexType>
188189

189190
<xsd:complexType name="validation">

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<service id="translator.default" class="Symfony\Bundle\FrameworkBundle\Translation\Translator" public="true">
1111
<argument /> <!-- translation loaders locator -->
12-
<argument type="service" id="translator.selector" />
12+
<argument type="service" id="translator.formatter" />
1313
<argument>%kernel.default_locale%</argument>
1414
<argument type="collection" /> <!-- translation loaders ids -->
1515
<argument type="collection">
@@ -28,6 +28,10 @@
2828
<tag name="monolog.logger" channel="translation" />
2929
</service>
3030

31+
<service id="translator.formatter.default" class="Symfony\Component\Translation\Formatter\MessageFormatter">
32+
<argument type="service" id="translator.selector" />
33+
</service>
34+
3135
<service id="translation.loader.php" class="Symfony\Component\Translation\Loader\PhpFileLoader" public="true">
3236
<tag name="translation.loader" alias="php" />
3337
</service>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ protected static function getBundleDefaultConfig()
255255
'enabled' => !class_exists(FullStack::class),
256256
'fallbacks' => array('en'),
257257
'logging' => true,
258+
'formatter' => 'translator.formatter.default',
258259
'paths' => array(),
259260
),
260261
'validation' => array(

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Psr\Container\ContainerInterface;
1616
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
17+
use Symfony\Component\Translation\Formatter\MessageFormatter;
1718
use Symfony\Component\Translation\MessageCatalogue;
1819
use Symfony\Component\Filesystem\Filesystem;
19-
use Symfony\Component\Translation\MessageSelector;
2020

2121
class TranslatorTest extends TestCase
2222
{
@@ -149,7 +149,7 @@ public function testGetDefaultLocaleOmittingLocale()
149149
->with('kernel.default_locale')
150150
->will($this->returnValue('en'))
151151
;
152-
$translator = new Translator($container, new MessageSelector());
152+
$translator = new Translator($container, new MessageFormatter());
153153

154154
$this->assertSame('en', $translator->getLocale());
155155
}
@@ -162,7 +162,7 @@ public function testGetDefaultLocaleOmittingLocale()
162162
public function testGetDefaultLocaleOmittingLocaleWithPsrContainer()
163163
{
164164
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
165-
$translator = new Translator($container, new MessageSelector());
165+
$translator = new Translator($container, new MessageFormatter());
166166
}
167167

168168
/**
@@ -277,7 +277,7 @@ public function testLoadResourcesWithoutCaching()
277277
public function testGetDefaultLocale()
278278
{
279279
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
280-
$translator = new Translator($container, new MessageSelector(), 'en');
280+
$translator = new Translator($container, new MessageFormatter(), 'en');
281281

282282
$this->assertSame('en', $translator->getLocale());
283283
}
@@ -290,7 +290,7 @@ public function testInvalidOptions()
290290
{
291291
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
292292

293-
(new Translator($container, new MessageSelector(), 'en', array(), array('foo' => 'bar')));
293+
(new Translator($container, new MessageFormatter(), 'en', array(), array('foo' => 'bar')));
294294
}
295295

296296
/** @dataProvider getDebugModeAndCacheDirCombinations */
@@ -468,15 +468,15 @@ private function createTranslator($loader, $options, $translatorClass = '\Symfon
468468
if (null === $defaultLocale) {
469469
return new $translatorClass(
470470
$this->getContainer($loader),
471-
new MessageSelector(),
471+
new MessageFormatter(),
472472
array($loaderFomat => array($loaderFomat)),
473473
$options
474474
);
475475
}
476476

477477
return new $translatorClass(
478478
$this->getContainer($loader),
479-
new MessageSelector(),
479+
new MessageFormatter(),
480480
$defaultLocale,
481481
array($loaderFomat => array($loaderFomat)),
482482
$options

src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
1616
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
1717
use Symfony\Component\Translation\Translator as BaseTranslator;
18-
use Symfony\Component\Translation\MessageSelector;
1918
use Symfony\Component\Translation\Exception\InvalidArgumentException;
19+
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
2020

2121
/**
2222
* Translator.
@@ -56,15 +56,15 @@ class Translator extends BaseTranslator implements WarmableInterface
5656
* * debug: Whether to enable debugging or not (false by default)
5757
* * resource_files: List of translation resources available grouped by locale.
5858
*
59-
* @param ContainerInterface $container A ContainerInterface instance
60-
* @param MessageSelector $selector The message selector for pluralization
61-
* @param string $defaultLocale
62-
* @param array $loaderIds An array of loader Ids
63-
* @param array $options An array of options
59+
* @param ContainerInterface $container A ContainerInterface instance
60+
* @param MessageFormatterInterface $formatter The message formatter
61+
* @param string $defaultLocale
62+
* @param array $loaderIds An array of loader Ids
63+
* @param array $options An array of options
6464
*
6565
* @throws InvalidArgumentException
6666
*/
67-
public function __construct(ContainerInterface $container, MessageSelector $selector, $defaultLocale = null, array $loaderIds = array(), array $options = array())
67+
public function __construct(ContainerInterface $container, $formatter, $defaultLocale = null, array $loaderIds = array(), array $options = array())
6868
{
6969
// BC 3.x, to be removed in 4.0 along with the $defaultLocale default value
7070
if (is_array($defaultLocale) || 3 > func_num_args()) {
@@ -90,7 +90,7 @@ public function __construct(ContainerInterface $container, MessageSelector $sele
9090
$this->resourceLocales = array_keys($this->options['resource_files']);
9191
$this->addResourceFiles($this->options['resource_files']);
9292

93-
parent::__construct($defaultLocale, $selector, $this->options['cache_dir'], $this->options['debug']);
93+
parent::__construct($defaultLocale, $formatter, $this->options['cache_dir'], $this->options['debug']);
9494
}
9595

9696
/**

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
* Improved Xliff 2.0 loader to load `<notes>` section.
1313
* Added `TranslationWriterInterface`
1414
* Deprecated `TranslationWriter::writeTranslations` in favor of `TranslationWriter::write`
15+
* added support for adding custom message formatter and decoupling the default one.
1516

1617
3.2.0
1718
-----
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Abdellatif Ait boudad <[email protected]>
16+
*/
17+
interface ChoiceMessageFormatterInterface
18+
{
19+
/**
20+
* Formats a localized message pattern with given arguments.
21+
*
22+
* @param string $message The message (may also be an object that can be cast to string)
23+
* @param int $number The number to use to find the indice of the message
24+
* @param string $locale The message locale
25+
* @param array $parameters An array of parameters for the message
26+
*
27+
* @return string
28+
*/
29+
public function choiceFormat($message, $number, $locale, array $parameters = array());
30+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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+
use Symfony\Component\Translation\MessageSelector;
15+
16+
/**
17+
* @author Abdellatif Ait boudad <[email protected]>
18+
*/
19+
class MessageFormatter implements MessageFormatterInterface, ChoiceMessageFormatterInterface
20+
{
21+
private $selector;
22+
23+
/**
24+
* @param MessageSelector|null $selector The message selector for pluralization
25+
*/
26+
public function __construct(MessageSelector $selector = null)
27+
{
28+
$this->selector = $selector ?: new MessageSelector();
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function format($message, $locale, array $parameters = array())
35+
{
36+
return strtr($message, $parameters);
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function choiceFormat($message, $number, $locale, array $parameters = array())
43+
{
44+
$parameters = array_merge(array('%count%' => $number), $parameters);
45+
46+
return $this->format($this->selector->choose($message, (int) $number, $locale), $locale, $parameters);
47+
}
48+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 <[email protected]>
16+
* @author Abdellatif Ait boudad <[email protected]>
17+
*/
18+
interface MessageFormatterInterface
19+
{
20+
/**
21+
* Formats a localized message pattern with given arguments.
22+
*
23+
* @param string $message The message (may also be an object that can be cast to string)
24+
* @param string $locale The message locale
25+
* @param array $parameters An array of parameters for the message
26+
*
27+
* @return string
28+
*/
29+
public function format($message, $locale, array $parameters = array());
30+
}

0 commit comments

Comments
 (0)