<?php
/**
* Implements hook_nodeapi().
*/
function translator_nodeapi(&$node, $op, $a3, $a4) {
// Translate bindings
if ($node->type == 'template' && $op == 'load') {
translate_bindings($node);
}
// Translate CCK fields
if ($op == 'load') {
translate_content($node);
}
}
/**
* Helper function to determine language setting
*/
function translator_language_fr() {
$fr = $_COOKIE['locale'] == 'fr_FR' ? true : false;
return $fr;
}
/**
* Helper function to translate content
*/
function translate_content(&$node) {
// Check if they are rendering in French
$fr = translator_language_fr();
foreach ($node as $key => $value) {
if (preg_match('/^field_en_(.*)$/', $key, $matches)) {
if ($fr && $node->{'field_fr_' . $matches[1]}[0]['value] != '') {
$node->v[$matches[1]] = $node->{'field_fr_' . $matches[1]}[0]['value];
}
else {
$node->v[$matches[1]] = $value[0]['value'];
}
}
}
}
/**
* Helper function to translate bindings
*/
function translate_bindings(&$node) {
// If bindings are provided, make them available
// to the node object.
if (isset($node->field_bindings_txt[0]['value'])) {
$bindings = tablefield_rationalize_table(unserialize($node->field_bindings_txt[0]['value']));
// Check if they are rendering in French
$fr = translator_language_fr();
// Create default binding values in English
// $binding[0] - Key
// $binding[1] - English value
// $binding[2] - French value
foreach ($bindings as $binding) {
// Override with french if specified
if ($fr && $binding[2] != '') {
$node->v[$binding[0]] = $binding[2];
}
else {
$node->v[$binding[0]] = $binding[1];
}
}
}
}
?>
Post new comment