Translate a node programmatically

Submitted by editor on Sat, 11/28/2015 - 19:01
Question

How to translate a node programmatically and how to get a field translated in Drupal 8

Exemples:

$translation = $entity->getTranslation('de');

$translation = \Drupal::entityManager()->getTranslationFromContext($entity);

$node_es = $node->addTranslation('es');
$node_es->title = 'Mi prueba!';
$node_es->body->value = '<p>El cuerpo de mi nodo.</p>';
$node_es->body->format = 'full_html';
$node_es->save();

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.