3

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] }
Bertuz
  • 2,390
  • 3
  • 25
  • 50

1 Answers1

0

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

Matteo
  • 37,680
  • 11
  • 100
  • 115
  • That definitely solved the problem of getting an instance of Translator, but when I try to output the translated key nothing seems to be translated. I tried to look for something in the log (like a warning, according to the documentation) but nothing about the translator seems to be logged. Any suggestions? – Bertuz Dec 17 '15 at 10:35
  • Plus, I think the documentation still refers to `translator` in place of `translator.default`. http://symfony.com/doc/2.8/book/translation.html#basic-translation do you think it's valid anyway? – Bertuz Dec 17 '15 at 10:38
  • Hi @Bertuz you can check that you have correctly configured the messages with the command `translation:update` executing it as example, `app/console translation:update it --dump-messages` – Matteo Dec 17 '15 at 10:42
  • it actually seems to be well configured. The key appears with a cozy `[OK] 1 messages were successfully extracted.` But by using `$this->get('translator.default')->trans('');` the key is not translated – Bertuz Dec 17 '15 at 10:52
  • @Bertuz try setting only one fallback in the configuration like this: `translator: { fallback: it }` – Matteo Dec 17 '15 at 11:04
  • you can also check for the configuration with the command `app/console debug:container --parameters | grep locale` – Matteo Dec 17 '15 at 11:06
  • nope, nothing changes! I tried to figure out what's happening by calling `die(dump($translator->getMessages()));` and `die(dump($translator->getLocale()));`. Locale says 'it' which is what I expect, but `getMessages`returns a disappointing empty array. Sigh. – Bertuz Dec 17 '15 at 11:07
  • 1
    nope, nothing happens with `app/console cache:clear `. Daje, I move this issue into another question. This was actually regarding getting the correct Translation instance. Thank you – Bertuz Dec 17 '15 at 11:12
  • here it is: http://stackoverflow.com/questions/34338186/translatorgetmessages-returns-an-empty-array – Bertuz Dec 17 '15 at 16:08