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

Skip to content

Commit 1660808

Browse files
committed
[Translation] allow using multiple formatters with decoupling the default one.
1 parent 5213778 commit 1660808

File tree

15 files changed

+306
-43
lines changed

15 files changed

+306
-43
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Bridge\Twig\Extension\TranslationExtension;
1515
use Symfony\Component\Translation\Translator;
16-
use Symfony\Component\Translation\MessageSelector;
1716
use Symfony\Component\Translation\Loader\ArrayLoader;
1817

1918
class TranslationExtensionTest extends \PHPUnit_Framework_TestCase
@@ -34,7 +33,7 @@ public function testTrans($template, $expected, array $variables = array())
3433
echo $template."\n";
3534
$loader = new \Twig_Loader_Array(array('index' => $template));
3635
$twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
37-
$twig->addExtension(new TranslationExtension(new Translator('en', new MessageSelector())));
36+
$twig->addExtension(new TranslationExtension(new Translator('en')));
3837

3938
echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSource('index'), 'index')))."\n\n";
4039
$this->assertEquals($expected, $this->getTemplate($template)->render($variables));
@@ -136,7 +135,7 @@ public function testDefaultTranslationDomain()
136135
',
137136
);
138137

139-
$translator = new Translator('en', new MessageSelector());
138+
$translator = new Translator('en');
140139
$translator->addLoader('array', new ArrayLoader());
141140
$translator->addResource('array', array('foo' => 'foo (messages)'), 'en');
142141
$translator->addResource('array', array('foo' => 'foo (custom)'), 'en', 'custom');
@@ -169,7 +168,7 @@ public function testDefaultTranslationDomainWithNamedArguments()
169168
',
170169
);
171170

172-
$translator = new Translator('en', new MessageSelector());
171+
$translator = new Translator('en');
173172
$translator->addLoader('array', new ArrayLoader());
174173
$translator->addResource('array', array('foo' => 'foo (messages)'), 'en');
175174
$translator->addResource('array', array('foo' => 'foo (custom)'), 'en', 'custom');
@@ -184,7 +183,7 @@ public function testDefaultTranslationDomainWithNamedArguments()
184183
protected function getTemplate($template, $translator = null)
185184
{
186185
if (null === $translator) {
187-
$translator = new Translator('en', new MessageSelector());
186+
$translator = new Translator('en');
188187
}
189188

190189
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
@@ -550,6 +550,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
550550
->defaultValue(array('en'))
551551
->end()
552552
->booleanNode('logging')->defaultValue($this->debug)->end()
553+
->scalarNode('formatter')->defaultValue('translator.formatter.default')->end()
553554
->arrayNode('paths')
554555
->prototype('scalar')->end()
555556
->end()

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

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

720720
// Use the "real" translator instead of the identity default
721721
$container->setAlias('translator', 'translator.default');
722+
$container->setAlias('translator.formatter', $config['formatter']);
722723
$translator = $container->findDefinition('translator.default');
723724
$translator->addMethodCall('setFallbackLocales', array($config['fallbacks']));
724725

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
@@ -171,6 +171,7 @@
171171
<xsd:attribute name="enabled" type="xsd:boolean" />
172172
<xsd:attribute name="fallback" type="xsd:string" />
173173
<xsd:attribute name="logging" type="xsd:boolean" />
174+
<xsd:attribute name="formatter" type="xsd:string" />
174175
</xsd:complexType>
175176

176177
<xsd:complexType name="validation">

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<services>
88
<service id="translator.default" class="Symfony\Bundle\FrameworkBundle\Translation\Translator">
99
<argument type="service" id="service_container" />
10-
<argument type="service" id="translator.selector" />
10+
<argument type="service" id="translator.formatter" />
1111
<argument type="collection" /> <!-- translation loaders -->
1212
<argument type="collection">
1313
<argument key="cache_dir">%kernel.cache_dir%/translations</argument>
@@ -28,6 +28,12 @@
2828
</service>
2929

3030
<service id="translator" class="Symfony\Component\Translation\IdentityTranslator">
31+
<argument type="service" id="translator.formatter" />
32+
</service>
33+
34+
<service id="translator.formatter" alias="translator.formatter.default" />
35+
36+
<service id="translator.formatter.default" class="Symfony\Component\Translation\Formatter\TransMessageFormatter" public="false">
3137
<argument type="service" id="translator.selector" />
3238
</service>
3339

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ protected static function getBundleDefaultConfig()
203203
'enabled' => false,
204204
'fallbacks' => array('en'),
205205
'logging' => true,
206+
'formatter' => 'translator.formatter.default',
206207
'paths' => array(),
207208
),
208209
'validation' => array(

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
1515
use Symfony\Component\Translation\MessageCatalogue;
1616
use Symfony\Component\Filesystem\Filesystem;
17-
use Symfony\Component\Translation\MessageSelector;
1817

1918
class TranslatorTest extends \PHPUnit_Framework_TestCase
2019
{
@@ -130,7 +129,7 @@ public function testGetDefaultLocale()
130129
->will($this->returnValue('en'))
131130
;
132131

133-
$translator = new Translator($container, new MessageSelector());
132+
$translator = new Translator($container);
134133

135134
$this->assertSame('en', $translator->getLocale());
136135
}
@@ -264,7 +263,7 @@ private function createTranslator($loader, $options, $translatorClass = '\Symfon
264263
{
265264
return new $translatorClass(
266265
$this->getContainer($loader),
267-
new MessageSelector(),
266+
null,
268267
array($loaderFomat => array($loaderFomat)),
269268
$options
270269
);

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
1515
use Symfony\Component\Translation\Translator as BaseTranslator;
16-
use Symfony\Component\Translation\MessageSelector;
1716
use Symfony\Component\DependencyInjection\ContainerInterface;
17+
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
1818

1919
/**
2020
* Translator.
@@ -46,14 +46,15 @@ class Translator extends BaseTranslator implements WarmableInterface
4646
* * debug: Whether to enable debugging or not (false by default)
4747
* * resource_files: List of translation resources available grouped by locale.
4848
*
49-
* @param ContainerInterface $container A ContainerInterface instance
50-
* @param MessageSelector $selector The message selector for pluralization
51-
* @param array $loaderIds An array of loader Ids
52-
* @param array $options An array of options
49+
* @param ContainerInterface $container A ContainerInterface instance
50+
* @param MessageFormatterInterface[] $formatters An array of formatters
51+
* Passing the MessageSelector as a second parameter is deprecated since version 3.1.
52+
* @param array $loaderIds An array of loader Ids
53+
* @param array $options An array of options
5354
*
5455
* @throws \InvalidArgumentException
5556
*/
56-
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array())
57+
public function __construct(ContainerInterface $container, $formatters = null, $loaderIds = array(), array $options = array())
5758
{
5859
$this->container = $container;
5960
$this->loaderIds = $loaderIds;
@@ -69,7 +70,7 @@ public function __construct(ContainerInterface $container, MessageSelector $sele
6970
$this->loadResources();
7071
}
7172

72-
parent::__construct($container->getParameter('kernel.default_locale'), $selector, $this->options['cache_dir'], $this->options['debug']);
73+
parent::__construct($container->getParameter('kernel.default_locale'), $formatters, $this->options['cache_dir'], $this->options['debug']);
7374
}
7475

7576
/**

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
* Added support for escaping `|` in plural translations with double pipe.
8+
* Added support for using multiple formatters and decoupling default formatter.
89

910
3.1.0
1011
-----
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 $id The message id (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($id, $locale, array $parameters = array());
30+
31+
/**
32+
* Returns the name of the formatter.
33+
*
34+
* @return string The formatter name
35+
*/
36+
public function getName();
37+
}
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 PluralMessageFormatterInterface extends MessageFormatterInterface
18+
{
19+
/**
20+
* Formats a localized message pattern with given arguments.
21+
*
22+
* @param string $id The message id (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 pluralFormat($id, $number, $locale, array $parameters = array());
30+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 TransMessageFormatter implements PluralMessageFormatterInterface
20+
{
21+
const NAME = 'trans';
22+
23+
private $selector;
24+
25+
/**
26+
* @param MessageSelector|null $selector The message selector for pluralization
27+
*/
28+
public function __construct(MessageSelector $selector = null)
29+
{
30+
$this->selector = $selector ?: new MessageSelector();
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function format($id, $locale, array $parameters = array())
37+
{
38+
return strtr((string) $id, $parameters);
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function pluralFormat($id, $number, $locale, array $parameters = array())
45+
{
46+
return $this->format($this->selector->choose($id, (int) $number, $locale), $locale, $parameters);
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function getName()
53+
{
54+
return self::NAME;
55+
}
56+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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\Tests\Formatter;
13+
14+
use Symfony\Component\Translation\Formatter\TransMessageFormatter;
15+
16+
class TransMessageFormatterTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @dataProvider getTransMessages
20+
*/
21+
public function testFormat($expected, $message, $parameters = array())
22+
{
23+
$formatter = new TransMessageFormatter();
24+
25+
$this->assertEquals($expected, $formatter->format($message, 'en', $parameters));
26+
}
27+
28+
/**
29+
* @dataProvider getTransChoiceMessages
30+
*/
31+
public function testPluralFormat($expected, $message, $number, $parameters)
32+
{
33+
$formatter = new TransMessageFormatter();
34+
35+
$this->assertEquals($expected, $formatter->pluralFormat($message, $number, 'fr', $parameters));
36+
}
37+
38+
public function getTransMessages()
39+
{
40+
return array(
41+
array(
42+
'There is one apple',
43+
'There is one apple',
44+
),
45+
array(
46+
'There are 5 apples',
47+
'There are %count% apples',
48+
array('%count%' => 5),
49+
),
50+
array(
51+
'There are 5 apples',
52+
'There are {{count}} apples',
53+
array('{{count}}' => 5),
54+
),
55+
);
56+
}
57+
58+
public function getTransChoiceMessages()
59+
{
60+
return array(
61+
array('Il y a 0 pomme', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 0, array('%count%' => 0)),
62+
array('Il y a 1 pomme', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 1, array('%count%' => 1)),
63+
array('Il y a 10 pommes', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 10, array('%count%' => 10)),
64+
65+
array('Il y a 0 pomme', 'Il y a %count% pomme|Il y a %count% pommes', 0, array('%count%' => 0)),
66+
array('Il y a 1 pomme', 'Il y a %count% pomme|Il y a %count% pommes', 1, array('%count%' => 1)),
67+
array('Il y a 10 pommes', 'Il y a %count% pomme|Il y a %count% pommes', 10, array('%count%' => 10)),
68+
69+
array('Il y a 0 pomme', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 0, array('%count%' => 0)),
70+
array('Il y a 1 pomme', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 1, array('%count%' => 1)),
71+
array('Il y a 10 pommes', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 10, array('%count%' => 10)),
72+
73+
array('Il n\'y a aucune pomme', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 0, array('%count%' => 0)),
74+
array('Il y a 1 pomme', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 1, array('%count%' => 1)),
75+
array('Il y a 10 pommes', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 10, array('%count%' => 10)),
76+
77+
array('Il y a 0 pomme', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 0, array('%count%' => 0)),
78+
);
79+
}
80+
}

0 commit comments

Comments
 (0)