I had to import four different languages nodes into my Drupal 7 installation programmatically. It’s easy when you know where to begin with.
First, you have to enable different languages you will use into your Drupal administration. For example, I used
- EN: English
- NL: Dutch
- DE: German
- FR: French
Then, you need to define the default languages you will use for the first node created. You have to set it from the administration page too. We chose English language.
You have to know each translated nodes will be linked to others by referencing the parent and default language, here: English.
Let’s create your nodes :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
$node = new stdClass(); $node->type = 'my_custom_type'; // will add all default fields not mentioned node_object_prepare($node); // if you want it linked to a custom user $node->uid = $account->uid; // we chose English language by default $node->language = 'en'; // fill all your custom fields // this will save your node into your Drupal database and will assign a nid (node Id) to the $node variable node_save($node); // Set its default language parent, $node->nid will be equal to $node->tnid $node->tnid = $tnid = $node->nid; node_save($node); // The node is updated because the nid already exists in db // Now create translated nodes that will reference $tnid as parent // NL Translation $node = new stdClass(); $node->type = 'my_custom_type'; node_object_prepare($node); $node->uid = $account->uid; $node->language = 'nl'; // fill all your custom fields $node->tnid = $tnid; node_save($node); // DE Translation $node = new stdClass(); $node->type = 'my_custom_type'; node_object_prepare($node); $node->uid = $account->uid; $node->language = 'de'; // fill all your custom fields $node->tnid = $tnid; node_save($node); // FR Translation $node = new stdClass(); $node->type = 'my_custom_type'; node_object_prepare($node); $node->uid = $account->uid; $node->language = 'fr'; // fill all your custom fields $node->tnid = $tnid; node_save($node); |
You have successfully created four nodes linked together by the English node.
Now if you want to get all translated nodes from Drupal, you can use translation_node_get_translations function that takes a parent translated nid in parameter and send back translated nodes array:
1 2 3 4 5 6 7 |
$translatedNodesArray = translation_node_get_translations($tnid); //For example, check if french translation exists $nodeFR = isset($translatedNodesArray ['fr']) ? node_load($translatedNodesArray ['fr']->nid) : null; |
For Drupal localization projects I recommend https://poeditor.com as a very useful crowd translation tool tool. It has a plugin that connects the platform to your Drupal website, making import and export easier.
Thanks! Nice article and good solution.
I got what I needed.
Not easy to find tutorial on multilingual Drupal.
This great article
Great article. Import translated nodes is a big problem in drupal 6. You`ve saved me a lot of work.
Thanks for sharing!