Thursday, March 1, 2012

Storing another language word for the English word and retrieving

 Storing another language word for the English word and retrieving anywhere in our module or tpl :

This function to create the form fields for entering the english and spanish words and also for displaying it in table format.
function get_englishtospanish(){
$edit_option = arg(3);
$disalbled = '';
  if(arg(4)=='edit'){
  $eng_spa_value = db_fetch_object(db_query("SELECT * FROM {tbl_eng_span} WHERE s_id = %d ", arg(3)));
    $english = $eng_spa_value->english;
    $spanish=  $eng_spa_value->spanish;
    //$disalbled = TRUE;
  }else if(arg(4)=='delete'){
      db_query("DELETE FROM {tbl_eng_span} WHERE s_id = %d", arg(3));
      drupal_goto('admin/settings/eng2span');
  }
  $form['english'] = array(
    '#type' => 'textfield',
    '#title' =>  t('English'),
    '#description' => t('Enter the English word/sentence'),
    '#default_value' => ($english ? $english : ''),
      '#disabled' => $disalbled,
      '#required'=> TRUE,
  );

  $form['spanish'] = array(
    '#type' => 'textfield',
    '#title' =>  t('Spanish'),
    '#description' => t('Enter the Spanish word/sentence'),
    '#default_value' => ($spanish? $spanish: ''),
      '#required'=> TRUE,
  );
 
  $form['save'] = array('#type' => 'submit', '#value' => (arg(3)? 'Update':'Save'));

    $headers = array('S.No', 'English Words/Sentence', 'Spanish Words/Sentence', 'Action');
        $eng_spa_details = pager_query('SELECT * FROM {tbl_eng_span}',25);
        $options['attributes']['onclick'] = "return confirm('Are you sure you want to delete?')";
        while ($eng_spa_obj = db_fetch_object($eng_spa_details)) {
            $eng_spa_id = $eng_spa_obj->s_id;
            $incre++;
            $eng_spa_eng = $eng_spa_obj->english;
            $eng_spa_spa= $eng_spa_obj->spanish;
            $rows[] = array($incre,
                $eng_spa_eng,
                $eng_spa_brz,
                l(('Edit'), 'admin/settings/eng2spa/' . $eng_brz_id .'/edit').'/'.l(('Delete'), 'admin/settings/eng2spa/' . $eng_spa_id.'/delete',$options)
            );
        }
        $output .= theme('table', $headers, $rows);
        $output .= theme('pager');
        $form['result'] = array('#value' => $output);

  return $form;
}
//for creating tables to store these values,you need to create the table using install file,
[[[
$schema['tbl_eng_span']= array(
           'description' => t('Account Information Table'),

'fields' => array(
           's_id' => array(
           'type' => 'serial',
           'description' => t('Sid'),
           'length' => 250,
           'not null' => TRUE
),
           'english' => array(
           'type' => 'varchar',
           'description' => t('English'),
           'length' => 250,
           'not null' => TRUE
),
           'spanish' => array(
           'type' => 'varchar',
           'description' => t('Spanish'),
           'length' => 250,
           'not null' => TRUE
),         

),
     'primary key' => array('s_id')
);

]]]





//Submitting the values for storing,editing,updating and deleting.
function get_englishtospanish_submit($form, &$form_state) {
$edit_option = arg(3);
$eng_spa_obj = db_fetch_object(db_query("SELECT * FROM {tbl_eng_span}"));
  $result_array = array_slice($form_state['values'],0,2);
    if($edit_option != ''){
      $sid = array("s_id" => $edit_option);
      $result_array = array_merge($result_array, $sid);
      drupal_write_record('tbl_eng_span',$result_array,'s_id');
      drupal_set_message(t("Data's Updated"));
      drupal_goto('admin/settings/eng2spa');
   }else if(($form_state['values']['english'])== ($eng_spa_obj->english)){
          drupal_set_message("Word/Sentence <b><i>" . $eng_spa_obj->english ."</i></b> Already Exists",'error');
      }else{
         drupal_write_record('tbl_eng_span',$result_array);
         drupal_set_message(t("Data's Inserted"));
  }
}

This is the main function,here we will pass the english word,it will return our saved another language word:Ex.Spanish. We will use "http://translate.google.com/" to translate the english word to Spanish.
function spanish_t($eng){  
    $brazil_word = db_fetch_object(db_query("SELECT spanish FROM {tbl_eng_span} WHERE english = '%s'", $eng));
    $
spanish _word = $spanish _word->spanish ;
    if($
spanish _word == ''){
        $
spanish _word = $eng;
    }
    return $
spanish _word;
}


Final Look of this page,

No comments:

Post a Comment