I'm using doctrine translatable extensiın for symfony2 but this is not working.I'm thinking I tried everything. I am very glad if solve this problem.
config.yml
- { resource: doctrine_extensions.yml }
...
translator: { fallbacks: ["%locale%"] }
...
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
mappings:
translatable:
type: annotation
alias: Gedmo
prefix: Gedmo\Translatable\Entity
# make sure vendor library location is correct
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity"
doctrine_extensions.yml
# services to handle doctrine extensions
# import it in config.yml
strong textservices:
# KernelRequest listener
extension.listener:
class: MyApp\MyBundle\EventListener\DoctrineExtensionListener
calls:
- [ setContainer, [ @service_container ] ]
tags:
# translatable sets locale after router processing
- { name: kernel.event_listener, event: kernel.request, method: onLateKernelRequest, priority: -10 }
# loggable hooks user username if one is in security context
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
# Doctrine Extension listeners to handle behaviors
gedmo.listener.translatable:
class: Gedmo\Translatable\TranslatableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]
- [ setDefaultLocale, [ %locale% ] ]
- [ setTranslationFallback, [ false ] ]
gedmo.listener.loggable:
class: Gedmo\Loggable\LoggableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]
DoctrineExtensionListener.php
<?php
namespace MyApp\MyBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
DoctrineExtensionListener implements ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
protected $container;
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
public function onLateKernelRequest(GetResponseEvent $event)
{
$translatable = $this->container->get('gedmo.listener.translatable');
$translatable->setTranslatableLocale($event->getRequest()->getLocale());
}
public function onKernelRequest(GetResponseEvent $event)
{
$securityContext = $this->container->get('security.context', ContainerInterface::NULL_ON_INVALID_REFERENCE);
if (null !== $securityContext && null !== $securityContext->getToken() && $securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
$loggable = $this->container->get('gedmo.listener.loggable');
$loggable->setUsername($securityContext->getToken()->getUsername());
}
}
}
And
$em = $this->getContainer()->get('doctrine')->getEntityManager();
$article = new Category();
$article->setName('Home');
$article->setSlug('home');
$article->setCreatedAt(new \DateTime());
$article->setUpdatedAt(new \DateTime());
$article->setTranslatableLocale('en');
$em->persist($article);
$em->flush();
$article->setName('Ev');
$article->setSlug('ev');
$article->setCreatedAt(new \DateTime());
$article->setUpdatedAt(new \DateTime());
$article->setTranslatableLocale('tr');
$em->persist($article);
$em->flush();
what could be wrong?