Updates according to feedback
This commit is contained in:
parent
597a15d7f7
commit
f88153fa22
@ -7,7 +7,7 @@ CHANGELOG
|
||||
* Started using ICU parent locales as fallback locales.
|
||||
* deprecated `TranslatorInterface` in favor of `Symfony\Contracts\Translation\TranslatorInterface`
|
||||
* deprecated `MessageSelector`, `Interval` and `PluralizationRules`; use `IdentityTranslator` instead
|
||||
* Added `IntlMessageFormatter` and`FallbackMessageFormatter`
|
||||
* Added `IntlMessageFormatter` and `FallbackMessageFormatter`
|
||||
|
||||
4.1.0
|
||||
-----
|
||||
|
@ -9,8 +9,6 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Symfony\Component\Translation\Formatter;
|
||||
|
||||
use Symfony\Component\Translation\Exception\LogicException;
|
||||
@ -73,6 +71,6 @@ class FallbackFormatter implements MessageFormatterInterface, ChoiceMessageForma
|
||||
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.'));
|
||||
}
|
||||
}
|
||||
|
@ -27,12 +27,12 @@ class IntlMessageFormatter implements MessageFormatterInterface
|
||||
try {
|
||||
$formatter = new \MessageFormatter($locale, $message);
|
||||
} 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);
|
||||
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;
|
||||
|
@ -16,12 +16,10 @@ use Symfony\Component\Translation\Formatter\IntlMessageFormatter;
|
||||
|
||||
class IntlMessageFormatterTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function setUp()
|
||||
protected function setUp()
|
||||
{
|
||||
if (!\extension_loaded('intl')) {
|
||||
$this->markTestSkipped(
|
||||
'The Intl extension is not available.'
|
||||
);
|
||||
$this->markTestSkipped('The Intl extension is not available.');
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,13 +28,13 @@ class IntlMessageFormatterTest extends \PHPUnit\Framework\TestCase
|
||||
*/
|
||||
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()
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->getMessageFormatter()->format('{foo', 'en', array(2));
|
||||
(new IntlMessageFormatter())->format('{foo', 'en', array(2));
|
||||
}
|
||||
|
||||
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.}}}}
|
||||
_MSG_;
|
||||
|
||||
$formatter = $this->getMessageFormatter();
|
||||
$message = $formatter->format($chooseMessage, 'en', array(
|
||||
$message = (new IntlMessageFormatter())->format($chooseMessage, 'en', array(
|
||||
'gender_of_host' => 'male',
|
||||
'num_guests' => 10,
|
||||
'host' => 'Fabien',
|
||||
@ -90,9 +87,4 @@ _MSG_;
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
private function getMessageFormatter()
|
||||
{
|
||||
return new IntlMessageFormatter();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user