Making content translatable - DoSomething/legacy-website GitHub Wiki

Overview

We support multilingual content with help of Entity Translation module in combination with Title module.

Entity Translation

The Entity Translation module allows entities (such as nodes) to be translated into different languages. Entity Translation provides field-level translation, so that site builders can select which fields should be translatable. It provides a UI for users to translate these fields into other languages. Entity Translation is now in Drupal 8 core1.

We have decided to use Entity Translation because content translations share the same DB identificator (id), as the original content. For example Clean and Green campaign is always 3413, doesn't matter if it's in Portuguese or Spanish.

This is achieved through utilizing Drupal Field Language API native support. All fields use the following structure:

$entity->{$field_name}[$langcode][$delta][$column_name]
Enabling Nodes

Let's say, you want to enable translation for Is Staff Pick field, attached to Campaign content type.

Enable content type translation
  1. Make sure field translation is enabled for Campaigns:

image

Enable field translation
  1. Go to Content -> Manage Fields
  2. Click Edit on "Is Staff Pick", scroll down to "IS STAFF PICK FIELD SETTINGS".
  3. Click "Enable translation" it will take you to the following dialog

image

  1. Uncheck "Copy translation". We'll do it later using drush.
  2. Confirm
  3. Find out to which feature the field is saved to:
drush fc field_base | grep field_staff_pick
field_base:field_staff_pick                Provided by: dosomething_campaign
  1. Update dosomething_campaign feature: drush fu dosomething_campaign

  2. Branch, commit, push, create PR.

  3. Copy existing translation using custom drush command:

❯ drush ds-global-field-fix field_staff_pick Replacing field values for field_staff_pick. Data successfully processed.


  You'll need to do it on production.
1. If have multiple fields to enable translations for and you want to make sure you don't forget to run `drush ds-global-field-fix` for each one, use `ds-global` here, see example in [#5892](https://github.com/DoSomething/phoenix/pull/5892/files). You still are going to need to run `ds-global` on production.

##### Fixing field display

1. Ensure Field is translated correctly and looks as expected. If no, make sure the variables are loaded properly, for example when entity_metadata_wrapper is used, current language must be set:

   ```php
$wrapper = entity_metadata_wrapper('node', $node);
global $language_content;
$wrapper->language($language_content->language);

or

$wrapper->language(dosomething_helpers_get_current_language_content_code());
  1. Search for the field_staff_pick in all project and make sure it is not accessed through LANGUAGE_NONE constant. Example:

$tiles['is_staff_pick'] = $node->field_staff_pick[LANGUAGE_NONE][0]['value'];


  `LANGUAGE_NONE` should be replaced with `dosomething_helpers_get_current_language_content_code()`.

##### Enabling Taxonomony
Taxonomy Terms translation is already enabled for all vocabularies. You only can enable translation per specific fields attached to a vocabulary.

#### Todo
- Explain Title module
- Content type translation settings, saving it to features

#### Useful links
- [Multilingual site prototype ](https://github.com/DoSomething/phoenix/issues/3720)
⚠️ **GitHub.com Fallback** ⚠️