Updates according to feedback

This commit is contained in:
Nyholm 2018-09-04 07:36:39 +02:00
parent 597a15d7f7
commit f88153fa22
4 changed files with 9 additions and 19 deletions

View File

@ -7,7 +7,7 @@ CHANGELOG
* Started using ICU parent locales as fallback locales. * Started using ICU parent locales as fallback locales.
* deprecated `TranslatorInterface` in favor of `Symfony\Contracts\Translation\TranslatorInterface` * deprecated `TranslatorInterface` in favor of `Symfony\Contracts\Translation\TranslatorInterface`
* deprecated `MessageSelector`, `Interval` and `PluralizationRules`; use `IdentityTranslator` instead * deprecated `MessageSelector`, `Interval` and `PluralizationRules`; use `IdentityTranslator` instead
* Added `IntlMessageFormatter` and`FallbackMessageFormatter` * Added `IntlMessageFormatter` and `FallbackMessageFormatter`
4.1.0 4.1.0
----- -----

View File

@ -9,8 +9,6 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
declare(strict_types=1);
namespace Symfony\Component\Translation\Formatter; namespace Symfony\Component\Translation\Formatter;
use Symfony\Component\Translation\Exception\LogicException; use Symfony\Component\Translation\Exception\LogicException;
@ -73,6 +71,6 @@ class FallbackFormatter implements MessageFormatterInterface, ChoiceMessageForma
return $this->secondFormatter->choiceFormat($message, $number, $locale, $parameters); return $this->secondFormatter->choiceFormat($message, $number, $locale, $parameters);
} }
throw new LogicException(sprintf('The no formatter support plural translations.')); throw new LogicException(sprintf('No formatters support plural translations.'));
} }
} }

View File

@ -27,12 +27,12 @@ class IntlMessageFormatter implements MessageFormatterInterface
try { try {
$formatter = new \MessageFormatter($locale, $message); $formatter = new \MessageFormatter($locale, $message);
} catch (\Throwable $e) { } catch (\Throwable $e) {
throw new InvalidArgumentException(sprintf('Invalid message format. Reason: %s (error #%d)', intl_get_error_message(), intl_get_error_code()), 0, $e); throw new InvalidArgumentException(sprintf('Invalid message format (%s, error #%d).', intl_get_error_message(), intl_get_error_code()), 0, $e);
} }
$message = $formatter->format($parameters); $message = $formatter->format($parameters);
if (U_ZERO_ERROR !== $formatter->getErrorCode()) { if (U_ZERO_ERROR !== $formatter->getErrorCode()) {
throw new InvalidArgumentException(sprintf('Unable to format message. Reason: %s (error #%s)', $formatter->getErrorMessage(), $formatter->getErrorCode())); throw new InvalidArgumentException(sprintf('Unable to format message ( %s, error #%s).', $formatter->getErrorMessage(), $formatter->getErrorCode()));
} }
return $message; return $message;

View File

@ -16,12 +16,10 @@ use Symfony\Component\Translation\Formatter\IntlMessageFormatter;
class IntlMessageFormatterTest extends \PHPUnit\Framework\TestCase class IntlMessageFormatterTest extends \PHPUnit\Framework\TestCase
{ {
public function setUp() protected function setUp()
{ {
if (!\extension_loaded('intl')) { if (!\extension_loaded('intl')) {
$this->markTestSkipped( $this->markTestSkipped('The Intl extension is not available.');
'The Intl extension is not available.'
);
} }
} }
@ -30,13 +28,13 @@ class IntlMessageFormatterTest extends \PHPUnit\Framework\TestCase
*/ */
public function testFormat($expected, $message, $arguments) public function testFormat($expected, $message, $arguments)
{ {
$this->assertEquals($expected, trim($this->getMessageFormatter()->format($message, 'en', $arguments))); $this->assertEquals($expected, trim((new IntlMessageFormatter())->format($message, 'en', $arguments)));
} }
public function testInvalidFormat() public function testInvalidFormat()
{ {
$this->expectException(InvalidArgumentException::class); $this->expectException(InvalidArgumentException::class);
$this->getMessageFormatter()->format('{foo', 'en', array(2)); (new IntlMessageFormatter())->format('{foo', 'en', array(2));
} }
public function testFormatWithNamedArguments() public function testFormatWithNamedArguments()
@ -64,8 +62,7 @@ class IntlMessageFormatterTest extends \PHPUnit\Framework\TestCase
other {{host} invites {guest} as one of the # people invited to their party.}}}} other {{host} invites {guest} as one of the # people invited to their party.}}}}
_MSG_; _MSG_;
$formatter = $this->getMessageFormatter(); $message = (new IntlMessageFormatter())->format($chooseMessage, 'en', array(
$message = $formatter->format($chooseMessage, 'en', array(
'gender_of_host' => 'male', 'gender_of_host' => 'male',
'num_guests' => 10, 'num_guests' => 10,
'host' => 'Fabien', 'host' => 'Fabien',
@ -90,9 +87,4 @@ _MSG_;
), ),
); );
} }
private function getMessageFormatter()
{
return new IntlMessageFormatter();
}
} }