<?php
// smallscale_translate_tutorial.php ... Google Translate ... not the whole website ... RJM Programming tutorial
//mb_internal_encoding("UTF-8");
$ls = '<select style="margin-top:8px;padding-bottom:4px;background-color:white;color:blue;" id="language" title="Translations courtesy of Google Translate at http://translate.google.com/" onchange="ChangeLcodeNow();"><option value="">Language</option><option value="af">Afrikaans</option><option value="sq">Albanian</option><option value="ar">Arabic</option><option value="hy">Armenian</option><option value="az">Azerbaijani</option><option value="eu">Basque</option><option value="be">Belarusian</option><option value="bg">Bulgarian</option><option value="ca">Catalan</option><option value="zh-CN">Chinese (Simplified)</option><option value="zh-TW">Chinese (Traditional)</option><option value="hr">Croatian</option><option value="cs">Czech</option><option value="da">Danish</option><option value="nl">Dutch</option><option value="">English</option><option value="et">Estonian</option><option value="tl">Filipino</option><option value="fi">Finnish</option><option value="fr">French</option><option value="gl">Galician</option><option value="ka">Georgian</option><option value="de">German</option><option value="el">Greek</option><option value="ht">Haitian Creole</option><option value="iw">Hebrew</option><option value="hi">Hindi</option><option value="hu">Hungarian</option><option value="is">Icelandic</option><option value="id">Indonesian</option><option value="ga">Irish</option><option value="it">Italian</option><option value="ja">Japanese</option><option value="ko">Korean</option><option value="la">Latin</option><option value="lv">Latvian</option><option value="lt">Lithuanian</option><option value="mk">Macedonian</option><option value="ms">Malay</option><option value="mt">Maltese</option><option value="no">Norwegian</option><option value="fa">Persian</option><option value="pl">Polish</option><option value="pt">Portuguese</option><option value="ro">Romanian</option><option value="ru">Russian</option><option value="sr">Serbian</option><option value="sk">Slovak</option><option value="sl">Slovenian</option><option value="es">Spanish</option><option value="sw">Swahili</option><option value="sv">Swedish</option><option value="th">Thai</option><option value="tr">Turkish</option><option value="uk">Ukrainian</option><option value="ur">Urdu</option><option value="vi">Vietnamese</option><option value="cy">Welsh</option><option value="yi">Yiddish</option></select>';
$larray = array("Translations courtesy of Google Translate at","Language","Afrikaans","Albanian","Arabic","Armenian","Azerbaijani","Basque","elarusian","Bulgarian","Catalan","Chinese (Simplified)","Chinese (Traditional)","Croatian","Czech","Danish","Dutch","English","Estonian","Filipino","Finnish","French","Galician","Georgian","German","Greek","Haitian Creole","Hebrew","Hindi","Hungarian","Icelandic","Indonesian","Irish","Italian","Japanese","Korean","Latin","Latvian","Lithuanian","Macedonian","Malay","Maltese","Norwegian","Persian","Polish","Portuguese","Romanian","Russian","Serbian","Slovak","Slovenian","Spanish","Swahili","Swedish","Thai","Turkish","Ukrainian","Urdu","Vietnamese","Welsh","Yiddish");
$lis = "";
$hl = "en";
$p = "";
$array = array("My hovercraft is full of eels.",
"One language is never enough.",
"Stop the world, I want to get off!",
"This gentleman will pay for everything.",
"This lady will pay for everything.",
"Say cheese!",
"Pleased to meet you.",
"What's your name?",
"Where are you from?",
"I don't understand.",
"Please say that again.",
"Please speak more slowly.",
"Please write it down.",
"Do you speak English?",
"I don't know.");

$earray = array("My hovercraft is full of eels.",
"One language is never enough.",
"Stop the world, I want to get off!",
"This gentleman will pay for everything.",
"This lady will pay for everything.",
"Say cheese!",
"Pleased to meet you.",
"What's your name?",
"Where are you from?",
"I don't understand.",
"Please say that again.",
"Please speak more slowly.",
"Please write it down.",
"Do you speak English?",
"I don't know.");

function translate($ins) {
     global $lis, $hl;
     $outs = $ins;
     if ($lis != "" && $lis != "en") {
       $huh = file_get_contents("http://www.google.com/translate_t?ie=UTF8&hl=" . $hl . "&langpair=" . $hl . "|" . $lis . "&text=" . str_replace(" ", "+", $ins));
       $outs = rettrans($huh, $ins);
     }
     return $outs;
}

function rettrans($inhuh, $defhuh) {
     global $lis;
     $xxxx = $defhuh;
     $xpos = strpos($inhuh, "onmouseout");
     if ($xpos !== false) {
      $zpos = substr($inhuh, strpos($inhuh, "onmouseout"));
      //if ($i == 0) echo " found at " . $xpos;
      $xxpos = strpos($zpos, ">");
      $zzpos = substr($zpos, strpos($zpos, ">") + 1);
      //if ($i == 0) echo " then found at " . $xxpos;
      $xxxpos = strpos($zzpos, "<");
      //if ($i == 0) echo " then again found at " . $xxxpos;
      $zzzpos = substr($zzpos, 0, strpos($zzpos, "<") - 0);
      $xxxx = $zzzpos;
     } 
        //$fl=fopen(dirname(__FILE__) . "/myselectf." . $lis, "a");
        //fwrite($fl, $xxxx . "\n");
        //fclose($fl);

     return $xxxx;    
}

// Fixes the encoding to uf8
// Thanks to ... http://php.net/manual/en/function.utf8-encode.php ... no mb_* on website ... boo hoo
function fixEncoding($in_str)
{
  //$cur_encoding = mb_detect_encoding($in_str) ;
  //if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8"))
  //  return $in_str;
  //else
    return utf8_encode($in_str);
} // fixEncoding 


echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
echo "\n" . '<html xmlns="http://www.w3.org/1999/xhtml">';
echo "\n<head>";
echo "\n" . '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
echo "\n" . '<meta name="viewport" content="width=device-width, user-scaleable=yes" />';

if (isset($_GET['lang'])) {
   $lis = $_GET['lang'];
   echo "\n<title>" . translate("The Wonders of Google Translate - Small Amounts of Translation - RJM Programming tutorial");
   for ($i=0; $i<sizeof($array); $i++) {
    if (!file_exists(dirname(__FILE__) . "/myp" . $i . "." . $lis)) {
      $p = $array[$i];
      $huh = file_get_contents("http://www.google.com/translate_t?ie=UTF8&hl=" . $hl . "&langpair=" . $hl . "|" . $lis . "&text=" . str_replace(" ", "+", $p));
      $fh = fopen(dirname(__FILE__) . "/myp" . $i . "." . $lis, "w");
      fwrite($fh, $huh);
      fclose($fh);
     } else {
      $huh = "";
      $fl=fopen(dirname(__FILE__) . "/myp" . $i . "." . $lis, "r");
      while (($buffer = fgets($fl, 500000)) !== false) {
       $huh .= $buffer;
      }
      fclose($fl);
     }
     //if ($i == 0) echo str_replace("<", " ", str_replace(">", " ", substr($huh, strpos($huh, "onmouseout"))));
     $xpos = strpos($huh, "onmouseout");
     if ($xpos !== false) {
      $zpos = substr($huh, strpos($huh, "onmouseout"));
      //if ($i == 0) echo " found at " . $xpos;
      $xxpos = strpos($zpos, ">");
      $zzpos = substr($zpos, strpos($zpos, ">") + 1);
      //if ($i == 0) echo " then found at " . $xxpos;
      $xxxpos = strpos($zzpos, "<");
      //if ($i == 0) echo " then again found at " . $xxxpos;
      $zzzpos = substr($zzpos, 0, strpos($zzpos, "<") - 0);
      $xxxx = $zzzpos;
      //if ($i == 0) echo " gives " . $xxxx;
      $array[$i] = rettrans($huh, $array[$i]); //$xxxx;
      if ($i == 0) {
       if (file_exists(dirname(__FILE__) . "/myselect." . $lis)) {
        $ls = "";
        $fl=fopen(dirname(__FILE__) . "/myselect." . $lis, "r");
        while (($buffer = fgets($fl, 500000)) !== false) {
         $ls .= $buffer;
        }
        fclose($fl);
       } else {
        $xls = $ls;
        $huh = file_get_contents("http://www.google.com/translate_t?ie=UTF8&hl=" . $hl . "&langpair=" . $hl . "|" . $lis . "&text=" . str_replace(" ", "+", $larray[0]));
        $ls = str_replace($larray[0], rettrans($huh, $larray[0]), $xls);
        $xls = $ls;
        for ($k=1; $k<sizeof($larray); $k++) {
         $huh = file_get_contents("http://www.google.com/translate_t?ie=UTF8&hl=" . $hl . "&langpair=" . $hl . "|" . $lis . "&text=" . str_replace(" ", "+", $larray[$k]));
        //$fl=fopen(dirname(__FILE__) . "/myselectf." . $lis, "a");
        //fwrite($fl, "larray[" . $k . "]=" . $larray[$k] . "\n");
        //fclose($fl);
         $ls = str_replace($larray[$k], rettrans($huh, $larray[$k]), $xls);
         $xls = $ls;
        }
        $fl=fopen(dirname(__FILE__) . "/myselect." . $lis, "w");
        fwrite($fl, $ls);
        fclose($fl);
       }
      }    
    } else if ($i == 0) {
      echo " not found ";
    }
   }
} else {
   echo "\n<title>" . "The Wonders of Google Translate - Small Amounts of Translation - RJM Programming tutorial";
}

echo "\n</title>";
echo "\n<style>";

echo "\nbody {";
echo "\n  margin: 0 20px;";
echo "\n  padding: 0;";
echo "\n  background-color: ochre;";
echo "\n  color: purple;";
echo "\n}";

echo "\n</style>";

if (isset($_GET['language'])) {
   $lis = $_GET['language'];
echo "<body><p style='color:red;width:800px;' id='ipis' title=' ... translating from English now ... please wait ... '> ... " . translate("translating from English now") . " ... " . translate("please wait") . " ...</p></body></html>";
} else {


echo "\n<script>";

echo "\nfunction doit() { ";
echo "\n var val = document.getElementById('language'); ";
echo "\n if (val.value.length != 0 && val.value.indexOf('en') == (0 - 1)) {  ";
echo "\n   window.location = './smallscale_translate_tutorial.php?lang=' + val.value; ";
echo "\n }  ";
echo "\n} ";
echo "\n ";
   
echo "\nfunction ChangeLcodeNow() { ";
echo "\n var val = document.getElementById('language'); ";
echo "\n if (val.value.length != 0 && val.value.indexOf('en') == (0 - 1)) {  ";
echo "\n   var ppis = document.getElementById('pis'); ";
echo "\n   var uis = './smallscale_translate_tutorial.php?language=' + val.value; ";
echo "\n   ppis.src = uis;"; // = \"<iframe id='ijpis' title=' ... translating now ... please wait ... ' style='width:600px;height:40px;border:0' src='\" + uis + \"'></iframe>\";"; 
echo "\n   ppis.style.display = 'block'; ";
echo "\n   var myd = document.getElementById('mydiv'); ";
echo "\n   myd.style.opacity = 0.4; ";
echo "\n   setInterval(doit, 3000); ";
echo "\n }  ";
echo "\n} ";
echo "\n</script>";
echo "\n</head>";
echo "\n<body>";

 if ($lis != "en" && $lis != "") {
   echo str_replace('value="' . $lis . '"', 'value="' . $lis . '" selected="selected"', fixEncoding($ls));
 } else {
   echo $ls;
 }

//echo "&nbsp;<p id='pis' title=' ... translating now ... please wait ... ' style='display:none;'> ... " . translate("translating now") . " ... " . translate("please wait") . " ...</p><div id='mydiv' style='opacity:1.0;'>";
echo "&nbsp;<iframe id='pis' title=' ... translating from English now ... please wait ... ' style='display:none; width:800px;height:40px;border:0'></iframe><div id='mydiv' style='opacity:1.0;border: 2px solid green;padding-left:5px;'>";
for ($j=1; $j<=sizeof($array); $j++) {
  echo "\n<p id='p" . $j . "' title=\"" . $earray[$j - 1] . "\">" . fixEncoding($array[$j - 1]) . "</p>";
}
echo "\n</div></body>";
echo "\n</html>";
}
?>
