我使用https://github.com/Atlantic18/DoctrineExtensions,我希望在我的项目中可以翻译和缓慢使用。首先我使用可翻译,默认语言是波兰语,翻译成英语和德语。一切都很美妙,添加,编辑,删除。然后我使用了sluggable,也很好用,但只使用默认语言。但是不会为其他语言创建一个slug。
config.yml:
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
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"
services.yml:
parameters:
# portal_cms.example.class: Portal\CmsBundle\Example
services:
extension.listener:
class: Portal\CmsBundle\EventListener\DoctrineExtensionListener
calls:
- [ setContainer, [ @service_container ] ]
tags:
# translatable sets locale after router processing
- { name: kernel.event_listener, event: kernel.request, method: onLateKernelRequest, priority: -10 }
gedmo.listener.sluggable:
class: Gedmo\Sluggable\SluggableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]
gedmo.listener.translatable:
class: Gedmo\Translatable\TranslatableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ @annotation_reader ] ]
- [ setDefaultLocale, [ %locale% ] ]
- [ setTranslationFallback, [ false ] ]
# portal_cms.example:
# class: %portal_cms.example.class%
# arguments: [@service_id, "plain_value", %parameter%]
DoctrineExtensionListener.php:
<?php
namespace Portal\CmsBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class 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());
}
}
实体Tag.php:
<?php
namespace Portal\CmsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Translatable\Translatable;
/**
* Tag
*
* @ORM\Table()
* @ORM\Entity
*/
class Tag implements Translatable
{
/**
* @ORM\OneToMany(targetEntity="ArticleTag", mappedBy="tag")
*/
protected $articleTags;
public function __construct()
{
$this->articleTags = new ArrayCollection();
}
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\Column(name="created", type="datetime", nullable=true)
*/
private $created;
/**
* @var string
*
* @Gedmo\Translatable
* @Gedmo\Slug(fields={"name"})
* @ORM\Column(name="url", type="string", length=150, unique=true)
*/
private $url;
/**
* @var string
*
* @Gedmo\Translatable
* @ORM\Column(name="name", type="string", length=150)
*/
private $name;
/**
* @Gedmo\Locale
* Used locale to override Translation listener`s locale
* this is not a mapped field of entity metadata, just a simple property
*/
private $locale;
private $translations;
/**
* Set created
*
* @param \DateTime $created
* @return Tag
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set url
*
* @param string $url
* @return Tag
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set name
*
* @param string $name
* @return Tag
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set translations
*
* @param string $translations
* @return Tag
*/
public function setTranslations($translations)
{
$this->translations = $translations;
return $this;
}
/**
* Get translations
*
* @return string
*/
public function getTranslations()
{
return $this->translations;
}
/**
* Add articleTags
*
* @param \Portal\CmsBundle\Entity\ArticleTag $articleTags
* @return Tag
*/
public function addArticleTag(\Portal\CmsBundle\Entity\ArticleTag $articleTags)
{
$this->articleTags[] = $articleTags;
return $this;
}
/**
* Remove articleTags
*
* @param \Portal\CmsBundle\Entity\ArticleTag $articleTags
*/
public function removeArticleTag(\Portal\CmsBundle\Entity\ArticleTag $articleTags)
{
$this->articleTags->removeElement($articleTags);
}
/**
* Get articleTags
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getArticleTags()
{
return $this->articleTags;
}
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
}
}
控制器
<?php
namespace Portal\CmsBundle\Controller;
use Portal\CmsBundle\Entity\Tag;
use Portal\CmsBundle\Entity\ArticleTag;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityRepository;
use Gedmo\Translatable\TranslatableListener;
class AdminTagsController extends AbstractController
{
public function indexAction(Request $request)
{
// action code here - negligible
}
public function addAction(Request $request)
{
if ($request->getMethod() == 'POST') {
$tag = new Tag();
$em = $this->getDoctrine()->getManager();
$formArray = $request->request->get('formTag');
$repository = $em->getRepository('PortalCmsBundle:Article');
$repository2 = $em->getRepository('Gedmo\\Translatable\\Entity\\Translation');
$tag->setName($formArray['name']);
$repository2->translate($tag, 'name', 'en', $formArray['name_en']);
$repository2->translate($tag, 'name', 'de', $formArray['name_de']);
$tag->setCreated(new \DateTime('now', new \DateTimeZone('Europe/Warsaw')));
$validator = $this->get('validator');
$errors = $validator->validate($tag);
if (count($errors) > 0) {
foreach ($errors as $error) {
$this->message .= $error->getPropertyPath().' <-- '.$error->getMessage().'
';
}
} else {
$em->persist($tag);
if (isset($formArray['articles'])) {
foreach ($formArray['articles'] as $article_id) {
$article = $repository->find((int)$article_id);
$article_tag = new ArticleTag($article, $tag);
$em->persist($article_tag);
}
}
$em->flush();
$this->message = 'Tag dodany';
}
return $this->redirect($this->generateUrl(
'admin_tags',
array(
'message' => $this->message,
'messageType' => $this->messageType
)
));
}
return $this->redirect($this->generateUrl(
'admin_tags'
));
}
// other actions
}
通常位于此处的部分文档:https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/sluggable.md#using-translationlistener-to-translate-our-slug对我来说并不完全清楚。是代码:
<?php
$evm = new \Doctrine\Common\EventManager();
$sluggableListener = new \Gedmo\Sluggable\SluggableListener();
$evm->addEventSubscriber($sluggableListener);
$translatableListener = new \Gedmo\Translatable\TranslationListener();
$translatableListener->setTranslatableLocale('en_us');
$evm->addEventSubscriber($translatableListener);
// now this event manager should be passed to entity manager constructor
我必须在某处插入,如果是的话,在哪里?信息无处可去。出于好奇,我把它放在addAction的控制器中并得到一个错误,无法找到\ Gedmo \ Translatable \ TranslationListener()。的确,在这个位置Gedmo没有这样的课程。这是文档中的错误,不应该在那里\ Gedmo \ Translatable \ TranslatableListener()?
第二个问题,或实体和数据库中的slug字段应该被称为'slug'?在我的情况下,它有另一个名称,但slug正确生成波兰语。这可能是翻译中一段时间的问题吗?
第三个问题,是否有必要在实体中添加这些字段:
<?php
/**
* @ORM\Column(type="string", length=64)
*/
private $uniqueTitle;
/**
* @Gedmo\Slug(fields={"uniqueTitle"}, prefix="some-prefix-")
* @ORM\Column(type="string", length=128, unique=true)
*/
private $uniqueSlug;
我会感激任何回应,也许是指导我解决这个问题。
答案 0 :(得分:1)
更改此行:
$translatableListener = new \Gedmo\Translatable\TranslationListener();
对此:
$translatableListener = new \Gedmo\Translatable\TranslatableListener();
请注意更改班级名称(ation
vs atable
)。