Having troubles with implementing translations in ApiPlatform?

Paula Lala
2 min readSep 25, 2018

--

So here you are, trying to implement translation for entities on your ApiPlatform app — but you can’t decide on the best way to do it.

Luckily for you, there’s a bundle that might help you. I named it LocasticApiTranslationBundle and it’s inspired by Sylius translation which uses the approach of personal translations.

Sylius is an Open Source eCommerce Framework based on Symfony full stack. The technology is constructed from fully decoupled components (bundles in Symfony glossary), which means that every feature (products catalog, shipping engine, promotions system…) can be used in any other application.

symfony.com

What’s the idea?

Each entity has a related translation entity which holds all translatable fields. Using serialization groups you can either return object translated to single language or return embedded collection with all available translations.

Let’s get down to coding

  1. Create translatable and translation classes

Assuming we would like to have Event entity with translatable fields, we need to make Event and EventTranslation classes. Let’s take a look at Event class:

Event needs to extend AbstractTranslatable class and implement method for creating new EventTranslation object. AbstractTranslatable class provides getTranslation() method which gives us access to our translation class. Because of this, we are able to make virtual fields for title and description and set their translated values. $translations variable is overwritten just to add group for writing Event object.

Translation class is somewhat simpler, only important thing is to extend AbstractTranslation and add corresponding serialization groups:

  • NOTE: for doctrine relation between Event and EventTranslation make sure you set fetch to “EXTRA_LAZY” and index-by to “locale”.

2. Create API resource

Next step is to create resource for Event and EventTranslation classes:

You should note two things here:

  • PUT and POST operations have translations normalization group, because we want to return full collection of translations for response.
  • We added translation.groups filter, so that client is able to request full translations response for any operation if necessary.

All ready!

Our translations are now ready for use and here is how:

POST Event with translations example

PUT Event with translations example

Only difference between POST and PUT request is id parameter in translation object which we need to send when editing.

Get response by locale

?locale=en

If you don’t specify locale, fallback locale from Symfony framework config will be used.

Get response with all available translations embedded:

?groups[]=translations

That’s all folks!

If you have any questions or suggestions on how to improve translation implementation, feel free to let us know :)

If you need help with your API Platform, Sylius or Symfony project, ping us: info@locastic.com

This creative piece was originally posted on Locastic.com — https://locastic.com/blog/having-troubles-with-implementing-translations-in-apiplatform/

--

--