-
Notifications
You must be signed in to change notification settings - Fork 18
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
currentLocale not being set from Request #13
Comments
Can you verify that both listeners are actually executed? The LocaleListener should be executed first (it copies the locale from the request to the translatable listener) and the translatable listener should be executed second (it copies the locale to the entity when loaded). One thing that comes to mind is that the translatable listener is only attached to the "default" entity manager. Are you using a different entity manager? |
Hi sandermarechal, thank you for your reply. When loading a translatable entity for editing, both listeners are indeed executed, and currentLocale is successfully set. I guess that the issue I was having is when trying to add a new translatable entity. The currentLocale not set exception occurs when injecting the new entity into createForm:
Looking inside the TranslatableListener, it seems that currentLocale is set on the "postLoad" event. Since the entity is not loaded from the database in the above example, the "postLoad" event does not fire for it, and currentLocale is not set. So it seems that I should set currentLocale manually for new entities like so:
It works, but I would still get problems when adding a collection of translatable entities in a OneToMany relationship with the Product. Is there a better way to do it? I noticed you mentioned the a2lix Translation Bundle: https://github.com/Prezent/doctrine-translatable-bundle/blob/master/Resources/doc/index.md#integration-with-forms-and-sonata-admin Is this the answer for what I need? |
I don't think the a2lix bundle sets your locale automatically for new entities in one-to-many relationships, but it should work for the rest I think. I think you should use the
|
You are awesome man, thank you! I had not made use of the empty_data option before, and seeing your suggestion now got me reading up about it in the Symfony cookbook. Looks like a really useful option which I have been overlooking. I tested your suggestion, and it works like a charm. Thanks for taking the time to help me out on this. |
I may be having a similar issue to what was mentioned here: #3
Basically, currentLocale is not being automatically set from the Request for me.
I need to manually set it in my Controller for things to work:
If don't set it manually, I get the exception: 'No locale has been set and currentLocale is empty'
I noticed in previous issue #3 that you mentioned that both
Prezent\Doctrine\TranslatableBundle\EventListener\LocaleListener
andPrezent\Doctrine\Translatable\EventListener\TranslatableListener
should appear in app/cache/[env]/ProjectContainer.In my app/cache/dev/appDevDebugProjectContainer.php I see this:
Below is my code for the translatable and translation entities which I am using to test this bundle. Please let me know if there is anything I might be doing wrong, or if more information is needed.
Translatable entity Product.php:
Translation entity ProductTranslation.php:
The text was updated successfully, but these errors were encountered: