In our example below, we will be adding translation to a node entity and therefore use the node namespace.

use Drupal\node\Entity\Node;

$nid = 10; // existing node id in main language.
$node = Node::load($nid); // loading node array in var

$node_trans = $node->addTranslation("FR");
//ISO lang code for translation(FR for French).

$node_trans->title = "Un titre";

$node_trans->body = [
  'value' => "<p> Une paragraphe </p>",
  'format' => "full_html"
];

$node_trans->status = 1; //1 for pubished state.
// adding metatag to translated content
$node_trans->field_metatag = serialize([
   'title' => "tag",
   'description' => "Some desc",
]);

$node->save(); // All done! translation will be created here.

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *