1

I have a custom module which is used to insert the data related employees but i need to translate that data into hindi language as well but i didn't know how can i do that as other content type automatically have translate tab where data can be inserted in different different languages. can anyone help me how can translate my custom module data. Here is my custom module file:

<?php
function form_example_permission() {
  return array(
    'submit form_example' => array(
      'title' => t('Submit form_example'),
      'description' => t('Submit the form_example form'),
    ),
    'access form_example submissions' => array(
      'title' => t('Access form_example submissions'),
      'description' => t('Access the form_example submissions'),
    ),
     'access form_example search' => array(
      'title' => t('Access form_example search'),
      'description' => t('Access the form_example search'),
    ),
  );
}
function form_example_menu() {
  $items = array();

 $items['employees-list'] = array(
    'title' => t('Employees List'),
    'page callback' => 'products_list',
    'access callback' => TRUE,
  );
 $items['employees-list/edit/%'] = array(
    'title' => t('Edit Product'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('products_edit_confirm', 2),
      'access callback' => TRUE,
    'type' => MENU_NORMAL_ITEM,
    );
  $items['form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
    'title' => 'Employess Entry Form', //page title
    'description' => "A form to submit the employee's data.",
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array('submit form_example'),
    'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed.  for a form, use drupal_get_form
    'page arguments' => array('form_example_form'),

      );

 $items['form-submissions'] = array(
    'title' => t('Employees List'),
    'page callback' => 'form_example_submissions',
    'access callback' => TRUE,
    'access arguments' => array('access form_example submissions'),
    'type' => MENU_CALLBACK,
  );

  $items['form-list'] = array(
    'title' => t('Employees List'),
    'page callback' => 'form_example_list',
    'access callback' => TRUE,
    'access arguments' => array('access form_example search'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function form_example_form($form, &$form_state) {

$form['empcode'] = array(
  '#weight' => '0',
  '#required' => '1',
  '#type' => 'textfield',
  '#title' => t('Employee Code'),
);
$form['empname'] = array(
  '#weight' => '1',
  '#required' => '1',
  '#type' => 'textfield',
  '#title' => t('Employee Name'),
);
$form['empemail'] = array(
  '#weight' => '2',
  '#required' => '0',
  '#type' => 'textfield',
  '#title' => t('Employee Email'),
);
$form['empdesignation'] = array(
  '#weight' => '3',
  '#required' => '1',
  '#type' => 'textfield',
  '#title' => t('Employee Designation'),
);
$form['mobile'] = array(
  '#weight' => '4',
  '#required' => '0',
  '#type' => 'textfield',
  '#title' => t('Mobile No.'),
);
$form['ipphone'] = array(
  '#weight' => '5',
  '#required' => '0',
  '#type' => 'textfield',
  '#title' => t('IP Phone'),
);
$form['qualifications'] = array(
  '#weight' => '6',
  '#required' => '0',
  '#type' => 'textfield',
  '#title' => t('Qualifications'),
);
$form['profile'] = array(
  '#weight' => '7',
  '#required' => '0',
  '#type' => 'textarea',
  '#title' => t('Profile'),
);
$form['file'] = array(
  '#weight' => '8',
  '#type' => 'file',
  '#size' => '30',
  '#title' => t('Employee Photo'),
);
$form['captcha'] = array(
  '#weight' => '9',
  '#type' => 'captcha',
  '#captcha_type' => 'captcha/Math',
);
 $form['submit'] = array(
     '#weight' => '10',
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

return $form;

}


function form_example_form_validate($form, &$form_state) {
  /*if (!($form_state['values']['price'] > 0)){
    form_set_error('price', t('Price must be a positive number.'));
  }*/
 // print('<pre>'.print_r($form_state['values'],1).'</pre>');
  //die();
 $file = file_save_upload('file', array(
    'file_validate_is_image' => array(),
    'file_validate_extensions' => array('png gif jpg jpeg'),
  ));
  if ($file) {
    if ($file = file_move($file, 'public://')) {
      $form_state['values']['file'] = $file;
    }
    else {
      form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
    }
  }
  else {
    form_set_error('file', t('No file was uploaded.'));
  }
}

function form_example_form_submit($form, &$form_state) {
   $file=$form_state['values']['file'];
  unset($form_state['values']['file']);
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);
 // drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $file->filename)));


 $fe_id = db_insert('form_example')
        ->fields(array(

          'empcode' => $form_state['values']['empcode'],
          'empname' => $form_state['values']['empname'],
          'empemail' => $form_state['values']['empemail'],
          'empdesignation' => $form_state['values']['empdesignation'],
          'empphoto' => $file->filename,
          'mobile'  => $form_state['values']['mobile'],
          'ipphone' => $form_state['values']['ipphone'],
          'qualifications' => $form_state['values']['qualifications'],
          'profile' => $form_state['values']['profile'],
        )) 
        ->execute();
        drupal_set_message(t('Successfully added'));

}
function form_example_theme($existing, $type, $theme, $path) {
  return array(
    'form_example_function' => array(
      'variables' => array('rows' => NULL),
    ),
    'form_example_template' => array(
      'variables' => array('rows' => NULL),
      'template' => 'form_example_template',
    ),


  );
}

/* view the form submissions*/
function form_example_submissions()
{
  $results =  db_query("Select * from {form_example}");
   $rows = array();
  foreach($results as $result)
  {
    $rows[] = array(
        $result->fe_id,
        $result->empcode,
        $result->empname,
        $result->empphoto,
        $result->empemail,
        $result->empdesignation,
        $result->mobile,
        $result->ipphone,
        $result->qualifications,
        $result->profile,
      );

  }
  return theme('form_example_function',array('rows' => $rows));
}
/*function theme_form_example_function($variables)
{
  print_r($variables);
//$output ="<h2>".print_r$variables."</pre></h2>";
/*  foreach ($variables as $result)
  {

    $output = "<h2><pre>".print_r($result)."</pre></h2>";
  }
return $output;
 $output = '';

    foreach($variables['rows'] as $row) {
        foreach($row as $values) {

          print_r($values);
         $output .= "Empcode: " . $values . "</br> ";
         // $output .= "EmpName: " . $values[1] . " </br>";
        //$output .= "Emphot: " . $values[2] . "\n";
        }
    }
    return $output;

}*/
function theme_form_example_function($variables)
{
    $output = '';
   $search_form =drupal_get_form('form_example_search_form');
    $output = '<div class="block" >'.drupal_render($search_form).'</div>';
    $output .= '<div style="float:left;margin-top:27px;" ><a href="http://localhost/nic/form-list" class="btn">ALL</a></div>';
        $output .= '<div style="clear:both;margin-top:20px;" >&nbsp;</div>';


//   print_r($variables);
    foreach($variables['items'] as $row) {
      //  echo $path;
      // $output .= "Empcode: " . (isset($row[1]) ? $row[1] : "--") . " ";
      //  $output .= "EmpName: " . (isset($row[2]) ? $row[2] : "--") . " ";
      //  $output .= "Emphot: " . (isset($row[3]) ? $row[3] : "--") . "<br>";   

$img_url = 'public://'.(isset($row[3]) ? $row[3] : "--");  // the orig image uri
$style = 'small';  // or any other custom image style you've created via /admin/config/media/image-styles



     $output .= '<div class="views-row"><div class="do_featured">
  <div style="position: relative;" class="do_image"> 
            <div class="field field-name-field-thumb-image field-type-image field-label-hidden"><div class="field-items"><div class="field-item "><img  src="'.image_style_url($style, $img_url).'" ></div></div></div></div>
  <div class="do_title">
    <span>    <div class="field"><div class="field-items"><b>'.(isset($row[2]) ? $row[2] : "--") .'</b></div></div>
 <div class="empfield"><div class="empfield-items">Emp Code: &nbsp;</div><div class="empfield-items">'.(isset($row[1]) ? $row[1] : "--") .'</div></div>';
 if(isset($row[6]))
 {

 $output .= '<div class="empfield"><div class="empfield-items">Mobile: &nbsp;</div><div class="empfield-items">'.(isset($row[6]) ? $row[6] : "--") .'</div></div> ';
}
if(isset($row[7]))
{
  $output .=' <div class="empfield"><div class="empfield-items">IP: &nbsp;</div><div class="empfield-items">'.(isset($row[7]) ? $row[7] : "--") .'</div></div> ';
}
if(isset($row[8]))
{
  $output .='<div class="empfield"><div class="empfield-items">Qualifications: &nbsp;</div><div class="empfield-items">'.(isset($row[8]) ? $row[8] : "--") .'</div></div> ';
}
if(isset($row[4]))
{

  $output .='<div class="field"><div class="field-items">'.(isset($row[4]) ? $row[4] : "--") .'</div></div>';
  }
  if(isset($row[5]))
  {

 $output .='<div class="field"><div class="field-items">'.(isset($row[5]) ? $row[5] : "--") .'</div></div>';
}
 if(isset($row[9]))
  {
 $output .='<div class="field"><div class="field-items">'.(isset($row[9]) ? $row[9] : "--") .'</div></div>';
}
      $output .='</span> 
   </div></div></div>';



    }

    return $output;
}
function products_list() {
  $header = array('EmpName','EmpCode','Actions');
  $results = db_query("SELECT fe_id,empname,empcode FROM {form_example}");
  $rows = array();
  foreach($results as $key) {
    $empid = $key->fe_id;
    $name = $key->empname;
    $empcode = $key->empcode;
    $rows[] = array($name,$empcode,"<a href='employees-list/edit/{$key->fe_id}'>" . t('Edit') . "</a>");
  }
  return theme('table', array('header' => $header, 'rows' => $rows));
}

/*function form_example_list(){
    $search_form =drupal_get_form('form_example_search_form');
    $content = '<div class="block" >'.drupal_render($search_form).'</div>';
    $content .= '<div style="clear:both;padding:30px;" ></div>';

    $query = db_select('form_example', 'pd');
    if(isset($_GET['empname']) && $_GET['empname'] != ''){
        $query->condition('pd.empname', '%' . db_like($_GET['empname']). '%','LIKE');
    }
    if(!empty($_GET['empcode'])){
        $query->condition('pd.empcode', '%' . db_like($_GET['empcode']). '%','LIKE');
    }
    if(isset($_GET['empdesignation']) && $_GET['empdesignation'] != ''){
        $query->condition('pd.empdesignation', '%' . db_like($_GET['empdesignation']). '%','LIKE');
    }
    $query->fields('pd');
  // $query->extend('PagerDefault')->limit(1);
   //$query->limit(10);
    //$table_sort = $query->extend('TableSort')->orderByHeader($header);
    //$pager = $table_sort->extend('PagerDefault')->limit(10);

    $results = $query->execute();
   $rows = array();
  foreach($results as $result)
  {
    $rows[] = array(
        $result->fe_id,
        $result->empcode,
        $result->empname,
        $result->empphoto,
        $result->empemail,
         $result->empdesignation,
      );

  }
//$limit =1;
  //pager_default_initialize(count($results), $limit, $element = 0);
//$pager = theme('pager',array("quantity"=>5));
//return theme("form_example_function",array("rows"=>$rows ,"pager"=>$pager));


 return theme('form_example_function',array('rows' => $rows));
 //return $content;
}*/

function form_example_list() {
  $query = db_select('form_example', 'n')->extend('PagerDefault');
    if(isset($_GET['empname']) && $_GET['empname'] != ''){
        $query->condition('n.empname', '%' . db_like($_GET['empname']). '%','LIKE');
    }
    if(!empty($_GET['empcode'])){
        $query->condition('n.empcode', '%' . db_like($_GET['empcode']). '%','LIKE');
    }
    if(isset($_GET['empdesignation']) && $_GET['empdesignation'] != ''){
        $query->condition('n.empdesignation', '%' . db_like($_GET['empdesignation']). '%','LIKE');
    }
  $query->fields('n', array('fe_id', 'empcode','empname','empphoto','empemail','empdesignation','mobile','ipphone','qualifications','profile'))
      ->limit(12)
      ->groupBy('n.fe_id');
  $results = $query->execute();
     $rows = array();
  foreach($results as $result)
  {
    $rows[] = array(
        $result->fe_id,
        $result->empcode,
        $result->empname,
        $result->empphoto,
        $result->empemail,
         $result->empdesignation,
         $result->mobile,
         $result->ipphone,
         $result->qualifications,
         $result->profile,
      );

  }
  $pager = theme('pager', array("quantity" => 5));
  $output = theme('form_example_function', array('items' => $rows));
  $output = $output . $pager;
  return $output;
}
function form_example_search_form($form, &$form_state){

    $form['#method'] = 'get';
    $form['#token'] = FALSE;
    $form['#form_build_id'] = FALSE;
    $form['#form_id'] = FALSE;
    $form['empname'] = array(
        '#type' => 'textfield',
        '#title' => t('Employee Name'),
        '#attributes' => array( 'maxlength' => '128', 'size'=> '30'),
        '#prefix' => '<div class="form-inline views-exposed-form"> <div class="views-exposed-widget">',
        '#suffix' => '</div>',
        '#default_value' => isset($_GET['empname'])?$_GET['empname']:'',
    );
    $form['empcode'] = array(
        '#type' => 'textfield',
        '#title' => t('Employee Code'),
        '#attributes' => array( 'maxlength' => '128', 'size'=> '30'),
        '#prefix' => '<div class="form-inline views-exposed-form"> <div class="views-exposed-widget">',
        '#suffix' => '</div>',
        '#default_value' => isset($_GET['empcode'])?$_GET['empcode']:'',
    );
    $form['empdesignation'] = array(
        '#type' => 'textfield',
        '#title' => t('Designation'),
        '#attributes' => array( 'maxlength' => '128', 'size'=> '30'),
        '#prefix' => '<div class="form-inline views-exposed-form"> <div class="views-exposed-widget">',
        '#suffix' => '</div>',
        '#default_value' => isset($_GET['empdesignation'])?$_GET['empdesignation']:'',
    );

    //views-exposed-widget

    $form['submit'] = array(
        '#type' => 'submit',
        '#attributes' => array( 'name' => ''),
        '#prefix' => '<div class="views-exposed-widget">',
        '#suffix' => '</div></div>',
        '#value' => 'Apply',
     );  

    return $form;
}

/*************for edit ****************/
function products_edit_confirm($form ,&$form_state, $product_code){
//drupal_set_message($product_code);
$fe_id = '';$name='';$empcode='';

$results = db_query("SELECT fe_id,empname,empcode,empdesignation,empphoto,mobile,ipphone,qualifications,profile FROM {form_example} WHERE fe_id='{$product_code}'");
  foreach($results as $key) {
    $fe_id = $key->fe_id;
    $name = $key->empname;
    $empcode = $key->empcode;
    $empdesignation = $key->empdesignation;
    $empphoto = $key->empphoto;
     $mobile = $key->mobile;
     $ipphone = $key->ipphone;
     $qualifications = $key->qualifications;
     $profile = $key->profile;
  }
$form = array();
$form['fe_id'] = array(
        '#type' => 'hidden',
        '#title' => t('Employee Id'),


        '#value' => $fe_id,);

  $form['empname'] = array(
        '#type' => 'textfield',
        '#title' => t('Employee Name'),


        '#default_value' => $name,);

    $form['empcode'] = array(
        '#type' => 'textfield',
        '#title' => t('Employee Code'),


        '#default_value' => $empcode,);

  $form['empdesignation'] = array(
        '#type' => 'textfield',
        '#title' => t('Designation'),


        '#default_value' => $empdesignation,);
$form['mobile'] = array(
  '#required' => '0',
  '#type' => 'textfield',
  '#title' => t('Mobile No.'),
   '#default_value' => $mobile,);

$form['ipphone'] = array(
  '#required' => '0',
  '#type' => 'textfield',
  '#title' => t('IP Phone'),
 '#default_value' => $ipphone,);


$form['qualifications'] = array(
   '#required' => '0',
  '#type' => 'textfield',
  '#title' => t('Qualifications'),
   '#default_value' => $qualifications,);

$form['profile'] = array(
  '#required' => '0',
  '#type' => 'textarea',
  '#title' => t('Profile'),
   '#default_value' => $profile,);


  $form['file'] = array(
  '#type' => 'file',
  '#size' => '30',
  '#title' => t('Employee Photo'),
  '#default_value' => $empphoto,);

  return confirm_form($form,t(''),
      isset($_GET['destination']) ? $_GET['destination'] : "employees-list",
      t(''),t('Edit'),t('Cancel'));
}


function products_edit_confirm_submit($form, &$form_state)
{
$form_values = $form_state['values'];
if ($form_state['values']['confirm']) {

    $fe_id = $form_state['values']['fe_id'];
    $name = $form_state['values']['empname'];
    $empcode= $form_state['values']['empcode'];
    $empdesignation = $form_state['values']['empdesignation'];
    $mobile = $form_state['values']['mobile'];
    $ipphone = $form_state['values']['ipphone'];
    $qualifications = $form_state['values']['qualifications'];
    $profile = $form_state['values']['profile'];

    $file = file_save_upload('file', array(
    'file_validate_is_image' => array(),
    'file_validate_extensions' => array('png gif jpg jpeg'),
  ));
  if ($file) {
    if ($file = file_move($file, 'public://')) {
      $form_state['values']['file'] = $file;
    }
    else {
      form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
    }
  }
  else {
    form_set_error('file', t('No file was uploaded.'));
  }
      $file=$form_state['values']['file'];    
      unset($form_state['values']['file']);
      $file->status = FILE_STATUS_PERMANENT;
      file_save($file);
      $filename = $file->filename;
      $pu = db_query("UPDATE  {form_example} SET empphoto='{$filename}' WHERE  fe_id='{$fe_id}'");



 $rs = db_query("UPDATE  {form_example} SET empname= '$name', empcode = '$empcode',empdesignation='$empdesignation',mobile='$mobile',ipphone='$ipphone',qualifications='$qualifications',profile='$profile' WHERE  fe_id='{$fe_id}'");
   drupal_set_message(t('Employee data has been updated successfully.'));
   }
 drupal_goto("employees-list");
}
?>
0

You should output all your module data using function t(). And use module Localization Client for adding translations into the base.

| improve this answer | |
  • i am confused how can i use function t ? – Sumit Aggarwal Jan 16 '17 at 5:41
  • For example, you need to output "Hello world" in Hindi language somewhere in your module. You write t('Hello world'). And afterwards with module Localization Client you translate this phrase into Hindi. – Yakimkin Roman Jan 16 '17 at 5:52
  • i have installed the localization client module and add the t function under output nothing could happend – Sumit Aggarwal Jan 16 '17 at 6:56
  • No, you don't use t() for anything that is not a literal string. You cannot expect t() to translate everything stored in a database table. – kiamlaluno Apr 28 '19 at 18:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.