<?php   	require_once('_approot.php');	 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="css/admin.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="css/style2.css" />

<?php 	require_once('_approot.php');	

	$transtype=$_GET['ls'];

 	$iftpl=$_GET['iftpl'];
	$currpartid=$_GET['currpartid'];
	if($currpartid!="") $session['currpartid']=$currpartid;

	if($iftpl=="")$iftpl=$_POST['iftpl'];
	$ls_msg="";
	$wrtpl=false;
	
	if($_POST['transtype']!="")$transtype=$_POST['transtype'];
	//echo $transtype;
	$transa=explode(",",$transFlags);
	if($_POST['submit']=="确定"){ //聚合字符串
		$wrtpl=true;
		$endt=$_POST["endt"];	 // 翻译表数据 表名，字段名，id
		foreach($transa as $lang){
			$allen="all".$lang;
			//echo $allen;
			$$allen="";
			$$allenen="";
			$en=$lang;
				for($i=1;$i<=10000;$i++){ //同一种语言的的翻译串拼合起来
					$enn=$en.$i; 
					if($_POST[$enn]!=""){
						
						$$allen.=$_POST[$enn];	
						//echo $_POST[$enn];
						$$allenen.=$_POST["en".$i];	
						
						
					}
					else
					{break;}
					
				}
				// echo $$allen;
				if($transtype=="constant")
				{	
					$filename="../ls_admin/inc/constant.php"; //  英文的constant
					$handle = fopen($filename,"r");
					//echo $filename;
					$partstr =  fread($handle,filesize($filename));  
					//echo $partstr."--";
					fclose ($handle);

				}

				//$$allen=str_replace("[{[[","[{[",$$allen);


 
			//	preg_match_all($p, $$allen, $arr);
			//	preg_match_all($p2, $$allen, $arr2);
			    
				$$allenen=str_replace("';","",$$allenen); 
				$temarr = explode("999\r\n", $$allenen); // 翻译原文
				$temarr2 =explode("999\r\n", $$allen);  // 翻译结果
				$temarr3 =explode("999\r\n",$endt);  // 翻译表数据 表名，字段名，id
				
				$i=0;
				for($j=0; $j<count($temarr2);$j++){
					
					$decodeenstr=trim(str_replace("\r\n","",$temarr[$j]));  // 翻译原文
					$decodeenstr=trim(str_replace("...","",$decodeenstr)); 
					$decodeenstr=trim(str_replace("999","",$decodeenstr)); 

					$objtpl=trim(str_replace("\r\n","",$temarr2[$j])); // 翻译结果
					$objtpl=trim(str_replace("...","",$objtpl)); 
					$objtpl=trim(str_replace("999","",$objtpl)); 
					
					$headlangdt=trim(str_replace("\r\n","",$temarr3[$j]));  // "999\r\n". $tablename."+".$fieldname."+".$pro_value['ID']."\r\n";
					
					if($transtype=="constant" || $transtype=="tplfield" )	addtransfast($decodeenstr,$lang,$objtpl,"tpl");	 //最下面确定按钮 模板 或者 constant增加快表 
					if(!$iftpl  ) //最下面确定按钮
					{
						if($transtype=="tabletext"){
							//echo $headlangdt."===============<br/>".$decodeenstr."<br/>";
							$entbpos=explode("+",$headlangdt );//   $tablename."+".$fieldname."+".$pro_value['ID'] 
							$transstr=$objtpl;  // 通过 global 变量传递 savetb
							if($headlangdt=="") { echo "<br>headlangdt:为空，transstr：<br>".$transstr."<br>-------<br>" ;continue;}
							if(trim($entbpos[2])=="") { echo "<br>ID:为空，表： ".trim($entbpos[0])."，字段： ".trim($entbpos[1])."<br>-------<br>" ;continue;}
							//echo "$headlangdt===$transstr<br> <br>";
							$ls_msg2.=savetb(trim($entbpos[0]),trim($entbpos[1]),trim($entbpos[2]),$lang);// 写入表 $tablename,$fieldname,$idid,$lang(语言)
						}
						if($transtype=="constant"){
							$entbpos= $decodeenstr  ;//  
							$transstr=$objtpl;
							if($transstr=="") continue;
							$scr="<translate>".$entbpos."</translate>";
							$obj= $transstr ;
							//$obj=str_replace("] "," ",$obj); // 多出来] 
							//echo  "----------------".$scr."----------------".$obj."<br/>";
							$partstr=str_replace($scr,$obj,$partstr) ;  // 写入数组
						}
					}
					$i++;					
				}
				// echo $partstr;
				if($transtype=="constant"&&$$allen!="")
				{	
					$path="../".$lang."/ls_admin/inc/"; 
					$filename2=$path."constant.php"; 
					if(!file_exists($filename2)) { 
						echo $filename2."不存在，请先生成多语种结构";
					} 
				 
					$handle = fopen($filename2,"w");
					 
					$encoding = mb_detect_encoding($partstr, array('GB2312','GBK','UTF-16','UCS-2','UTF-8','BIG5','ASCII'));
					if (mb_detect_encoding($partstr)!='UTF-8'){
                    $partstr = iconv($encoding, 'UTF-8', $partstr);}
					if(fwrite($handle,$partstr)) $ls_msg.="写入 $lang constant 成功。<br/>"; else $ls_msg.="写入 $lang constant 失败。";
					fclose ($handle);

				}

			}
		if($transtype=="tplfield"  ) echo "模板翻译内容已加入快表";
		// 设置完成标志
		if($session['currpartid']!=""){
			$mplstrpart="update ls_transslow set queryTotal=1 where ID=".$session['currpartid']." ";
			$update_query=$opendb->query($mplstrpart);
		}
	
	}
	echo $ls_msg;
 
// 以下为第一排按钮 模板、表数据、变量翻译 按下操作
//echo $transtype."==";
//$i=0;
//$alltrans="";
if($transtype=="constant"){
	foreach($lang_en as   $key => $value){
		$strlang=$value;
		$p = "%<translate>(.*?)</translate>%si";    //正则表达式
		$totalflag=preg_match_all($p, $strlang, $arr);
		$clearlang=$arr[1];
		//var_dump($clearlang);
		//$headlang="999\r\n".base64_encode($clearlang[0])."\r\n";
		if(!(strpos($clearlang[0],"999\r\n"))) "<br/>999\r\n 已存在<br/>";
		if(!(strpos($clearlang[0],"999"))) "<br/>999 已存在<br/>";
		
		$alltrans.="999\r\n".$clearlang[0]."\r\n";
		$i++;	
	}
		//$alltrans.="999";
		//echo $alltrans.strlen($alltrans);
		$val=$alltrans;

}
 

if($transtype=="tplfield"){
	 
	$mantplstr="";
	$transa=explode(",",$transFlags);
	foreach($transa as $lang){
		transtpl(trim($lang),$wrtpl); //翻译templatel1模板
		transtpl(trim($lang),$wrtpl,"../Templates"); //翻译template模板
	}
	$val=$mantplstr;
	  echo  $mantplstr."--";
}
if($transtype=="tabletext"){
	 
	if($currpartid!=""){
		
		$mplstrpart="select * from ls_transslow where ID= $currpartid ";
		$mspquery=$opendb->query($mplstrpart);
		$partrows=mysqli_fetch_assoc($mspquery);

		$mantplstr=$partrows['fr'];
		$headlang=$partrows['enText'];
		$session['currpartid']=$partrows['ID'];
		//echo $mantplstr."---".$headlang;
		//$val=$mantplstr;
		$val=urldecode($mantplstr);
	
		$endt= $headlang;

		
	}else{
		$mantplstr="";
		$headlang="";
		transdt("fr"); //翻译表数据 subpart 5370
		//$val=$mantplstr;
		$val=urldecode($mantplstr);
	
		$endt= $headlang;
	}
	//echo  $mantplstr."==";
}
?>
</head>
<body><br/>
	
<table width="522" border="0" align="center">
  <tr>
    <td width="178" align="center"><span  ><a href="?ls=constant"  >
      <button type="button" value="变量翻译"/> 
      变量翻译 </a></span></td>
    <td width="178" align="center"><span  ><a href="?ls=tplfield"  >
                    <button type="button" value="模板翻译"/>                    
模板翻译 </a></span></td>
    <td width="152" align="center"><span  ><a href="?ls=tabletext"  >
      <button type="button" value="数据表翻译"/> 
      表数据翻译 </a></span></td>
  </tr>
  <!--<tr>
    <td align="center"><span  ><a href="checktrans.php?ls=constant"  >
      <button type="button" value="变量验证"/> 
      变量验证 </a></span></td>
    <td align="center"> <span  ><a href="checktrans.php?ls=tplfield"  >
                    <button type="button" value="模板验证"/>                    
模板验证 </a></span></td>
    <td align="center"><span  ><a href="checktrans.php?ls=tabletext"  >
      <button type="button" value="数据表验证"/> 
      表数据验证 </a></span></td>
  </tr>-->
</table>
<?php $mplstrpart="select * from ls_transslow where cate='transdtpart' ";
$mspquery=$opendb->query($mplstrpart);
$numpart=mysqli_num_rows($mspquery);
if($numpart!=0){
?>
    <table width="245" border="0" align="center" cellpadding="0" cellspacing="4">
  <tr>
    <td width="191">数据表分<?=$numpart?>段：</td>
<?php $k=1;
while($partrows=mysqli_fetch_assoc($mspquery)){
?>

    <td width="42"><a href="?ls=tabletext&currpartid=<?=$partrows['ID']?>">P<?=$k?>.</a> </td>

<?php 
$k++;
}
?>
  </tr>
</table>
<?php }?>
<br/>
 
<h3 align="center"><strong><font color="#FF3300"><?=($transtype=="tabletext"?"数据表":($transtype=="tplfield"?"模板":"变量"))?></font>翻译：点击每个语言翻译链接，打开谷歌翻译，点击右下角复制按钮，复制翻译结果</strong>，在下面对应编号的语言框ctrl-v</h3>
<form name="myform" enctype="multipart/form-data" method="post" action=" "  >
<table width="99%" align="center"><tr><td>

 <input type="hidden" name="endt" id="hiddenField3" value="<?=$endt?>" />
<?php $transSplitLen=4980;
$i=1;	
  	

foreach(subpart($val, $transSplitLen, "utf-8","999") as $out_val){
	//echo $val; exit;
	$enstr=urlencode($out_val);
	?>
	<strong>==============  <span class="cu"><font color="#FF3300"><strong>英文 en<?=$i?></strong></font></span> : "<?=mb_substr(strip_tags($out_val),-80)?>"</strong> 
	 <input type="hidden" name="iftpl" id="hiddenField" value="<?=$iftpl?>" />
	 <input type="hidden" name="transtype" id="hiddenField2" value="<?=$transtype?>" />
	 <textarea name="en<?=$i?>" cols="228" rows="6" id="en<?=$i?>" ><?php echo  str_replace("�","",$out_val) ?> </textarea><br/><br/>
   
	<?php 	foreach($transa as $lang){
		$enstr=str_replace("&nbsp;","& ",$enstr);
		$enstr=str_replace("&amp;","&",$enstr);
		$enstr=str_replace("nbsp;"," ",$enstr);

		?>
		<a href="https://translate.google.cn/?sl=en&tl=<?=$lang?>&text=<?php echo $enstr;?>#en/<?=$lang?>/<?php echo $enstr;?>';" target="_blank" > 
		<?=$lang?>
		<strong><font face="Arial Black, Gadget, sans-serif"><?=$i?></font>	</strong>:</a>  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  <a href="http://fanyi.youdao.com/?keyfrom=dict2.index" target="_blank">有道 </a><br/>
		<textarea name="<?=$lang?><?=$i?>" cols="228" rows="6" id="<?=$lang?><?=$i?>" ><?= $_POST[$lang.$i] ?></textarea><br/><br/>
		
		<?php 	} // � 去掉特殊符号
 
	$i++;
}


?></td>
 </tr>
    
	<tr>
    <td align="center">
<button name="submit" type="submit" value="确定" onClick=" "/> 确定</button>
<button type="reset" name="button" id="button" value="重置" />重置</button>
			       

</td>
	</tr>
</table></form> 

<?php //$tablename 要写入的表的名字
//$fieldname 要写入的字段的名字
//$idid 要写入的行的ID的名字
//$lang 要写入的语言

function	savetb($tablename,$fieldname,$idid,$lang)  // 写入表
{
	global $transstr; //翻译结果字符串，比较长
	include('inc/conn_var.php');
	if($lang==""||$lang=="en") {echo '\$lang 为空或者 是 en '; return;}
	$dbnamenoen=$dbname."_".$lang;
	$connnoen=@mysqli_connect($host,$username,$password) or die ("connect DBS error， please check".$dbnamenoen);
	mysqli_select_db($connnoen,$dbnamenoen)or die("select db error".$dbnamenoen);
	mysqli_set_charset($connnoen,"utf8");	
	
	require_once('inc/conn.php');
	require_once('inc/ls_syscode.php');
	$myck=new cktb();
	$transstr=str_replace("¿" ,"",$transstr); 
	$transstr=str_replace("&amp; nbsp;" ,"",$transstr); 
	$transstr=str_replace("& nbsp;" ,"",$transstr); 
	$transstr=str_replace(" / " ,"/",$transstr); 
	$transstr=str_replace("/ " ,"/",$transstr); 
	$transstr=str_replace(" /" ,"/",$transstr); 
	$transstr=str_replace("，" ,",",$transstr); 
	$transstr=str_replace("'" ,"''",$transstr); //转换单引号 
	 
	$sqlstr="update `".$dbnamenoen."`.`".$tablename."` set `".$fieldname."` = '". $transstr."' where ID=".$idid." " ;
	//$sqlstr="update ".$tablename." set ".$fieldname." = '".htmlspecialchars($transstr, ENT_QUOTES)."' where ID=".$idid." " ;
    //echo $lang.": ".$sqlstr."<br/>";
	//echo "===>".$dbnamenoen.": update ".$dbnamenoen.".".$tablename." set ".$fieldname." = '$transstr' where ID=".$idid."<br/>" ;
	if($transstr!=""){
		$query=@mysqli_query($connnoen,$sqlstr) or die ("<br/><br/> DB:[$dbnamenoen] in savetb Error: <br/>   ".mysqli_error()."   <br/>-------->".$sqlstr." <---------<br/> ");
		if($myck->issetfield($tablename,"transflag"))$myck->setvalueidfield($tablename,"transflag",$idid,1); //有 transflag 改成1
		else 
		{
			$myck->checkfield($tablename,"transflag","ALTER TABLE  `".$dbname."`.`".$tablename."` ADD  `transflag` INT( 1 ) NOT NULL DEFAULT  '0' COMMENT  '是否翻译' ");
			$myck->setvalueidfield($tablename,"transflag",$idid,1); //没有添加 transflag ，然后改成1
		}
		return $lang.": ".$tablename." ID=".$idid."  ".$fieldname." ok<br/>";
	}
	else
	{
		return $lang.": ".$tablename." ID=".$idid."  ".$fieldname." is empty<br/>";
	}
	mysqli_close($connnoen);

}

	
?>