PHP code example of locastic / api-platform-translation-bundle

1. Go to this page and download the library: Download locastic/api-platform-translation-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

locastic / api-platform-translation-bundle example snippets

 php
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;

class Post extends AbstractTranslatable
{
    // ...
    
    protected function createTranslation(): TranslationInterface
    {
        return new PostTranslation();
    }
}
 php
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
use Symfony\Component\Serializer\Annotation\Groups;

class Post extends AbstractTranslatable
{
    // ...
    
    /**
    * @Groups({"post_read"})
    */
    private $title;
    
    public function setTitle(string $title)
    {
        $this->getTranslation()->setTitle($title);
    }

    public function getTitle(): ?string
    {
        return $this->getTranslation()->getTitle();
    }
}
 php
use Symfony\Component\Serializer\Annotation\Groups;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslation;

class PostTranslation extends AbstractTranslation
{
    // ...

    /**
     * @ORM\ManyToOne(targetEntity="Post", inversedBy="translations")
     */
    protected $translatable;
    
    /**
     * @ORM\Column(type="string")
     * 
     * @Groups({"post_read", "post_write", "translations"})
     */
    private $title;
    
    /**
     * @ORM\Column(type="string")
     *
     * @Groups({"post_write", "translations"})
     */
    protected $locale;

    public function setTitle(string $title): void
    {
        $this->title = $title;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }
}