I´ve got a multilingual app built in Symfony 3.0.8 and I have all literals translated but in forms it isn´t working because it uses __toString method. This is my entity:
<?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;
}
}
What I want is to change toString method to return the value depending on the locale. Something like this would be awesome:
public function __toString()
{
if ($locale == "es") {
return $this->getKodea() . " - " . $this->getIzenburuaes();
} else {
return $this->getKodea() . " - " . $this->getIzenburuaeu();
}
}
But how I know the _locale within a model?
any help or clue?
In your entity, you can do :
use Symfony\Component\Intl\Locale;
And then
$locale = Locale::getDefault();
Use a magic method to easily get the good translation
public function getIzenburua()
{
$locale = Locale::getDefault();
return $this->{'izenburua'.$locale};
}
Symfony puts the request containing the locale in $GLOBALS['request'].
if(isset($GLOBALS['request']) && $GLOBALS['request']) {
$locale = $GLOBALS['request']->getLocale();
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With