Developed with love by KnpLabs Hire us for your project!
32

ApiPlatformTranslationBundle

by locastic

Translation bundle for ApiPlatform based on Sylius translation

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: ``` php use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;

class Post extends AbstractTranslatable
{
// ...

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

}
```

  • Add translations serialization group to translations relation: ``` php 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: ``` php 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:
``` php
use Symfony\Component\Serializer\Annotation\Groups;
use Locastic\ApiTranslationBundle\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:
yaml
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

Serialization group for displaying all translations:

?groups[]=translations

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

EDIT translations example
json
{
"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 info@locastic.com

MIT License

Copyright (c) 2018 Locastic

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
  • Merge pull request #3 from alanpoulain/minor-fixes
    By web-flow, 7 months ago
  • Use Listener instead of Subscriber and remove references to ORM
    By alanpoulain, 7 months ago
  • Fix interfaces and traits
    By alanpoulain, 7 months ago
  • Improvements to Translator
    By alanpoulain, 7 months ago
  • Strict types
    By alanpoulain, 7 months ago
  • Merge pull request #2 from jewome62/feature/prefered-language-fallback
    By web-flow, 8 months ago
  • Add prefered Language as fallback language
    By , 8 months ago
  • Merge pull request #1 from samnela/patch-1
    By web-flow, 8 months ago
  • remove tests in the main autoload
    By web-flow, 8 months ago
  • Update README.md
    By web-flow, 8 months ago
  • autoload fix
    By , 8 months ago
  • phpunit
    By paullla, 8 months ago
  • remove 7.0 from travis
    By paullla, 8 months ago
  • phpunit
    By paullla, 8 months ago
  • Update README.md
    By web-flow, 8 months ago
  • Update README.md
    By web-flow, 8 months ago
  • typo
    By paullla, 8 months ago
  • typo
    By paullla, 8 months ago
  • Merge branch 'master' of github.com:Locastic/ApiPlatformTranslationBundle
    By paullla, 9 months ago
  • added scrutinizer
    By paullla, 9 months ago
  • added travis
    By paullla, 9 months ago
  • Update composer.json
    By web-flow, 9 months ago
  • readme
    By paullla, 9 months ago
  • readme
    By paullla, 9 months ago
  • Merge branch 'master' of github.com:Locastic/ApiPlatformTranslationBundle
    By paullla, 9 months ago
  • init commit
    By paullla, 9 months ago
  • Initial commit
    By web-flow, 9 months ago