Translator

<?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

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <span> <a>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.
My name is Kevin. I am a software engineer and bike geek living in Salem, Mass. I use Vim even when I don't need to. When I'm not on the computer, I'm usually hanging with my wife, Melissa and baby Benjamin or working on my pet project BIKENE.WS.