How to display a message in the default locale when the message is not available in the current locale ? Prezent Translations

依然范特西╮ 提交于 2019-12-11 06:47:54

问题


I'm working on a multilingual website, using the Prezent bundle to translate my entities.

Actually, the input in all the locales works, but I have some issues to display messages when they are not defined in the current locale.

Here is an extract of my Category entity (the field "name" is translated) :

/**
 * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
 */
class Category extends TranslatableEntity
{

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    protected $id;


    /**
     * @ORM\OneToMany(targetEntity="CategoryTranslation", mappedBy="translatable", cascade={"persist", "remove"}, indexBy="locale")
     */
    protected $translations;


    public function __construct()
    {
        $this->translations = new ArrayCollection();
        $this->translationEntity = 'CategoryTranslation';
    }


    public function getId(){
        return $this->id;
    }

    public function setId($id){
        $this->id = $id;
    }

    public function getName()
    {
        return $this->translate()->getName();
    }

    public function setName($name){
        $this->translate()->setName($name);
        return $this;
    }
}

The translate method is in TranslatableEntity, here is the code :

abstract class TranslatableEntity extends AbstractTranslatable
{

    /**
     * @Prezent\CurrentLocale
     */
    protected $currentLocale;

    protected $translationEntity;

    /**
     * Cache current translation. Useful in Doctrine 2.4+
     */
    protected $currentTranslation;

    public function getCurrentLocale()
    {
        return $this->currentLocale;
    }

    public function setCurrentLocale($locale)
    {
        $this->currentLocale = $locale;
        return $this;
    }

    /**
     * Translation helper method
     */
    public function translate($locale = null)
    {
        if (null === $locale) {
            $locale = $this->currentLocale;
        }

        if (!$locale) {
            throw new \RuntimeException('No locale has been set and currentLocale is empty');
        }

        if ($this->currentTranslation && $this->currentTranslation->getLocale() === $locale) {
            return $this->currentTranslation;
        }

        if (!$translation = $this->translations->get($locale)) {
            $className=$this->translationEntity;
            $translation = new $className;
            $translation->setLocale($locale);
            $this->addTranslation($translation);
        }

        $this->currentTranslation = $translation;
        return $translation;
    }

}

I use this way to display the translated names in Twig :

{{ cat.translations.get(app.request.locale).name }}

This works but I'm pretty sure that it is not the right way to do it. Moreover, the method throws an error when I try to display a name which not defined in the current locale.

I think that ...

{{ cat.translations.get(app.request.locale).name is defined ? cat.translations.get(app.request.locale).name  : cat.translations.get(default_locale).name }}

.. would solve but I'm also pretty sure that the case of "not available for this locale" is supported by the bundle.

Do you have any idea of what I am doing wrong ?

来源:https://stackoverflow.com/questions/56929994/how-to-display-a-message-in-the-default-locale-when-the-message-is-not-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!