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 a325a44

Browse filesBrowse files
committed
Allow config for different domain specific formatters
1 parent b43fe21 commit a325a44
Copy full SHA for a325a44

File tree

9 files changed

+64
-22
lines changed
Filter options

9 files changed

+64
-22
lines changed

‎src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
* Deprecated the `Symfony\Bundle\FrameworkBundle\Controller\Controller` class in favor of `Symfony\Bundle\FrameworkBundle\Controller\AbstractController`.
1212
* Enabled autoconfiguration for `Psr\Log\LoggerAwareInterface`
1313
* Added new "auto" mode for `framework.session.cookie_secure` to turn it on when HTTPS is used
14+
* Added support for configuring the `Translator` with multiple formatters.
1415

1516
4.1.0
1617
-----

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
+14-1Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,14 +690,27 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
690690
->{!class_exists(FullStack::class) && class_exists(Translator::class) ? 'canBeDisabled' : 'canBeEnabled'}()
691691
->fixXmlConfig('fallback')
692692
->fixXmlConfig('path')
693+
->fixXmlConfig('domain_formatter')
693694
->children()
694695
->arrayNode('fallbacks')
695696
->beforeNormalization()->ifString()->then(function ($v) { return array($v); })->end()
696697
->prototype('scalar')->end()
697698
->defaultValue(array('en'))
698699
->end()
699700
->booleanNode('logging')->defaultValue(false)->end()
700-
->scalarNode('formatter')->defaultValue('translator.formatter.default')->end()
701+
->scalarNode('formatter')
702+
->info('The default formatter to use if none is specified.')
703+
->defaultValue('translator.formatter.default')
704+
->end()
705+
->arrayNode('domain_formatters')
706+
->info('Configure different formatters per domain.')
707+
->useAttributeAsKey('domain')
708+
->prototype('array')
709+
->children()
710+
->scalarNode('service')->cannotBeEmpty()->end()
711+
->end()
712+
->end()
713+
->end()
701714
->scalarNode('default_path')
702715
->info('The default path used to load translations')
703716
->defaultValue('%kernel.project_dir%/translations')

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,9 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
981981
$container->setAlias('translator.formatter', new Alias($config['formatter'], false));
982982
$translator = $container->findDefinition('translator.default');
983983
$translator->addMethodCall('setFallbackLocales', array($config['fallbacks']));
984+
foreach ($config['domain_formatters'] as $formatter) {
985+
$translator->addMethodCall('addFormatter', array($formatter['domain'], new Reference($formatter['service'])));
986+
}
984987

985988
$container->setParameter('translator.logging', $config['logging']);
986989
$container->setParameter('translator.default_path', $config['default_path']);

‎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
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,19 @@
185185
<xsd:sequence>
186186
<xsd:element name="fallback" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
187187
<xsd:element name="path" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
188+
<xsd:element name="domain-formatter" type="translator_formatter" minOccurs="0" maxOccurs="unbounded" />
188189
</xsd:sequence>
189190
<xsd:attribute name="enabled" type="xsd:boolean" />
190191
<xsd:attribute name="fallback" type="xsd:string" />
191192
<xsd:attribute name="logging" type="xsd:boolean" />
192193
<xsd:attribute name="formatter" type="xsd:string" />
193194
</xsd:complexType>
194195

196+
<xsd:complexType name="translator_formatter">
197+
<xsd:attribute name="domain" type="xsd:string" use="required" />
198+
<xsd:attribute name="service" type="xsd:string" />
199+
</xsd:complexType>
200+
195201
<xsd:complexType name="validation">
196202
<xsd:choice minOccurs="0" maxOccurs="unbounded">
197203
<xsd:element name="static-method" type="xsd:string" />

‎src/Symfony/Component/Translation/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/CHANGELOG.md
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ CHANGELOG
77
* Started using ICU parent locales as fallback locales.
88
* deprecated `TranslatorInterface` in favor of `Symfony\Contracts\Translation\TranslatorInterface`
99
* deprecated `MessageSelector`, `Interval` and `PluralizationRules`; use `IdentityTranslator` instead
10+
* Added intl message formatter.
11+
* Added support for one formatter per domain
1012

1113
4.1.0
1214
-----
@@ -41,7 +43,6 @@ CHANGELOG
4143
-----
4244

4345
* Added support for escaping `|` in plural translations with double pipe.
44-
* Added intl message formatter.
4546

4647
3.1.0
4748
-----

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Formatter/IntlMessageFormatter.php
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ class IntlMessageFormatter implements MessageFormatterInterface, ChoiceMessageFo
2222
*/
2323
public function format($message, $locale, array $parameters = array())
2424
{
25-
$formatter = new \MessageFormatter($locale, $message);
25+
try {
26+
$formatter = new \MessageFormatter($locale, $message);
27+
} catch (\Throwable $e) {
28+
throw new \InvalidArgumentException('Invalid message format.', $e);
29+
}
2630
if (null === $formatter) {
2731
throw new \InvalidArgumentException(sprintf('Invalid message format. Reason: %s (error #%d)', intl_get_error_message(), intl_get_error_code()));
2832
}
2933

3034
$message = $formatter->format($parameters);
31-
if ($formatter->getErrorCode() !== U_ZERO_ERROR) {
35+
if (U_ZERO_ERROR !== $formatter->getErrorCode()) {
3236
throw new \InvalidArgumentException(sprintf('Unable to format message. Reason: %s (error #%s)', $formatter->getErrorMessage(), $formatter->getErrorCode()));
3337
}
3438

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/Formatter/IntlMessageFormatterTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Symfony\Component\Translation\Formatter\IntlMessageFormatter;
1515

16-
class IntlMessageFormatterTest extends \PHPUnit_Framework_TestCase
16+
class IntlMessageFormatterTest extends \PHPUnit\Framework\TestCase
1717
{
1818
public function setUp()
1919
{
@@ -34,7 +34,7 @@ public function testFormat($expected, $message, $arguments)
3434

3535
public function testFormatWithNamedArguments()
3636
{
37-
if (PHP_VERSION_ID < 50500 || version_compare(INTL_ICU_VERSION, '4.8', '<')) {
37+
if (version_compare(INTL_ICU_VERSION, '4.8', '<')) {
3838
$this->markTestSkipped('Format with named arguments can only be run with ICU 4.8 or higher and PHP >= 5.5');
3939
}
4040

‎src/Symfony/Component/Translation/Tests/TranslatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/TranslatorTest.php
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Translation\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
16+
use Symfony\Component\Translation\Translator;
1517
use Symfony\Component\Translation\Loader\ArrayLoader;
1618
use Symfony\Component\Translation\MessageCatalogue;
1719
use Symfony\Component\Translation\Translator;
@@ -567,6 +569,30 @@ public function testTransChoiceFallbackWithNoTranslation()
567569
// unchanged if it can't be found
568570
$this->assertEquals('some_message2', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
569571
}
572+
573+
public function testDomainSpecificFormatter()
574+
{
575+
$fooFormatter = $this->getMockBuilder(MessageFormatterInterface::class)
576+
->setMethods(array('format'))
577+
->getMock();
578+
$fooFormatter->expects($this->exactly(2))
579+
->method('format')
580+
->with('foo', 'en', array());
581+
582+
$barFormatter = $this->getMockBuilder(MessageFormatterInterface::class)
583+
->setMethods(array('format'))
584+
->getMock();
585+
$barFormatter->expects($this->exactly(1))
586+
->method('format')
587+
->with('bar', 'en', array());
588+
589+
$translator = new Translator('en', $fooFormatter);
590+
$translator->addFormatter('bar_domain', $barFormatter);
591+
592+
$translator->trans('foo');
593+
$translator->trans('foo', array(), 'foo_domain');
594+
$translator->trans('bar', array(), 'bar_domain');
595+
}
570596
}
571597

572598
class StringClass

‎src/Symfony/Component/Translation/Translator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Translator.php
+4-16Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function __construct(?string $locale, MessageFormatterInterface $formatte
8989
$formatter = new MessageFormatter();
9090
}
9191

92-
$this->formatters['default'] = $formatter;
92+
$this->formatters['_default'] = $formatter;
9393
$this->cacheDir = $cacheDir;
9494
$this->debug = $debug;
9595
}
@@ -137,11 +137,7 @@ public function addResource($format, $resource, $locale, $domain = null)
137137
}
138138
}
139139

140-
/**
141-
* @param string $domain
142-
* @param MessageFormatterInterface $formatter
143-
*/
144-
public function addFormatter(string $domain, MessageFormatterInterface $formatter)
140+
public function addFormatter(string $domain, MessageFormatterInterface $formatter): void
145141
{
146142
$this->formatters[$domain] = $formatter;
147143
}
@@ -470,16 +466,8 @@ private function getConfigCacheFactory(): ConfigCacheFactoryInterface
470466
return $this->configCacheFactory;
471467
}
472468

473-
/**
474-
* @param string $domain
475-
* @return MessageFormatterInterface
476-
*/
477-
private function getFormatter(string $domain)
469+
private function getFormatter(string $domain): MessageFormatterInterface
478470
{
479-
if (isset($this->formatters[$domain])) {
480-
return $this->formatters[$domain];
481-
}
482-
483-
return $this->formatters['default'];
471+
return $this->formatters[$domain] ?? $this->formatters['_default'];
484472
}
485473
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.