我将Sonata admin软件包与Symfony 3.4和knplabs一起使用。一切正常,除了一件事。
我用CRUD创建了一个Test类和奏鸣曲列表。 此类具有可翻译的标题,当我处于测试对象上的标题的编辑模式时,可以单击标记以多种语言编辑标题。
但是当我在列表视图中时,会显示标志,但是当我单击它时,列表将始终以英语(默认语言)显示标题。
我调试并发现在编辑视图中,方法setLocale和getLocal用于更改语言,但在列表视图中,它们不是调用语言。
如何在列表视图中转换实体?
这里是我的测试班,翻译测试班和testAdmin。
Test.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
use Sonata\TranslationBundle\Model\TranslatableInterface;
/**
* @ORM\Entity(repositoryClass="App\Repository\TestRepository")
*/
class Test implements TranslatableInterface
{
use ORMBehaviors\Translatable\Translatable;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="boolean")
*/
private $is_enable;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->translate(null, false)->getTitle();
}
public function setTitle(string $title): self
{
$this->translate(null, false)->setTitle($title);
return $this;
}
public function getIsEnable(): ?bool
{
return $this->is_enable;
}
public function setIsEnable(bool $is_enable): self
{
$this->is_enable = $is_enable;
return $this;
}
/**
* @param string $locale
*/
public function setLocale($locale)
{
$this->setCurrentLocale($locale);
return $this;
}
/**
* @return string
*/
public function getLocale()
{
return $this->getCurrentLocale();
}
/**
* @return string
*
* Set this to have a correct name display on BO (sonata translation add some weird id key after the name)
*/
public function __toString()
{
if (empty($this->getTitle())){
return '';
}
else{
return $this->getTitle();
}
}
}
TestTranslation.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* @ORM\Table(name="test_translation")
* @ORM\Entity
*/
class TestTranslation
{
use ORMBehaviors\Translatable\Translation;
/**
* @var string
*
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @param string $title
*
* @return TestTranslation
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
}
TestAdmin.php
<?php
namespace App\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
class TestAdmin extends AbstractAdmin
{
protected $baseRoutePattern = 'test';
protected $baseRouteName = 'test';
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title', TextType::class)
->add('is_enable', TextType::class)
;
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('id')
->add('title')
->add('is_enable')
->add('_action', 'actions', array(
'actions' => array(
'edit' => array(),
'delete' => array(),
)
))
;
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('id');
$datagridMapper->add('translations.title', null, array('label' => 'Title'));
$datagridMapper->add('is_enable');
}
}
答案 0 :(得分:0)
return (string)$this->getTranslations()->get($locale);
在__toString方法Test.php中使用以上代码