I need to export the content from all the nodes on our site to be translated offsite by our i18n team. I'm having problems importing new translations into the system with node_save()

To add a new translation for a node it seems I need to create a new node with the tnid set to the original node. This all kinda works and a new node row appears in the node DB table, but some things are a bit wonky. Even though the translated content shows on the site correctly (yay!) when I click to edit the German version on the "Translate" tab, it takes me to the homepage, not the edit screen. I'm sure I'm probably doing something silly here, but are there any examples out there that I can follow?

Here's a sample of what I'm doing for node 34.

$newNode = /* here's the var_dump of the node object */
  object(stdClass)#187 (11) {
    ["title"]=>
    string(16) "Latest News (DE)"
    ["body"]=>
    array(1) {
      ["de"]=>
      array(1) {
        [0]=>
        array(3) {
          ["value"]=>
          string(14) "DE NEWS IS FUN"
          ["summary"]=>
          string(0) ""
          ["format"]=>
          string(9) "full_html"
        }
      }
    }
    ["is_new"]=>
    bool(true)
    ["language"]=>
    string(2) "de"
    ["uid"]=>
    string(1) "1"
    ["tnid"]=>
    string(2) "34"
    ["type"]=>
    string(4) "page"
    ["status"]=>
    string(1) "0"
    ["created"]=>
    string(10) "1303927114"
    ["changed"]=>
    string(10) "1304978761"
    ["translation_source"]=> { /* original node object */ }
  }

node_save($newNode);

Comments

jgillick’s picture

More code, for context on how I'm creating newNode:


$tnid = 34;
$lang = "de";

$srcNode = node_load($tnid);
$i18n = getTranslationFor($tnid, $lang); /* translated content */

$newNode = (object) array( "title" => $i18n['title'],
  	                    "body" => array(
  	                      "$lang" => array(
  	                        array(
  	                          "value" => $i18n['body'],
  	                          "summary" => $i18n['summary'],
  	                          "format" => $srcNode->body["en"][0]["format"]
  	                        )
  	                      )
  	                    ));

$newNode->language            = $lang;
$newNode->uid                 = $srcNode->uid;
$newNode->tnid                = $srcNode->nid;
$newNode->type                = $srcNode->type;
$newNode->status              = $srcNode->status;
$newNode->created             = $srcNode->created;
$newNode->changed             = $srcNode->changed;
$newNode->translation_source  = $srcNode;

node_save($newNode);
cristian100’s picture

Excuse me, I'm having the exact same situation, need to import a collection of nodes, I wonder, did you figure out how to accomplish this?