<style>
*{font:12px verdana; }
</style>
<pre>
*{font:12px verdana; }
</style>
<pre>
下载地址:从汉语言文件制作多国语言包.
<?php
require_once("echo.php");
include_once("listfile.php");
/**
* 从汉语言文件制作多国语言包.
*/
class lang_make extends listfile
{
/**
* extname ,存放文本文件扩展名,供检查使用。
*/
var $extname = " php xml html htm txt ini sql acp stx ctl";
/**
* lang 存放语言包的内容 define(xx);.
*/
var $lang = "";
/**
* langr 存放对应替换的内容;.
*/
var $langr = "";
var $phpopen = false; //在php内
var $instring = false; //在双引号字串内
var $inSingleString = false; //在单引号字串内
var $instr = false; //在字串内
var $markline = false; //在行注释内
var $markb = false; //在块注释内
var $inhtml = false; //在html内
var $markhtml = false; //在html的注释内
var $csshtml = false; //在html的css内
/**
* 检查扩展名是否为文本文件
*/
function checkFileExt($file)
{
$dotpos = strrpos($file,".");
$ext = substr($file,$dotpos+1);
return strpos($this->extname, $ext);
} // end func
/**
* 生成汉语言的的数组,连在一起的汉字作为本数组的一元素
*/
function langmake($file)
{
//echo $file;
if (!$this->checkFileExt($file)) {
//echo "not txt file";
return;
}
//生成语言包前缀
$im = $_POST['pre'];
$path_parts = pathinfo( $file );
$f = $path_parts['basename'];
$ex = $path_parts['extension'];
$f = substr($f,0,strrpos($f,"."));
$uim = strtoupper( $im );
$basename =strtoupper($f);
$basename = str_replace(".","_",$basename);
$head = "_".$uim."_".$basename."_";
//echo $head;
//前缀生成
$temp = file_get_contents($file);
$ary =array( "gb2312","utf-8");
$encode = mb_detect_encoding($temp, $ary);
//echo "$encode = $file \n";
$encode = strtolower($encode);
if ($encode<>"utf-8") {
$temp = iconv($encode,"utf-8",$temp);
}
//备份
$fp = fopen($file.".bak.php.bak","wb");
fwrite($fp,$temp);
//备份结束。
$tmp = split("\n",$temp); //行数组
$langchar = array(); //存放语言数组
$c = -1; //$c,初始化存放语言数组的下标
for ( $z=0,$zn=count($tmp) ; $z<$zn; $z++ ) { //遍历每一行
$j = 0;
$tmpvalue = $tmp[$z];
for($i=0,$n=mb_strlen($tmpvalue,"utf-8");$i<$n;$i++){ //处理这一行,遍历每个字符
$char = mb_substr($tmpvalue,$i,1,"utf-8");
$code = $this->uniord($char);
if ($char == "?") { //如果遇到?判断是否为php语句块开始
if ($this->phpopen === false) {
$next3char = mb_substr($tmpvalue,$i+1,3,"utf-8");
if ($next3char == "php") {
$this->phpopen = true;//echo "[phpopen\n";
$this->inhtml = false ;
$i=$i+4;
}
}
elseif ($this->phpopen === true) {
$nextchar = mb_substr($tmpvalue,$i+1,1,"utf-8");
if ($nextchar == ">") {
$this->phpopen = false; //echo "phpclose]\n";
$this->inhtml = true ;
}
}
}
//第二步
if ( $this->phpopen === false ){
$this->inhtml = true ;
}
else {
//过滤注释语句
$markbclosepos = mb_strpos( $tmpvalue,"*/",$i,"utf-8" );
if ( $this->markb == true ) {
//echo "[markblock * $line ]$tmpvalue \n";
if ( $markbclosepos === false ) {
break;
}
else {
$this->markb = false;
//echored("mark closed]\n");
}
}
//首先判断是否在字符串内。区分单引号和双引号
if ( $this->inSingleString == true ) { //如果在单引号内
if ( $char == "'" ) {
$oldchar = mb_substr($tmpvalue,$i-1,1,"utf-8");
if( $oldchar != "\\" ){
//echo "单引号结束] [$z] \n ";
$this->inSingleString = false; $this->instr = false;
}
} //char is '
}
else {
if ( $char == "'" ) {
if( !$this->instring ){
//echo "[单引号开始 [$z] [$tmpvalue] \n ";
$this->inSingleString = true;
$this->instr = true;
}
} //char is '
else {
if ( $char == '"' && $this->instring == true ) {
//echo mb_substr($tmpvalue,$i-1,10,"utf-8");echo "\n";
$oldchar = mb_substr($tmpvalue,$i-1,1,"utf-8");
if ( $oldchar != '\\' ) {
//echo "$oldchar:双引号结束]\n";
$this->instring = false; $this->instr = false;
}
}
else if ($char == '"'){
//echo "[双引号开始 :$markbclosepos \n";
$this->instring = true; $this->instr = true;
}
else {
//echo "$char ";
}
}// char not '
}//如果不在单引号内
//判断是否在单引号双引号内结束,$this->instring表示双引号内,$this->inSingleString表示在单引号内。
//开始判断是否为注释语句。
$doublechar = mb_substr($tmpvalue,$i,2,"utf-8");
if (!$this->instr) { //如果不在字串内
//echo "$char 不在字符串内 \n";
if ( $doublechar == "//" ) {
//echored("行注释");
break; //下一行
}
elseif( $doublechar == "/*" ) {
$this->markb = true;
//echored( "[markblock 开始于位置 $i \n" );
$markbclosepos = mb_strpos( $tmpvalue,"*/",$i,"utf-8" );
if ( $markbclosepos === false ) {
//echo "行内找不到关闭";
break; //如果找不到关闭标记,换行
}
else {
//echo "markblock行内关闭]";
$this->markb = false;
$i = $markbclosepos+2;
}
}
}//如果不在字串内end
else {
//echo "$char ";
}
}//在 php代码内 过滤注释语句 结束。
if ($code>255) {
//echo "$code $c\n";
$c++;
$gb = true;
$langchar[$c] = "";
$langfile[$c] = "$file";
}
else {
$gb = false;
}
while ( $code>255 ) {
$langchar[$c] .= $char;
$i++;
$char = mb_substr($tmpvalue,$i,1,"utf-8");
$code = $this->uniord($char);
$j++;
if ( $j>200 ) { //不接受大于200个字的汉字串
$j=0; echo "200";
break;
}
}
if ($gb==true) { //如果发现了汉字
//检查是否重复
$dup = false;
$search = $langchar[$c];
$defineSn = $c;
for($li=0;$li<$c;$li++) {
if ($langchar[$li] == $langchar[$c]) {
//echo "重复$li\n";
$defineSn =$li;
$c--;
$dup = true;
break;
}
}
//echo "$langchar[$defineSn] \n";
$definekey = $head.$defineSn."_";
if (!$dup) { //如果不重复
$this->lang .= "define('".$definekey."','$langchar[$c]');\n";
$this->langr .= "$definekey=$definekey=$langchar[$c] \n";
}
$start = mb_strpos($tmpvalue , $search , 0 , "utf-8");
$cuthead = mb_substr( $tmpvalue , 0 , $start , "utf-8" );
$cutend = mb_substr( $tmpvalue, $start+mb_strlen($search,"utf-8") , $n , "utf-8" );
if ( $this->inhtml == true ) {
$mid = "<?php echo(".$definekey.");?>";
}
else {
$mid = "\".".$definekey.".\"";
}
$i = mb_strlen( $cuthead.$mid , "utf-8" )-1; //-1是因为已到循环上面要+1,而while里已经加过了
$tmpvalue = $cuthead.$mid.$cutend; //替换汉字为定义的变量,这是替换过的行
$n = mb_strlen( $tmpvalue , "utf-8" ); //重新计算行的字符数。
$tmp[$z] = $tmpvalue;
}//如果发现了汉字结束
if ($i>7*1024) {
break;
}
}//forend each character
}//foreach line end
//给langchar按照字串长短排序
if (count($langchar)>0) {
$this->lang ="/* $f */ \n".$this->lang ;
$lines = implode("\r\n",$tmp);
$fp = fopen($file,"wb");
fwrite($fp,$lines);
$this->lang .="/* / $f */ \n\n\n";
$this->langr .= "\n\n\n";
//print_R( $langchar );
}//如果汉字不为空。
} // end func langmake
/**
* 将字符串写入成语言包文件
*/
function make($langfile)
{
$this->lang = "<?php\n".$this->lang;
$this->lang .="?>\n";
$fp = fopen( $langfile , "wb");
fwrite($fp,$this->lang);
} // end func
/**
* 将字符串写入成语言包文件
*/
function maker($langrfile)
{
$fp = fopen( $langrfile , "wb");
fwrite($fp,$this->langr);
} // end func
/**
* unicode ord ,FROM manual,从手册中拿的检查unicode编码的函数,用来替代ord
*/
function uniord($u) {
$k = $u;
$k1 = ord(substr($k, 0, 1));
$k2 = ord(substr($k, 1, 1));
//$k3 = ord(substr($k, 2, 1));echo "k1 = $k1 k2=$k2 k3=$k3 <br/>";
return $k2 * 256 + $k1;
}//end func uniord
} // end class
/* example */
if (count($_POST)>0) {
$fname = $_POST['userfile'];
//echo $fname;
$dir = dirname($fname);
$langdir = dirname($dir);
$langdir = $langdir."\\lang";
if (!is_dir($langdir)) {
listfile::mkdirp($langdir); //如果语言包目录不存在,则新建。
}
$langbasename = basename($dir);
$langfile = $langdir."\\".$langbasename."_lang_ch.php";
$langrfile = $langdir."\\".$langbasename."_lang_ch_replace.php";
echo "$langfile";
if (is_dir($dir) && $_POST['changetype']=="dir") {
$list = new lang_make;
$list->callFunc = "\$this->langmake(\$filePath);";
$list->listDirTree($dir);
$list->make($langfile);
$list->maker($langrfile);
}//if dir
elseif(is_file($fname) && $_POST['changetype'] == "file") {
$list = new lang_make;
$list->langmake($fname);
$list->make($langfile);
$list->maker($langrfile);
}
}
?>
<form action="?" method="post">
前缀: <select name="pre">
<option value="module">module</option>
<option value="component">component</option>
</select>
转换为目录:<INPUT TYPE="radio" NAME="changetype" value="dir">
转换为文件:<INPUT TYPE="radio" NAME="changetype" value="file" checked>
<input type="file" name="userfile" />
<INPUT TYPE="submit" value="检查">
</FORM>
https://files.cnblogs.com/gudai/lang_make7.zip
<?php
require_once("echo.php");
include_once("listfile.php");
/**
* 从汉语言文件制作多国语言包.
*/
class lang_make extends listfile
{
/**
* extname ,存放文本文件扩展名,供检查使用。
*/
var $extname = " php xml html htm txt ini sql acp stx ctl";
/**
* lang 存放语言包的内容 define(xx);.
*/
var $lang = "";
/**
* langr 存放对应替换的内容;.
*/
var $langr = "";
var $phpopen = false; //在php内
var $instring = false; //在双引号字串内
var $inSingleString = false; //在单引号字串内
var $instr = false; //在字串内
var $markline = false; //在行注释内
var $markb = false; //在块注释内
var $inhtml = false; //在html内
var $markhtml = false; //在html的注释内
var $csshtml = false; //在html的css内
/**
* 检查扩展名是否为文本文件
*/
function checkFileExt($file)
{
$dotpos = strrpos($file,".");
$ext = substr($file,$dotpos+1);
return strpos($this->extname, $ext);
} // end func
/**
* 生成汉语言的的数组,连在一起的汉字作为本数组的一元素
*/
function langmake($file)
{
//echo $file;
if (!$this->checkFileExt($file)) {
//echo "not txt file";
return;
}
//生成语言包前缀
$im = $_POST['pre'];
$path_parts = pathinfo( $file );
$f = $path_parts['basename'];
$ex = $path_parts['extension'];
$f = substr($f,0,strrpos($f,"."));
$uim = strtoupper( $im );
$basename =strtoupper($f);
$basename = str_replace(".","_",$basename);
$head = "_".$uim."_".$basename."_";
//echo $head;
//前缀生成
$temp = file_get_contents($file);
$ary =array( "gb2312","utf-8");
$encode = mb_detect_encoding($temp, $ary);
//echo "$encode = $file \n";
$encode = strtolower($encode);
if ($encode<>"utf-8") {
$temp = iconv($encode,"utf-8",$temp);
}
//备份
$fp = fopen($file.".bak.php.bak","wb");
fwrite($fp,$temp);
//备份结束。
$tmp = split("\n",$temp); //行数组
$langchar = array(); //存放语言数组
$c = -1; //$c,初始化存放语言数组的下标
for ( $z=0,$zn=count($tmp) ; $z<$zn; $z++ ) { //遍历每一行
$j = 0;
$tmpvalue = $tmp[$z];
for($i=0,$n=mb_strlen($tmpvalue,"utf-8");$i<$n;$i++){ //处理这一行,遍历每个字符
$char = mb_substr($tmpvalue,$i,1,"utf-8");
$code = $this->uniord($char);
if ($char == "?") { //如果遇到?判断是否为php语句块开始
if ($this->phpopen === false) {
$next3char = mb_substr($tmpvalue,$i+1,3,"utf-8");
if ($next3char == "php") {
$this->phpopen = true;//echo "[phpopen\n";
$this->inhtml = false ;
$i=$i+4;
}
}
elseif ($this->phpopen === true) {
$nextchar = mb_substr($tmpvalue,$i+1,1,"utf-8");
if ($nextchar == ">") {
$this->phpopen = false; //echo "phpclose]\n";
$this->inhtml = true ;
}
}
}
//第二步
if ( $this->phpopen === false ){
$this->inhtml = true ;
}
else {
//过滤注释语句
$markbclosepos = mb_strpos( $tmpvalue,"*/",$i,"utf-8" );
if ( $this->markb == true ) {
//echo "[markblock * $line ]$tmpvalue \n";
if ( $markbclosepos === false ) {
break;
}
else {
$this->markb = false;
//echored("mark closed]\n");
}
}
//首先判断是否在字符串内。区分单引号和双引号
if ( $this->inSingleString == true ) { //如果在单引号内
if ( $char == "'" ) {
$oldchar = mb_substr($tmpvalue,$i-1,1,"utf-8");
if( $oldchar != "\\" ){
//echo "单引号结束] [$z] \n ";
$this->inSingleString = false; $this->instr = false;
}
} //char is '
}
else {
if ( $char == "'" ) {
if( !$this->instring ){
//echo "[单引号开始 [$z] [$tmpvalue] \n ";
$this->inSingleString = true;
$this->instr = true;
}
} //char is '
else {
if ( $char == '"' && $this->instring == true ) {
//echo mb_substr($tmpvalue,$i-1,10,"utf-8");echo "\n";
$oldchar = mb_substr($tmpvalue,$i-1,1,"utf-8");
if ( $oldchar != '\\' ) {
//echo "$oldchar:双引号结束]\n";
$this->instring = false; $this->instr = false;
}
}
else if ($char == '"'){
//echo "[双引号开始 :$markbclosepos \n";
$this->instring = true; $this->instr = true;
}
else {
//echo "$char ";
}
}// char not '
}//如果不在单引号内
//判断是否在单引号双引号内结束,$this->instring表示双引号内,$this->inSingleString表示在单引号内。
//开始判断是否为注释语句。
$doublechar = mb_substr($tmpvalue,$i,2,"utf-8");
if (!$this->instr) { //如果不在字串内
//echo "$char 不在字符串内 \n";
if ( $doublechar == "//" ) {
//echored("行注释");
break; //下一行
}
elseif( $doublechar == "/*" ) {
$this->markb = true;
//echored( "[markblock 开始于位置 $i \n" );
$markbclosepos = mb_strpos( $tmpvalue,"*/",$i,"utf-8" );
if ( $markbclosepos === false ) {
//echo "行内找不到关闭";
break; //如果找不到关闭标记,换行
}
else {
//echo "markblock行内关闭]";
$this->markb = false;
$i = $markbclosepos+2;
}
}
}//如果不在字串内end
else {
//echo "$char ";
}
}//在 php代码内 过滤注释语句 结束。
if ($code>255) {
//echo "$code $c\n";
$c++;
$gb = true;
$langchar[$c] = "";
$langfile[$c] = "$file";
}
else {
$gb = false;
}
while ( $code>255 ) {
$langchar[$c] .= $char;
$i++;
$char = mb_substr($tmpvalue,$i,1,"utf-8");
$code = $this->uniord($char);
$j++;
if ( $j>200 ) { //不接受大于200个字的汉字串
$j=0; echo "200";
break;
}
}
if ($gb==true) { //如果发现了汉字
//检查是否重复
$dup = false;
$search = $langchar[$c];
$defineSn = $c;
for($li=0;$li<$c;$li++) {
if ($langchar[$li] == $langchar[$c]) {
//echo "重复$li\n";
$defineSn =$li;
$c--;
$dup = true;
break;
}
}
//echo "$langchar[$defineSn] \n";
$definekey = $head.$defineSn."_";
if (!$dup) { //如果不重复
$this->lang .= "define('".$definekey."','$langchar[$c]');\n";
$this->langr .= "$definekey=$definekey=$langchar[$c] \n";
}
$start = mb_strpos($tmpvalue , $search , 0 , "utf-8");
$cuthead = mb_substr( $tmpvalue , 0 , $start , "utf-8" );
$cutend = mb_substr( $tmpvalue, $start+mb_strlen($search,"utf-8") , $n , "utf-8" );
if ( $this->inhtml == true ) {
$mid = "<?php echo(".$definekey.");?>";
}
else {
$mid = "\".".$definekey.".\"";
}
$i = mb_strlen( $cuthead.$mid , "utf-8" )-1; //-1是因为已到循环上面要+1,而while里已经加过了
$tmpvalue = $cuthead.$mid.$cutend; //替换汉字为定义的变量,这是替换过的行
$n = mb_strlen( $tmpvalue , "utf-8" ); //重新计算行的字符数。
$tmp[$z] = $tmpvalue;
}//如果发现了汉字结束
if ($i>7*1024) {
break;
}
}//forend each character
}//foreach line end
//给langchar按照字串长短排序
if (count($langchar)>0) {
$this->lang ="/* $f */ \n".$this->lang ;
$lines = implode("\r\n",$tmp);
$fp = fopen($file,"wb");
fwrite($fp,$lines);
$this->lang .="/* / $f */ \n\n\n";
$this->langr .= "\n\n\n";
//print_R( $langchar );
}//如果汉字不为空。
} // end func langmake
/**
* 将字符串写入成语言包文件
*/
function make($langfile)
{
$this->lang = "<?php\n".$this->lang;
$this->lang .="?>\n";
$fp = fopen( $langfile , "wb");
fwrite($fp,$this->lang);
} // end func
/**
* 将字符串写入成语言包文件
*/
function maker($langrfile)
{
$fp = fopen( $langrfile , "wb");
fwrite($fp,$this->langr);
} // end func
/**
* unicode ord ,FROM manual,从手册中拿的检查unicode编码的函数,用来替代ord
*/
function uniord($u) {
$k = $u;
$k1 = ord(substr($k, 0, 1));
$k2 = ord(substr($k, 1, 1));
//$k3 = ord(substr($k, 2, 1));echo "k1 = $k1 k2=$k2 k3=$k3 <br/>";
return $k2 * 256 + $k1;
}//end func uniord
} // end class
/* example */
if (count($_POST)>0) {
$fname = $_POST['userfile'];
//echo $fname;
$dir = dirname($fname);
$langdir = dirname($dir);
$langdir = $langdir."\\lang";
if (!is_dir($langdir)) {
listfile::mkdirp($langdir); //如果语言包目录不存在,则新建。
}
$langbasename = basename($dir);
$langfile = $langdir."\\".$langbasename."_lang_ch.php";
$langrfile = $langdir."\\".$langbasename."_lang_ch_replace.php";
echo "$langfile";
if (is_dir($dir) && $_POST['changetype']=="dir") {
$list = new lang_make;
$list->callFunc = "\$this->langmake(\$filePath);";
$list->listDirTree($dir);
$list->make($langfile);
$list->maker($langrfile);
}//if dir
elseif(is_file($fname) && $_POST['changetype'] == "file") {
$list = new lang_make;
$list->langmake($fname);
$list->make($langfile);
$list->maker($langrfile);
}
}
?>
<form action="?" method="post">
前缀: <select name="pre">
<option value="module">module</option>
<option value="component">component</option>
</select>
转换为目录:<INPUT TYPE="radio" NAME="changetype" value="dir">
转换为文件:<INPUT TYPE="radio" NAME="changetype" value="file" checked>
<input type="file" name="userfile" />
<INPUT TYPE="submit" value="检查">
</FORM>
下载地址:从汉语言文件制作多国语言包.
https://files.cnblogs.com/gudai/lang_make7.zip