1

I am adding Arabic translations to an English website. For every article node, I need to individually translate each node.

Is there a way to add translations in one go?

The customer will update the content, but I need to create the initial translation of more than 500 nodes.

1

You can get all nodes, then with a foreach create a translation for each node.

$nodes = \Drupal::entityQuery("node")
//->condition('type', 'my_custom_type') //use this for specific content types
->execute();

$nodes =  \Drupal\node\Entity\Node::loadMultiple($nids);

foreach ($nodes as $node) {
  //Creating the translation Spanish in this case
  $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';
  //Saving the node
  $node_es->save();
}

You can add some fields like title and body, or just save.

  • Thanks Carlos, If I want to copy all the content in translated node, then any way to do that? – Sheena Rawal Feb 1 '18 at 15:35
  • Actually if you don't update any field it should copy all content, so do addTranslation then save. – Carlos Feb 1 '18 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.