php - Symfony2 Doctrine可翻译扩展不起作用

php - Symfony2 Doctrine可翻译扩展不起作用,第1张

我正在使用doctrinetranslatableextensiınforsymfony2但这不起作用。我想我尝试了一切。如果解决这个问题,我很高兴。

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());
    }
}
   }

并且

     $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();

可能出错?

最佳答案:

0 个答案:

没有答案
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复