spy-enot
spy-enot

Reputation: 203

Symfony 4.0 Translator class not injected to controller

I got a problem in new symfony 4.

<?php

namespace App\Controller;

use App\Entity\Flight;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use App\Form\FlightType;
use Symfony\Component\Translation\Translator;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class DefaultController
 * @package App\Controller
 */
class DefaultController extends Controller
{
    /**
     * 
     * @Route("/")
     * @Route("/{_locale}/", name="homepage", requirements={"_locale" = "%app.locales%"})
     * 
     * @param Translator $translator
     * @param Request $request
     * 
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
     */
    public function index(Translator $translator, Request $request)
    {
        $translated = $translator->trans('Symfony is great');

Error: Controller "App\Controller\DefaultController::index()" requires that you provide a value for the "$translator" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.

Configs: service.yaml

services:    
    _defaults:
        autowire: true    
        autoconfigure: true 
        public: false

...
App\Controller\:
    autowire: true       
    resource: '../src/Controller'
    tags: ['controller.service_arguments']

translation.yaml

framework:
    default_locale: '%locale%'
    translator:
        paths:
            - '%kernel.project_dir%/translations'
        fallbacks: ['en']    

Whats wrong? Manual here: http://symfony.com/doc/current/translation.html

Upvotes: 1

Views: 3945

Answers (1)

spy-enot
spy-enot

Reputation: 203

Found the answer.

  1. bin/console cache:clear did not clear messages cache. Helped just hard remove var/cache folder.
  2. I have used Action(TranslatorInterface $translator) for inject to controller action (Probably bug in docs)
  3. $translator->trans('id') doesn't work with ids. It work when using trans-unit sourse tag.

Upvotes: 3

Related Questions