Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error loading translations from database #388

Open
nithiz opened this issue Feb 17, 2021 · 11 comments
Open

Error loading translations from database #388

nithiz opened this issue Feb 17, 2021 · 11 comments
Assignees
Labels

Comments

@nithiz
Copy link

nithiz commented Feb 17, 2021

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
Copy link
Author

nithiz commented Feb 17, 2021

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 bartmcleod added the bug label Feb 17, 2021
@bartmcleod bartmcleod self-assigned this Feb 17, 2021
@bartmcleod
Copy link
Collaborator

@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
Copy link
Author

nithiz commented Feb 23, 2021

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

@bartmcleod
Copy link
Collaborator

@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
Copy link
Author

nithiz commented Feb 23, 2021

@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
Copy link
Collaborator

@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
Copy link

jawira commented Jan 13, 2022

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
Copy link
Collaborator

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
Copy link

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
Copy link

info-refactory commented Oct 3, 2022

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants