Skip to content

12. Translating content

Content entities can be made translatable in the storage by amending the entity type attributes. However, this by itself does not make the content entity translatable in the user interface. It only allows site builders to make it translatable in the user interface with the Content Translation module.

12.1. Install the Content Translation module

Section titled “12.1. Install the Content Translation module”
  • Install the Content Translation module on /admin/modules

  • Add a second language on /admin/config/regional/language

  • Visit /admin/config/regional/content-language

    Note that events cannot be selected for translation.

  • Delete all existing events

  • Delete the Events and Event teaser views

  • Add the following to the attributes in src/Entity/Event.php:

    translatable: TRUE,
    data_table: 'event_field_data',
  • Add the following to the entity_keys part of the attributes in src/Entity/Event.php:

    'langcode' => 'langcode',

    Like for the id, uuid and type fields, the field definition for the langcode field is automatically generated by ContentEntityBase::baseFieldDefinitions().

  • Run drush entity:updates

    Note that the {event_field_data} table has been created and the type column has been added to the {event} table.

  • Verify that Events can be marked as translatable

    Note that only the Comments field is translatable.

  • Add the following to field definitions for the title, description, published, path and changed fields in the baseFieldDefinitions() method of the Event class before the semicolon:

    ->setTranslatable(TRUE)
  • Run drush entity:updates

  • Mark all fields of all event types as translatable

  • Add an event entity

    Note there is a Translate local task

    Notice the following exception is thrown when visiting the Translate local task:

    The "event" entity type did not specify a "default" form class.
  • Add the following to the form handlers part of the attributes in src/Entity/Event.php:

    'default' => EventForm::class,
  • Rebuild caches

    Run drush cache:rebuild

  • Translate entity

    Note that non-translatable fields are still shown. Editing these will change the values in the source translation

  • Verify that translation works

  • Re-add the Events view