Skip to content

Commit 5751278

Browse files
committed
Allow config for different domain specific formatters
1 parent efe6abf commit 5751278

File tree

5 files changed

+40
-21
lines changed

5 files changed

+40
-21
lines changed

CHANGELOG.md

Lines 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
-----

Formatter/IntlMessageFormatter.php

Lines 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

Tests/Formatter/IntlMessageFormatterTest.php

Lines 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

Tests/TranslatorTest.php

Lines 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

Translator.php

Lines 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)