Programmatically Create translated Nodes

Events happening in the community are now at Drupal community events on www.drupal.org.
mariano86's picture

Hello:

I have i18n module installed in Drupal 6. Using drupal_execute I have a created a lot of nodes with the info in a database.

I have also the translated data into the database. Therefore I like to create the "translation" of these nodes using the same system.

Here is the code:

<?php
  $sql
= "SELECT DISTINCT city, td.tid province, n.nid orig
          FROM programs p
          INNER JOIN node n ON n.title = p.city AND n.type = 'city'
          INNER JOIN term_data td ON td.name = p.province AND td.vid = 1"
;
 
 
$res  = db_query($sql);
 
  while (
$ob = db_fetch_object($res)) {
   
$node = array();
   
$node['type'] = 'city';
   
   
$form_state = array();
   
$form_state['values']['title'] = $ob->city;   
   
$form_state['values']['taxonomy'] = array(1 => array($ob->province));
   
   
// For i18n
   
$form_state['values']['language'] = 'en';
   
$form_state['values']['translation_source'] = node_load($ob->orig);
   
   
$form_state['values']['op'] = t('Save');
   
$form_state['values']['name'] = 'admin';
   
   
drupal_execute('city_node_form', $form_state, (object)$node);
  }
?>

But this code doesn't create a translation of the node. Instead of that its create a new node with english language by default.
Any ideas?

Thanks
Mariano

Comments

A translation is a new node

frank ralf's picture

Hi Mariano,

That seems to be the correct behavior.

A source node and translations of the source node have separate node numbers and, in fact, exist as completely separate nodes in the database. They are related to each other by the tnid column of the node table, which has as its value the node ID of the source node.

from "Pro Drupal Development" (http://www.drupalbook.com), p. 437

hth
Frank

PS:
Some more related information:

Support for displaying translation sets
http://drupal.org/node/307032

Enable fields to be stored by translation set (tnid)
http://drupal.org/node/340355

Internationalization

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: