Locastic / ApiPlatformTranslationBundle

Translation bundle for ApiPlatform based on Sylius translation
MIT License
85 stars 28 forks source link

Locastic Api Translation Bundle

Translation bundle for ApiPlatform based on Sylius translation

Installation:

$ composer require locastic/api-platform-translation-bundle

Implementation:

Translatable entity:

class Post extends AbstractTranslatable { // ...

protected function createTranslation(): TranslationInterface
{
    return new PostTranslation();
}

}


- Add a `translations`-property. Add the `translations` serializations group and make a connection to the translation entity:
``` php
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;

class Post extends AbstractTranslatable
{
    // ...

    /**
     * @ORM\OneToMany(targetEntity="PostTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
     *
     * @Groups({"post_write", "translations"})
     */
    protected $translations;
}

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();
}

}


**Translation entity:**
- Add entity with all translatable fields. Name needs to be name of translatable entity + Translation
- Extend `Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslation`
- Add serialization group `translations` to all fields and other read/write groups.
Example Translation entity:
``` 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;
    }
}

Api resource

Usage:

Language param for displaying single translation:

?locale=de

Or use Accept-Language http header

Accept-Language: de

Serialization group for displaying all translations:

?groups[]=translations

POST translations example

{
    "datetime":"2017-10-10",
    "translations": { 
        "en":{
            "title":"test",
            "content":"test",
            "locale":"en"
        },
        "de":{
            "title":"test de",
            "content":"test de",
            "locale":"de"
        }
    }
}

EDIT translations example

{
    "datetime": "2017-10-10T00:00:00+02:00",
    "translations": {
        "de": {
          "id": 3,
          "title": "test edit de",
          "content": "test edit de",
          "locale": "de"
        },
        "en": {
          "id": 2,
          "title": "test edit",
          "content": "test edit",
          "locale": "en"
        }
    }
}

Contribution

If you have idea on how to improve this bundle, feel free to contribute. If you have problems or you found some bugs, please open an issue.

Support

Want us to help you with this bundle or any Api Platform/Symfony project? Write us an email on info@locastic.com