Closed fecou closed 3 weeks ago
So had the same problem : The translation listener could not be found
. I found a fix, sorry if it's not the right explanation but it works for me for now.
I have
"php": ">=8.3", "doctrine/doctrine-bundle": "^2.13", "doctrine/orm": "^3.2.2"
it was working in 6.4, but at equals other packages not working in 7.0 and after.
So this is my original configuration,
Gedmo\Translatable\TranslatableListener:
calls:
- [setTranslatableLocale, ['%locale%']] # Set locale, typically the current request's locale
- [setDefaultLocale, ['%locale%']] # Set default locale, fallback to the default app locale
tags:
- { name: doctrine.event_subscriber, connection: default }
I tried different things as it is deprecated to declared events like that but from this doc it should still works. https://symfony.com/doc/7.0/doctrine/events.html
So I tried with the new way using AsDoctrineListener, and made a listener like this ;
<?php
namespace App\Listeners;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\Events;
use Gedmo\Translatable\TranslatableListener;
#[AsDoctrineListener(event: Events::postLoad, priority: 0, connection: 'default')]
#[AsDoctrineListener(event: Events::postPersist, priority: 0, connection: 'default')]
#[AsDoctrineListener(event: Events::preFlush, priority: 0, connection: 'default')]
#[AsDoctrineListener(event: Events::onFlush, priority: 0, connection: 'default')]
#[AsDoctrineListener(event: Events::loadClassMetadata, priority: 0, connection: 'default')]
class WatbookTranslatableListener extends TranslatableListener
{
}
You don't need to subclass the listener, just update your config to match the updated documentation.
Thanks I was looking for something like that, looked in the wrong place.
Thank you for your answers. It works much better know. For example I can know make fixtures with translations. But it still doesnt work in my edit form with a translatable field. When I look in the database, I have a new ext_translations, so it seems ok. But the language is "en_US" instead of "en". When I assign the language, I assign "en_US" but in the route I only have "en". Maybe it explains why if in my controller I put dd($request->getLocale()); it prints "en" instead of "en_US". There is an inconsistency there that doesnt allow me to use the doctrine extension with translatable. Any idea how to solve that ?
In services.yaml I have :
parameters: locale: 'fr_FR' # gedmo translatable
and
gedmo.mapping.driver.attribute:
class: Gedmo\Mapping\Driver\AttributeReader
# Gedmo Translatable Extension Listener (see https://github.com/doctrine-extensions/DoctrineExtensions/blob/main/doc/frameworks/symfony.md#registering-extension-listeners)
gedmo.listener.translatable:
class: Gedmo\Translatable\TranslatableListener
tags:
- { name: doctrine.event_listener, event: 'postLoad' }
- { name: doctrine.event_listener, event: 'postPersist' }
- { name: doctrine.event_listener, event: 'preFlush' }
- { name: doctrine.event_listener, event: 'onFlush' }
- { name: doctrine.event_listener, event: 'loadClassMetadata' }
calls:
# Uncomment the below call if using attributes, and comment the call for the annotation reader
- [ setAnnotationReader, [ '@gedmo.mapping.driver.attribute' ] ]
# The `annotation_reader` service was deprecated in Symfony 6.4 and removed in Symfony 7.0
# - [ setAnnotationReader, [ '@annotation_reader' ] ]
# The Kernel's `locale` parameter is used to configure the default locale for the extension
- [ setDefaultLocale, [ '%locale%' ] ]
In doctrine.yaml, I have :
doctrine:orm:mappings: translatable: type: attribute alias: GedmoTranslatable prefix: Gedmo\Translatable\Entity dir: '%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Translatable/Entity'
And in translations.yaml, I have :
framework: default_locale: fr_FR enabled_locales: ['fr_FR', 'en_US'] translator: default_path: '%kernel.project_dir%/translations' fallbacks:
What is really strange is that in my entry in ext_translations table I change "fr_FR" to "fr", matching in fact with the $request->getLocale(), twig stills serving the field in english. So it's as if symfony wouldnt translate and ignores ext_translations entries.
Reading the documentation again, at https://github.com/doctrine-extensions/DoctrineExtensions/blob/main/doc/frameworks/symfony.md#configuring-extensions-via-event-subscribers, I realize I didn't add the event listener. So I have : `<?php
namespace App\EventListener;
use Gedmo\Translatable\TranslatableListener; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
final class GedmoExtensionsEventSubscriber implements EventSubscriberInterface { public function __construct( private TranslatableListener $translatableListener, private ?AuthorizationCheckerInterface $authorizationChecker = null, private ?TokenStorageInterface $tokenStorage = null, ) {}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => [
['configureTranslatableListener'], // Must run after the locale is configured
],
];
}
/**
* Configures the translatable listener using the request locale
*/
public function configureTranslatableListener(RequestEvent $event): void
{
$this->translatableListener->setTranslatableLocale($event->getRequest()->getLocale());
}
}`
But adding this listener gives me this error :
Cannot autowire service "App\EventListener\GedmoExtensionsEventSubscriber": argument "$translatableListener" of method "__construct()" references class "Gedmo\Translatable\TranslatableListener" but no such service exists.
What is the problem there ? Thank you !
In services.yam, I added : `App\EventListener\GedmoExtensionsEventSubscriber: arguments:
It works know !
Environment
Symfony 6.4 Doctrine ORM 3.1
Package
"gedmo/doctrine-extensions": "^3.16",
Doctrine packages
"doctrine/annotations": "^2.0", "doctrine/dbal": "^3", "doctrine/doctrine-bundle": "^2.12", "doctrine/doctrine-migrations-bundle": "^3.3", "doctrine/orm": "^3.1",
PHP version
Subject
I installed doctrine extensions and doctrine annotations. in config/doctrine.yaml :
mappings : translatable: type: attribute is_bundle: false prefix: Gedmo\Translatable\Entity dir: '%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Translatable/Entity' alias: GedmoTranslatable
I have a country entity : ` ... use Gedmo\Mapping\Annotation as Gedmo; use Gedmo\Translatable\Translatable;
[ORM\Entity(repositoryClass: CountryRepository::class)]
class Country implements Translatable {
... public function setTranslatableLocale($locale) { $this->locale = $locale; } ... `
I have also a fixture
... $repository = $em->getRepository('Gedmo\\Translatable\\Entity\\Translation'); ... $repository->translate($country, 'nom', 'fr', 'Allemagne')
But if I do that, I get error : The translation listener could not be foundAlso if I open a form switching locale in french. I write a name of the country in french. I save. After I switch to english. I write a name of the country in english. I save.
I expect that when something to be written in the table ext_translations, but nothing is written. The attribute #[Gedmo\Translatable] is ignored. My fields with this attribute aren't translatable.