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

Appearance settings

Commit ade060e

Browse filesBrowse files
committed
feature #18314 [Translation] added support for adding custom message formatter (aitboudad)
This PR was merged into the 3.4 branch. Discussion ---------- [Translation] added support for adding custom message formatter | Q | A | | --- | --- | | Branch? | master | | Bug fix? | no | | New feature? | yes | | BC breaks? | no | | Deprecations? | yes | | Tests pass? | yes | | Fixed tickets | #6009, #10152, one item in #11742, #11948 | | License | MIT | | Doc PR | ~ | Commits ------- 42183b0 [Translation] Support adding custom message formatter
2 parents eefded7 + 42183b0 commit ade060e
Copy full SHA for ade060e

File tree

17 files changed

+282
-49
lines changed
Filter options

17 files changed

+282
-49
lines changed

‎UPGRADE-3.4.md

Copy file name to clipboardExpand all lines: UPGRADE-3.4.md
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,27 @@ Translation
239239
and will be removed in 4.0, use `Symfony\Component\Translation\Writer\TranslationWriter::write`
240240
instead.
241241

242+
* Passing a `Symfony\Component\Translation\MessageSelector` to `Translator` has been
243+
deprecated. You should pass a message formatter instead
244+
245+
Before:
246+
247+
```php
248+
use Symfony\Component\Translation\Translator;
249+
use Symfony\Component\Translation\MessageSelector;
250+
251+
$translator = new Translator('fr_FR', new MessageSelector());
252+
```
253+
254+
After:
255+
256+
```php
257+
use Symfony\Component\Translation\Translator;
258+
use Symfony\Component\Translation\Formatter\MessageFormatter;
259+
260+
$translator = new Translator('fr_FR', new MessageFormatter());
261+
```
262+
242263
TwigBridge
243264
----------
244265

‎UPGRADE-4.0.md

Copy file name to clipboardExpand all lines: UPGRADE-4.0.md
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,9 @@ Translation
648648
* Removed `Symfony\Component\Translation\Writer\TranslationWriter::writeTranslations`,
649649
use `Symfony\Component\Translation\Writer\TranslationWriter::write` instead.
650650

651+
* Removed support for passing `Symfony\Component\Translation\MessageSelector` as a second argument to the
652+
`Translator::__construct()`. You should pass an instance of `Symfony\Component\Translation\Formatter\MessageFormatterInterface` instead.
653+
651654
TwigBundle
652655
----------
653656

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Tests/Extension/TranslationExtensionTest.php
+4-5Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
669669
->defaultValue(array('en'))
670670
->end()
671671
->booleanNode('logging')->defaultValue($this->debug)->end()
672+
->scalarNode('formatter')->defaultValue('translator.formatter.default')->end()
672673
->arrayNode('paths')
673674
->prototype('scalar')->end()
674675
->end()

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd
+1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml
+5-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php
+1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php
+7-7Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
+8-8Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/CHANGELOG.md
+1Lines 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
-----
+30Lines 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 <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 Abdellatif Ait boudad <a.aitboudad@gmail.com>
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+
}
+48Lines 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 <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+
use Symfony\Component\Translation\MessageSelector;
15+
16+
/**
17+
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
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+
}
+30Lines 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 <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+
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.