Symfony 3.0.8で構築された多言語アプリケーションがありますが、すべてのリテラルが翻訳されていますが、フォームでは__toStringメソッドを使用しているため動作していません。これは私のエンティティです:symfonyエンティティ内のロケールを取得
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Annotation\UdalaEgiaztatu;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
/**
* Atala
*
* @ORM\Table(name="atala", indexes={@ORM\Index(name="ordenantza_id_idx", columns={"ordenantza_id"})})
* @ORM\Entity
* @ExclusionPolicy("all")
* @UdalaEgiaztatu(userFieldName="udala_id")
*/
class Atala
{
/**
* @var integer
*
* @ORM\Column(name="id", type="bigint", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
* @Expose
*
* @ORM\Column(name="kodea", type="string", length=9, nullable=true)
*/
private $kodea;
/**
* @var string
* @Expose
*
* @ORM\Column(name="izenburuaeu", type="string", length=255, nullable=true)
*/
private $izenburuaeu;
/**
* @var string
* @Expose
*
* @ORM\Column(name="izenburuaes", type="string", length=255, nullable=true)
*/
private $izenburuaes;
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="datetime", nullable=false)
*/
private $createdAt;
/**
* @var \DateTime
*
* @ORM\Column(name="updated_at", type="datetime", nullable=false)
*/
private $updatedAt;
public function __construct()
{
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
}
public function __toString()
{
return $this->getKodea() . " - " . $this->getIzenburuaeu();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set kodea
*
* @param string $kodea
*
* @return Atala
*/
public function setKodea($kodea)
{
$this->kodea = $kodea;
return $this;
}
/**
* Get kodea
*
* @return string
*/
public function getKodea()
{
return $this->kodea;
}
/**
* Set izenburuaeu
*
* @param string $izenburuaeu
*
* @return Atala
*/
public function setIzenburuaeu($izenburuaeu)
{
$this->izenburuaeu = $izenburuaeu;
return $this;
}
/**
* Get izenburuaeu
*
* @return string
*/
public function getIzenburuaeu()
{
return $this->izenburuaeu;
}
/**
* Set izenburuaes
*
* @param string $izenburuaes
*
* @return Atala
*/
public function setIzenburuaes($izenburuaes)
{
$this->izenburuaes = $izenburuaes;
return $this;
}
/**
* Get izenburuaes
*
* @return string
*/
public function getIzenburuaes()
{
return $this->izenburuaes;
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
*
* @return Atala
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* @param \DateTime $updatedAt
*
* @return Atala
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
}
私がしたいのは、ロケールに応じて値を返すtoStringメソッドを変更することです。このようなものは素晴らしいでしょう:
public function __toString()
{
if ($locale == "es") {
return $this->getKodea() . " - " . $this->getIzenburuaes();
} else {
return $this->getKodea() . " - " . $this->getIzenburuaeu();
}
}
しかし、私はモデル内の_localeをどのように知っていますか?
助けや手がかりはありますか? 、あなたが行うことができますあなたのエンティティで
データベースにマップされたエンティティに '__constructor'で設定したデフォルト値(たとえば 'es')を使って新しい属性を追加し、それを次のようにテストすることができます:' if($ this - > locale == 'es') '... – DOZ
yapですが、現在のロケールと比較する必要があります –
この解決方法では、現在のロケール<=>' private $ locale 'を使用すると、ロケールの変更時にこの属性を設定する必要があります – DOZ