Translator::getMessages returns an empty array

♀尐吖头ヾ 提交于 2019-12-11 20:13:47

问题


I'm trying to use the translator.default service in a Controller, but any message I'm trying to translate by means of the trans method returns the untranslated key.

/**
 * @Route("/testT")
 */
public function testTAction(Request $req)
{
    $translator = $this->get('translator.default');
    $translated = $translator->trans('Hello from the other side');
    die(dump($translated));
}

In order to be sure my keys were correctly loaded I've done the following checks:

php app/console translation:update it --dump-messages

which outputs the following:

Translation Messages Extractor and Dumper
=========================================

 // Generating "it" translation files for "app folder"
 // Parsing templates...
 // Loading translation files...

Messages extracted for domain "messages" (1 messages)
-----------------------------------------------------

 * Hello from the other side


 [OK] 1 messages were successfully extracted    

my keys are stored under <root>/app/resources/translations/messages.it.yml and my locale, when requesting the translated key is well set up:

    $translator = $this->get('translator.default');
    die(dump($translator->getLocale()));

if called in my controller, this generates the following output: "it", which I guess it's correct. So: where am I wrong?

EDIT

Here is my <root>/app/Resources/translations/messages.it.yml

Hello from the other side: Ciao dall'altra parte

and Here is the chunk related to the translation service in my <root>/app/config/config.yml

framework:
    translator: { fallbacks: it }

I'm using the dev configuration, but nothing is related to the translation there.


回答1:


Have you cleared the cache after creating the new messages.it.yml file?

It has already happened to me that the translation appears as 'not found', even when I'm sure it's there. After clearing the cache it normally works.

Also for the future, you can run under app_dev and check the profiler to see the messages that are found and the missing messages, like this:

When you click at the translation icon, you should see a screen with a list like this:



来源:https://stackoverflow.com/questions/34338186/translatorgetmessages-returns-an-empty-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!