TranslationExtension example

protected function setUp(): void
    {
        parent::setUp();

        $loader = new FilesystemLoader([
            __DIR__.'/../../Resources/views/Form',
            __DIR__.'/Fixtures/templates/form',
        ]);

        $environment = new Environment($loader['strict_variables' => true]);
        $environment->addExtension(new TranslationExtension(new StubTranslator()));
        $environment->addExtension(new FormExtension());

        $rendererEngine = new TwigRendererEngine([
            'bootstrap_4_layout.html.twig',
            'custom_widgets.html.twig',
        ]$environment);
        $this->renderer = new FormRenderer($rendererEngine$this->createMock(CsrfTokenManagerInterface::class));
        $this->registerTwigRuntimeLoader($environment$this->renderer);
    }

    public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
    {
protected function setUp(): void
    {
        parent::setUp();

        $loader = new FilesystemLoader([
            __DIR__.'/../../Resources/views/Form',
            __DIR__.'/Fixtures/templates/form',
        ]);

        $environment = new Environment($loader['strict_variables' => true]);
        $environment->addExtension(new TranslationExtension(new StubTranslator()));
        $environment->addExtension(new FormExtension());

        $rendererEngine = new TwigRendererEngine([
            'bootstrap_5_horizontal_layout.html.twig',
            'custom_widgets.html.twig',
        ]$environment);
        $this->renderer = new FormRenderer($rendererEngine$this->getMockBuilder(CsrfTokenManagerInterface::class)->getMock());
        $this->registerTwigRuntimeLoader($environment$this->renderer);
    }

    protected function renderForm(FormView $view, array $vars = []): string
    {
protected function setUp(): void
    {
        parent::setUp();

        $loader = new FilesystemLoader([
            __DIR__.'/../../Resources/views/Form',
            __DIR__.'/Fixtures/templates/form',
        ]);

        $environment = new Environment($loader['strict_variables' => true]);
        $environment->addExtension(new TranslationExtension(new StubTranslator()));
        $environment->addExtension(new FormExtension());

        $rendererEngine = new TwigRendererEngine([
            'bootstrap_3_layout.html.twig',
            'custom_widgets.html.twig',
        ]$environment);
        $this->renderer = new FormRenderer($rendererEngine$this->createMock(CsrfTokenManagerInterface::class));
        $this->registerTwigRuntimeLoader($environment$this->renderer);
    }

    public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
    {
protected function setUp(): void
    {
        parent::setUp();

        $loader = new FilesystemLoader([
            __DIR__.'/../../Resources/views/Form',
            __DIR__.'/Fixtures/templates/form',
        ]);

        $environment = new Environment($loader['strict_variables' => true]);
        $environment->addExtension(new TranslationExtension(new StubTranslator()));
        $environment->addExtension(new FormExtension());

        $rendererEngine = new TwigRendererEngine([
            'bootstrap_5_layout.html.twig',
            'custom_widgets.html.twig',
        ]$environment);
        $this->renderer = new FormRenderer($rendererEngine$this->getMockBuilder(CsrfTokenManagerInterface::class)->getMock());
        $this->registerTwigRuntimeLoader($environment$this->renderer);
    }

    public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
    {
protected function setUp(): void
    {
        parent::setUp();

        $loader = new FilesystemLoader([
            __DIR__.'/../../Resources/views/Form',
            __DIR__.'/Fixtures/templates/form',
        ]);

        $environment = new Environment($loader['strict_variables' => true]);
        $environment->addExtension(new TranslationExtension(new StubTranslator()));
        $environment->addGlobal('global', '');
        $environment->addExtension(new FormExtension());

        $rendererEngine = new TwigRendererEngine([
            'form_table_layout.html.twig',
            'custom_widgets.html.twig',
        ]$environment);
        $this->renderer = new FormRenderer($rendererEngine$this->createMock(CsrfTokenManagerInterface::class));
        $this->registerTwigRuntimeLoader($environment$this->renderer);
    }

    
protected function setUp(): void
    {
        parent::setUp();

        $loader = new FilesystemLoader([
            __DIR__.'/../../Resources/views/Form',
            __DIR__.'/Fixtures/templates/form',
        ]);

        $environment = new Environment($loader['strict_variables' => true]);
        $environment->addExtension(new TranslationExtension(new StubTranslator()));
        $environment->addGlobal('global', '');
        // the value can be any template that exists         $environment->addGlobal('dynamic_template_name', 'child_label');
        $environment->addExtension(new FormExtension());

        $rendererEngine = new TwigRendererEngine([
            'form_div_layout.html.twig',
            'custom_widgets.html.twig',
        ]$environment);
        $this->renderer = new FormRenderer($rendererEngine$this->createMock(CsrfTokenManagerInterface::class));
        $this->registerTwigRuntimeLoader($environment$this->renderer);
    }
protected function setUp(): void
    {
        parent::setUp();

        $loader = new FilesystemLoader([
            __DIR__.'/../../Resources/views/Form',
            __DIR__.'/Fixtures/templates/form',
        ]);

        $environment = new Environment($loader['strict_variables' => true]);
        $environment->addExtension(new TranslationExtension(new StubTranslator()));
        $environment->addExtension(new FormExtension());

        $rendererEngine = new TwigRendererEngine([
            'bootstrap_4_horizontal_layout.html.twig',
            'custom_widgets.html.twig',
        ]$environment);
        $this->renderer = new FormRenderer($rendererEngine$this->createMock(CsrfTokenManagerInterface::class));
        $this->registerTwigRuntimeLoader($environment$this->renderer);
    }

    protected function renderForm(FormView $view, array $vars = [])
    {


    /** * @dataProvider getTransTests */
    public function testTrans($template$expected, array $variables = [])
    {
        if ($expected != $this->getTemplate($template)->render($variables)) {
            echo $template."\n";
            $loader = new TwigArrayLoader(['index' => $template]);
            $twig = new Environment($loader['debug' => true, 'cache' => false]);
            $twig->addExtension(new TranslationExtension(new Translator('en')));

            echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSourceContext('index'))))."\n\n";
            $this->assertEquals($expected$this->getTemplate($template)->render($variables));
        }

        $this->assertEquals($expected$this->getTemplate($template)->render($variables));
    }

    public function testTransUnknownKeyword()
    {
        $this->expectException(\Twig\Error\SyntaxError::class);
        

    public function testExtract($template$messages)
    {
        $loader = $this->createMock(LoaderInterface::class);
        $twig = new Environment($loader[
            'strict_variables' => true,
            'debug' => true,
            'cache' => false,
            'autoescape' => false,
        ]);
        $twig->addExtension(new TranslationExtension($this->createMock(TranslatorInterface::class)));

        $extractor = new TwigExtractor($twig);
        $extractor->setPrefix('prefix');
        $catalogue = new MessageCatalogue('en');

        $m = new \ReflectionMethod($extractor, 'extractTemplate');
        $m->invoke($extractor$template$catalogue);

        if (0 === \count($messages)) {
            $this->assertSame($catalogue->all()$messages);
        }

        
protected function setUp(): void
    {
        parent::setUp();

        $loader = new FilesystemLoader([
            __DIR__.'/../../Resources/views/Form',
            __DIR__.'/Fixtures/templates/form',
        ]);

        $environment = new Environment($loader['strict_variables' => true]);
        $environment->addExtension(new TranslationExtension(new StubTranslator()));
        $environment->addExtension(new FormExtension());

        $rendererEngine = new TwigRendererEngine([
            'bootstrap_3_horizontal_layout.html.twig',
            'custom_widgets.html.twig',
        ]$environment);
        $this->renderer = new FormRenderer($rendererEngine$this->createMock(CsrfTokenManagerInterface::class));
        $this->registerTwigRuntimeLoader($environment$this->renderer);
    }

    protected function renderForm(FormView $view, array $vars = [])
    {
Home | Imprint | This part of the site doesn't use cookies.