lexik / LexikTranslationBundle

This Symfony bundle allow to import translation files content into the database and provide a GUI to edit translations.
MIT License
418 stars 255 forks source link

Error loading translations from database #388

Open nithiz opened 3 years ago

nithiz commented 3 years ago

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 commented 3 years ago

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?

bartmcleod commented 3 years ago

@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?

nithiz commented 3 years ago

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

bartmcleod commented 3 years ago

@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?

nithiz commented 3 years ago

@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) 
bartmcleod commented 3 years ago

@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.

jawira commented 2 years ago

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

bartmcleod commented 2 years ago

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

tmzwinkels commented 1 year ago

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

info-refactory commented 1 year ago

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).

pierre-dedi commented 4 months ago

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.