How to create a translatable field

Translatable fields let you store objects’ data in several languages. If you have a translatable field and want to retrieve the data you saved in a given language you have to pass a request parameter named _locale to the API endpoint, with locale code given defined in the admin panel.

Let’s say you want to add a translatable field containing a brand name to the API. To do that, add a translatable field type to the form:

<?php
    $builder->add('translations', TranslationsType::class, [
        'required' => true,
        'fields' => [
            'brandName' => [
                'field_type' => TextType::class,
            ],
        ],
    ]);

Next, we need to create a mapping for entity translation. Because brandName is a field of Campaign objects, we create a CampaignTranslation entity. Here is a Doctrine definition:

OpenLoyalty\Domain\Campaign\CampaignTranslation:
  type: entity
  fields:
    brandName:
      type: text
      nullable: true
      column: brand_name

and entity class body with FallbackTranslation trait:

Next we need to add FallbackTranslatable trait in the OpenLoyaltyDomainCampaignCampaign class

and modify setFromArray method if it exists:

You also need to add translation setters and getters, which will be responsible for modifying and returning the translated data

/**
 * @return string|null
 */
public function getBrandName(): ?string
{
    return $this->translateFieldFallback(null, 'brandName')->getBrandName();
}

/**
 * @param string|null $brandName
 */
public function setBrandName(?string $brandName): void
{
    $this->translate(null, false)->setBrandName($brandName);
}