IntlFormatter example

/** * @requires extension intl */
class IntlFormatterTest extends \PHPUnit\Framework\TestCase
{
    /** * @dataProvider provideDataForFormat */
    public function testFormat($expected$message$arguments)
    {
        $this->assertEquals($expectedtrim((new IntlFormatter())->formatIntl($message, 'en', $arguments)));
    }

    public function testInvalidFormat()
    {
        $this->expectException(InvalidArgumentException::class);
        (new IntlFormatter())->formatIntl('{foo', 'en', [2]);
    }

    public function testFormatWithNamedArguments()
    {
        if (version_compare(\INTL_ICU_VERSION, '4.8', '<')) {
            
class MessageFormatter implements MessageFormatterInterface, IntlFormatterInterface
{
    private TranslatorInterface $translator;
    private IntlFormatterInterface $intlFormatter;

    /** * @param TranslatorInterface|null $translator An identity translator to use as selector for pluralization */
    public function __construct(TranslatorInterface $translator = null, IntlFormatterInterface $intlFormatter = null)
    {
        $this->translator = $translator ?? new IdentityTranslator();
        $this->intlFormatter = $intlFormatter ?? new IntlFormatter();
    }

    public function format(string $message, string $locale, array $parameters = []): string
    {
        if ($this->translator instanceof TranslatorInterface) {
            return $this->translator->trans($message$parameters, null, $locale);
        }

        return strtr($message$parameters);
    }

    
Home | Imprint | This part of the site doesn't use cookies.