8

I am using i18n module. How can I programmatically create node translation?

2 Answers 2

9
// Define which node is the source, this node has his own id as translation nid (tnid)
$source_node = node_load($source_nid);
$source_node->tnid = $source_nid;
node_save($source_node);

$new_node_in_a_other_language = new stdClass();
$new_node_in_a_other_language->type = 'your_type';
node_object_prepare($new_node_in_a_other_language);
$new_node_in_a_other_language->title    = 'title fr';
$new_node_in_a_other_language->language = 'fr';
// define source tnid   
$new_node_in_a_other_language->tnid = $source_nid;
node_save($new_node_in_a_other_language);
0

here is a code that will work a bit better in terms of cloning such features like panelizer and node configurations.

$node_id = 42611;
$source_node = node_load($node_id );
$es_node_404 = clone $source_node;
$source_node->tnid = $node_id ;
node_save($source_node);

// Making cloned node unique.
unset($es_node_404->nid);
unset($es_node_404->vid);

node_object_prepare($es_node_404);
$es_node_404->title = '[ES] ' . $source_node->title;
$es_node_404->language = 'es';
$es_node_404->status = 1;
// Define source tnid.
$es_node_404->tnid = $node_id;
node_save($es_node_404);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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