LexikTranslationBundle icon indicating copy to clipboard operation
LexikTranslationBundle copied to clipboard

Error loading translations from database

Open nithiz opened this issue 4 years ago • 11 comments

Hi,

When using the newest 5.0.2 package i get an Warning: Invalid argument supplied for foreach() error on Translation/Translator.php (line 33). It seems that the GetDatabaseResourcesListener is never fired and $event->getResources() is always null. I'm using the minimal configuration.

This is on Symfony 5.1.7.

Any ideas?

nithiz avatar Feb 17 '21 11:02 nithiz

Adding $container->addCompilerPass(new RegisterListenersPass()); will solve the listener problem but will throw a new issue Constructing service "doctrine.orm.default_entity_manager" from a parent definition is not supported at build time.. What is the reason we want to load translations (from the database) in build time?

nithiz avatar Feb 17 '21 12:02 nithiz

@nithiz I sat down to fix this, but I realized you haven't described what you were trying to do. When do you see the error?

bartmcleod avatar Feb 22 '21 22:02 bartmcleod

@bartmcleod I'm seeing the error on page load.

nithiz avatar Feb 23 '21 07:02 nithiz

@nithiz It might be the Symfony version then. I tested against SF 5.2. Any specific page where this happens? I have no routes in my Symfony app. Can you pin it down to the exact call in your code that precedes the error?

bartmcleod avatar Feb 23 '21 08:02 bartmcleod

@bartmcleod It's really hard to figure out where exactly it goes wrong. By default the dispatched GetDatabaseResourcesEvent event is never really fired in build time causing the resources to be empty.

I'm running symfony 5.1.7 with php 7.4. I'm using Sulu as a CMS which uses a modified public/index.php but i don't think that is causing an issue.

It's not any specific page as the error is triggered in build time. Even running bin/console will trigger the error.

As a workaround i'm simply using my own Translator class and stripped out all caching:

class Translator extends \Lexik\Bundle\TranslationBundle\Translation\Translator
{
    /**
     * Add all resources available in database.
     */
    public function addDatabaseResources()
    {
        $event = new GetDatabaseResourcesEvent();
        $this->container->get('event_dispatcher')->dispatch($event);

        $resources = $event->getResources() ?: [];

        foreach ($resources as $resource) {
            if ($resource['locale'] === null) {
                continue;
            }

            $this->addResource('database', 'DB', $resource['locale'], $resource['domain']);
        }
    }
}

I added my stacktrace, it might help you figure out where it goes wrong.

ErrorException:
Warning: Invalid argument supplied for foreach()

  at /application/sulu/vendor/lexik/translation-bundle/Translation/Translator.php:33
  at Lexik\Bundle\TranslationBundle\Translation\Translator->addDatabaseResources()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:1567)
  at Symfony\Component\DependencyInjection\ContainerBuilder->callMethod()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:1116)
  at Symfony\Component\DependencyInjection\ContainerBuilder->createService()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:597)
  at Symfony\Component\DependencyInjection\ContainerBuilder->doGet()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:575)
  at Symfony\Component\DependencyInjection\ContainerBuilder->doGet()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:1214)
  at Symfony\Component\DependencyInjection\ContainerBuilder->doResolveServices()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:1162)
  at Symfony\Component\DependencyInjection\ContainerBuilder->doResolveServices()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:1064)
  at Symfony\Component\DependencyInjection\ContainerBuilder->createService()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:597)
  at Symfony\Component\DependencyInjection\ContainerBuilder->doGet()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:1214)
  at Symfony\Component\DependencyInjection\ContainerBuilder->doResolveServices()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:1162)
  at Symfony\Component\DependencyInjection\ContainerBuilder->doResolveServices()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:1064)
  at Symfony\Component\DependencyInjection\ContainerBuilder->createService()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:597)
  at Symfony\Component\DependencyInjection\ContainerBuilder->doGet()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:1214)
  at Symfony\Component\DependencyInjection\ContainerBuilder->doResolveServices()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:1162)
  at Symfony\Component\DependencyInjection\ContainerBuilder->doResolveServices()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:1064)
  at Symfony\Component\DependencyInjection\ContainerBuilder->createService()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:597)
  at Symfony\Component\DependencyInjection\ContainerBuilder->doGet()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:542)
  at Symfony\Component\DependencyInjection\ContainerBuilder->get()
     (/application/sulu/vendor/sulu/sulu/src/Sulu/Bundle/SnippetBundle/DependencyInjection/Compiler/SnippetAreaCompilerPass.php:26)
  at Sulu\Bundle\SnippetBundle\DependencyInjection\Compiler\SnippetAreaCompilerPass->process()
     (/application/sulu/vendor/symfony/dependency-injection/Compiler/Compiler.php:91)
  at Symfony\Component\DependencyInjection\Compiler\Compiler->compile()
     (/application/sulu/vendor/symfony/dependency-injection/ContainerBuilder.php:736)
  at Symfony\Component\DependencyInjection\ContainerBuilder->compile()
     (/application/sulu/vendor/symfony/http-kernel/Kernel.php:533)
  at Symfony\Component\HttpKernel\Kernel->initializeContainer()
     (/application/sulu/vendor/symfony/http-kernel/Kernel.php:131)
  at Symfony\Component\HttpKernel\Kernel->boot()
     (/application/sulu/vendor/symfony/http-kernel/Kernel.php:191)
  at Symfony\Component\HttpKernel\Kernel->handle()
     (/application/sulu/public/index.php:63) 

nithiz avatar Feb 23 '21 10:02 nithiz

@nithiz Sorry for my late reply. I remember encountering this issue, but I fixed it. So could it be something old is stuck in your config cache or in some custom configuration? I remember that I had to change quite a bit in the configuration to make this error go away and have the resources loaded.

bartmcleod avatar Mar 03 '21 09:03 bartmcleod

Hi @bartmcleod, do you remember what config file you changed ? I'm updating a project from Symfony 4.4 to 5.0 and I have the same issue.

As explained by another user the error comes from vendor/lexik/translation-bundle/Translation/Translator.php:43 (lexik/translation-bundle:v5.0.3):

        foreach ($resources as $resource) {
            $this->addResource('database', 'DB', $resource['locale'], $resource['domain']);
        }

This is the dump from $resource variable:

locale-null

Thanks in advance

jawira avatar Jan 13 '22 12:01 jawira

Hi @jawira, sorry, I don't remember. I had to step through it with the debugger to find the culprit is all I remember

bartmcleod avatar Jan 31 '22 19:01 bartmcleod

Quite an old 'bug' but for me it was caused / resolved because database entries in lexik_trans_unit didn't had a corresponding value in lexik_trans_unit_translations

tmzwinkels avatar Sep 28 '22 11:09 tmzwinkels

Quite an old 'bug' but for me it was caused / resolved because database entries in lexik_trans_unit didn't had a corresponding value in lexik_trans_unit_translations

@tmzwinkels thank you, this fixed it for me on Symfony 5.4 and TranslationBundle 5.2. (i.e. I deleted the orphan entry from lexik_trans_unit).

info-refactory avatar Oct 03 '22 09:10 info-refactory

Hello I encounter the same error when installing the plugin on sulu 2.5.12 and symfony 6.0. My database does not yet have the plugin tables. The error occurs because the GetDatabaseResourcesListener is not called when dispatching the GetDatabaseResourcesEvent.

pierre-dedi avatar Mar 05 '24 08:03 pierre-dedi