Search code examples
symfonysymfony-components

serviceContainer returns an IdentityTranslator in symfony 2.8


I'm trying to use the translation component but I'm not able to get it - and use - properly.

When I request /testT:

namespace Codechick\Test2Bundle\Controller;

class DefaultController extends Controller
{
    /**
     * @Route("/testT")
     */
    public function testTAction(Request $req)
    {
        die(var_dump($this->get('translator')));
    }
}

What I get is this:

object(Symfony\Component\Translation\IdentityTranslator)[203]
  private 'selector' => 
    object(Symfony\Component\Translation\MessageSelector)[202]
  private 'locale' => string 'it' (length=2)

hence, nothing is being translated. Why is that? Here is my configuration (there are no overrides in my _dev conf):

#<root>/app/config/config.yml
framework:
    translator: { fallbacks: [it,en] }

Solution

  • In according with the news announcement, from the version 2.6 the translator component is defined as service like translator.default.

    So change your code:

        die(var_dump($this->get('translator')));
    

    with

        die(var_dump($this->get('translator.default')));
    

    Hope this help