Translation bundle for ApiPlatform based on Sylius translation
$ composer require locastic/api-platform-translation-bundle
Translatable entity:
- Extend your model/resource with
Locastic\ApiTranslationBundle\Model\AbstractTranslatable - Add
createTranslation()method which returns new object of translation Entity. Example:
useLocastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
useLocastic\ApiPlatformTranslationBundle\Model\TranslationInterface;
class Post extends AbstractTranslatable
{
// ...protectedfunctioncreateTranslation(): TranslationInterface
{
returnnewPostTranslation();
}
}- Add a
translations-property. Add thetranslationsserializations group and make a connection to the translation entity:
useLocastic\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;
}- Add virtual fields for all translatable fields, and add read serialization group. Getters and setters must call getters and setters from translation class. Example:
useLocastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
useSymfony\Component\Serializer\Annotation\Groups;
class Post extends AbstractTranslatable
{
// .../** * @Groups({"post_read"}) */private$title;
publicfunctionsetTitle(string$title)
{
$this->getTranslation()->setTitle($title);
}
publicfunctiongetTitle(): ?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
translationsto all fields and other read/write groups. Example Translation entity:
useSymfony\Component\Serializer\Annotation\Groups;
useLocastic\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;
publicfunctionsetTitle(string$title): void
{
$this->title = $title;
}
publicfunctiongetTitle(): ?string
{
return$this->title;
}
}Api resource
- Add
translation.groupsfilter if you would like to have option to return all translation objects in response. If you don't usetranslationsgroup, 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: GETput:
method: PUTnormalization_context:
groups: ['translations']collectionOperations:
get:
method: GETpost:
method: POSTnormalization_context:
groups: ['translations']attributes:
filters: ['translation.groups']normalization_context:
groups: ['post_read']denormalization_context:
groups: ['post_write']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"
}
}
}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.
Want us to help you with this bundle or any Api Platform/Symfony project? Write us an email on info@locastic.com