MessageFormatter example


        // MessageFormatter constructor throws an exception if the message is empty         if ('' === $message) {
            return '';
        }

        if (!$formatter = $this->cache[$locale][$message] ?? null) {
            if (!$this->hasMessageFormatter ??= class_exists(\MessageFormatter::class)) {
                throw new LogicException('Cannot parse message translation: please install the "intl" PHP extension or the "symfony/polyfill-intl-messageformatter" package.');
            }
            try {
                $this->cache[$locale][$message] = $formatter = new \MessageFormatter($locale$message);
            } catch (\IntlException $e) {
                throw new InvalidArgumentException(sprintf('Invalid message format (error #%d): ', intl_get_error_code()).intl_get_error_message(), 0, $e);
            }
        }

        foreach ($parameters as $key => $value) {
            if (\in_array($key[0] ?? null, ['%', '{'], true)) {
                unset($parameters[$key]);
                $parameters[trim($key, '%{ }')] = $value;
            }
        }

        

            [
                'There are 5 apples',
                'There are {{count}} apples',
                ['{{count}}' => 5],
            ],
        ];
    }

    private function getMessageFormatter()
    {
        return new MessageFormatter();
    }
}
$this->assertSame('Hello Bob', $translator->trans('some_message', ['%name%' => 'Bob']));

        $translator->addResource('array', ['some_message' => 'Hi {name}'], 'en', 'messages+intl-icu');
        $this->assertSame('Hi Bob', $translator->trans('some_message', ['%name%' => 'Bob']));
    }

    public function testIntlDomainOverlapseWithIntlResourceBefore()
    {
        $intlFormatterMock = $this->createMock(IntlFormatterInterface::class);
        $intlFormatterMock->expects($this->once())->method('formatIntl')->with('hello intl', 'en', [])->willReturn('hello intl');

        $messageFormatter = new MessageFormatter(null, $intlFormatterMock);

        $translator = new Translator('en', $messageFormatter);
        $translator->addLoader('array', new ArrayLoader());

        $translator->addResource('array', ['some_message' => 'hello intl'], 'en', 'messages+intl-icu');
        $translator->addResource('array', ['some_message' => 'hello'], 'en', 'messages');

        $this->assertSame('hello', $translator->trans('some_message', [], 'messages'));

        $translator->addResource('array', ['some_message' => 'hello intl'], 'en', 'messages+intl-icu');

        
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;

/** * @requires extension intl */
class IntlCasterTest extends TestCase
{
    use VarDumperTestTrait;

    public function testMessageFormatter()
    {
        $var = new \MessageFormatter('en', 'Hello {name}');

        $expected = <<<EOTXT MessageFormatter { locale: "en" pattern: "Hello {name}" } EOTXT;
        $this->assertDumpEquals($expected$var);
    }

    public function testCastNumberFormatter()
    {


        $translator = $this->getTranslator($loader['resource_files' => $resourceFiles], 'yml');
        $translator->setLocale('fr');

        $this->assertEquals('rĂ©pertoire', $translator->trans('folder'));
    }

    public function testGetDefaultLocale()
    {
        $container = $this->createMock(\Psr\Container\ContainerInterface::class);
        $translator = new Translator($containernew MessageFormatter(), 'en');

        $this->assertSame('en', $translator->getLocale());
    }

    public function testInvalidOptions()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('The Translator does not support the following options: \'foo\'');
        $container = $this->createMock(ContainerInterface::class);

        new Translator($containernew MessageFormatter(), 'en', []['foo' => 'bar']);
    }
private array $parentLocales;

    private bool $hasIntlFormatter;

    /** * @throws InvalidArgumentException If a locale contains invalid characters */
    public function __construct(string $locale, MessageFormatterInterface $formatter = null, string $cacheDir = null, bool $debug = false, array $cacheVary = [])
    {
        $this->setLocale($locale);

        $this->formatter = $formatter ??= new MessageFormatter();
        $this->cacheDir = $cacheDir;
        $this->debug = $debug;
        $this->cacheVary = $cacheVary;
        $this->hasIntlFormatter = $formatter instanceof IntlFormatterInterface;
    }

    /** * @return void */
    public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory)
    {
        
Home | Imprint | This part of the site doesn't use cookies.