<?php

function webapp_translator($string){

    if(!isset($_SESSION))
      //@session_start();
 
 $_SESSION['USER_LANGUAGE'] = "EN";

    $hash = sha1($string);
    $language = "XX";

    if(isset($_SESSION['USER_LANGUAGE'])){

      $language = $_SESSION['USER_LANGUAGE'];

    }

    if(isset($_SESSION['language'])) {
       if($_SESSION['language']=='english'){
        $language="EN";
      }else if($_SESSION['language']=='italian'){
        $language="IT";
      }else if($_SESSION['language']=='french'){
        $language="FRA";
      }
    }
   
    $path = __DIR__ . "/languagepacks/webapp_language_" . $language . ".wap";
  
    if(!file_exists($path)) 
    //echo "File not found " . $path;
      return $string; // ritorno italiano.

  
    $xmlStr = file_get_contents($path);
    $xmlObj = simplexml_load_string($xmlStr, 'SimpleXMLElement');
    $locxml = objectsIntoArray($xmlObj);

    if(isset($locxml['hashes']["h_".$hash]))
      if(is_array($locxml['hashes']["h_".$hash]))
        return $locxml['hashes']["h_".$hash][0];
      else
        return $locxml['hashes']["h_".$hash];


    return $string;


  }


  function objectsIntoArray($arrObjData, $arrSkipIndices = array())
  {
      $arrData = array();

      // if input is object, convert into array
      if (is_object($arrObjData)) {
          $arrObjData = get_object_vars($arrObjData);
      }

      if (is_array($arrObjData)) {

          foreach ($arrObjData as $index => $value) {
              //$index=remove_dots($index);

              if (is_object($value) || is_array($value)) {
                  $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
              }

              if (in_array($index, $arrSkipIndices)) {
                  continue;
              }
              $arrData[$index] = $value;
          }
      }
      return $arrData;
  }


?>
