Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get locale within symfony entity

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?

like image 503
Kioko Kiaza Avatar asked Dec 04 '25 21:12

Kioko Kiaza


2 Answers

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};
}
like image 107
ArGh Avatar answered Dec 06 '25 12:12

ArGh


Symfony puts the request containing the locale in $GLOBALS['request'].

   if(isset($GLOBALS['request']) && $GLOBALS['request']) {
        $locale = $GLOBALS['request']->getLocale();    
        ...
   }
like image 30
Jérôme Gillard Avatar answered Dec 06 '25 12:12

Jérôme Gillard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!