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-languageNote that events cannot be selected for translation.
12.2. Make events translatable
Section titled “12.2. Make events translatable”-
Delete all existing events
-
Delete the Events and Event teaser views
-
Add the following to the attributes in
src/Entity/Event.php:
-
Add the following to the
entity_keyspart of the attributes insrc/Entity/Event.php:Like for the
id,uuidandtypefields, the field definition for thelangcodefield is automatically generated byContentEntityBase::baseFieldDefinitions(). -
Run
drush entity:updatesNote that the
{event_field_data}table has been created and thetypecolumn 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,pathandchangedfields in thebaseFieldDefinitions()method of theEventclass before the semicolon: -
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:
-
Add the following to the form handlers part of the attributes in
src/Entity/Event.php: -
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