问题
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