Git Product home page Git Product logo

apiplatformtranslationbundle's Introduction

Locastic Api Translation Bundle

Translation bundle for ApiPlatform based on Sylius translation

Installation:

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

Implementation:

Translatable entity:

  • Extend your model/resource with Locastic\ApiTranslationBundle\Model\AbstractTranslatable
  • Add createTranslation method which returns new object of translation Entity. Example:
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;

class Post extends AbstractTranslatable
{
    // ...
    
    protected function createTranslation()
    {
        return new PostTranslation();
    }
}
  • Add translations serialization group to translations relation:
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;

class Post extends AbstractTranslatable
{
    // ...
    
    /**
     * @Groups({"post_write", "translations"})
     */
    protected $translations;
}
  • Add virtual fields for all translatable fields, and add read serialization group. Getters and setters must call getters and setters from translation class. Example:
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
use Symfony\Component\Serializer\Annotation\Groups;

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

        return $this;
    }

    public function getTitle()
    {
        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:
use Symfony\Component\Serializer\Annotation\Groups;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslation;

class PostTranslation extends AbstractTranslation
{
    // ...
    
    /**
     * @var string
     *
     * @Groups({"post_read", "post_write", "translations"})
     */
    private $title;
    
    /**
     * @var string
     * @Groups({"post_write", "translations"})
     */
    protected $locale;

    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

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

Api resource

  • Add translation.groups filter if you would like to have option to return all translation objects in response. If you don't use translations group, response will return only requested locale translation or fallback locale translation.
  • Add translations to normalization_context for PUT and POST methods to make sure they return all translation objects.
  • Example:
AppBundle\Entity\Post:
    itemOperations:
        get:
            method: GET
        put:
            method: PUT
            normalization_context:
                groups: ['translations']
    collectionOperations:
        get:
            method: GET
        post:
            method: POST
            normalization_context:
                groups: ['translations']
    attributes:
        filters: ['translation.groups']
        normalization_context:
            groups: ['post_read']
        denormalization_context:
            groups: ['post_write']

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 Platfrom/Symfony project? Write us an email on [email protected]

apiplatformtranslationbundle's People

Contributors

alanpoulain avatar antonioperic avatar arnoudthibaut avatar gonzaloalonsod avatar jewome62 avatar paullla avatar samnela avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.