Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 4 Translations not displaying

So I have the following demo for the Symfony 4 framework which works fine and translates everything as expected:

$translator = new \Symfony\Component\Translation\Translator('en_GB');
$translator->addLoader('array', new \Symfony\Component\Translation\Loader\ArrayLoader());
$translator->addResource('array', [
    'sample' => 'The English translation goes here...',
], 'en_GB');
echo $translator->trans('sample');

However I can't seem to get the following demo to work:

echo $this->get('translator')->trans('sample');

The following config is in place:

/config/services.yaml:

parameters:
    locale: en

/config/packages/translation.yaml:

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

/translations/messages.en.yaml:

sample: The English translation goes here...

Dumping out the following:

$request->getLocale();
$request->getDefaultLocale();

Produces the following strings:

'en_GB'
'en'

And I've tried renaming the translation file to 'messages.en.yaml', 'messages.en_GB.yaml', 'messages.en_US.yaml' and then run "php bin/console cache:clear" afterwards to ensure the cache was fully cleared.

I can't seem to get the translations to display from the yaml file. There are no errors displaying but only the translation keys are visible.

Can anyone see if I'm making an obvious mistake somewhere or overlooking something?

PHP 7.2.0-2+ubuntu16.04.1+deb.sury.org+2 (cli) (built: Dec 7 2017 20:14:31) ( NTS ) Linux Mint 18, Apache2 Ubuntu.

like image 227
Donal.Lynch.Msc Avatar asked Jan 15 '18 19:01

Donal.Lynch.Msc


1 Answers

I had the same problem and i found a solution . Maybe this is a solution for someone else too. In my case the command

./bin/console cache:clear

didn't cleared the /var/cache/translations folder. After dropping this folder manually

rm -rf var/log/translations

the translations were loaded like expected.

I don't know why the cache:clear isn't working probably in this case, but at the moment i get along with it. Maybe i find some time next week to debug the reason.

like image 93
Didatus Avatar answered Sep 21 '22 23:09

Didatus