系统进行多语言支持转换

 
 
makeLanguageConfig.php
<?
/*
*@desc:生成语言包配置
*/
$sourceFile = $argv[1];
$configFile = $argv[2];
if(empty($sourceFile) || empty($configFile)){
        help();
}
 
//清除原内容
clearConfig($configFile);
makeConfig($sourceFile,$configFile);
 
function makeConfig($sourceFile,$configFile){
        $e = file_get_contents($sourceFile);
        $lines = explode("\n",$e);
        foreach($lines as $v){
                $res = explode("\t",$v);
                $arr[$res[0]]['other'] = $res[2]; //去重复
        }
        writeConfig("<?",$configFile);
        writeConfig('global $LANGUAGE_CONFIG;',$configFile);
        foreach($arr as $key => $v){
                writeConfig('$LANGUAGE_CONFIG'."['{$key}'] = '{$v[other]}';",$configFile);
        }
}
function writeConfig($value,$configFile){
        file_put_contents($configFile,$value."\n",FILE_APPEND);
}
 
 
function clearConfig($configFile){
        file_put_contents("language.inc.php","");
}
 
 
function help(){
        echo "
        =========================================================
        生成语言包配置请按以下格式运行
        php  makeLanguageConfig.php 语言包文件 语言包配置
        =========================================================
        ";
        die();
 
 
getChineseWord.php
<?php
/**
*@desc:获取文件夹下所有的中文字符
*/
 
$dir = $argv [1];
 
if (empty ( $dir )) {
        help ();
}
 
/**
* 遍历所有目录
*/
$it = new RecursiveDirectoryIterator ( $dir );
foreach ( new RecursiveIteratorIterator ( $it ) as $file ) {
        if (strpos ( $file, "svn" )) {
                continue;
        }
        getChineseWord ( $file );
}
 
/**
* 获取文件中的中文字符
*
 * @param unknown_type $file           
*/
function getChineseWord($file) {
        $x = file_get_contents ( $file );
        if (preg_match_all ( "/([\x{4e00}-\x{9fa5}\x{fe30}-\x{ffa0}]*)/u", $x, $match )) {
                foreach ( $match [0] as $k => $v ) {
                        if (! empty ( $v )) {
                                $wordList [$v] = $v; // 去重
                        }
                }
                if(!empty($wordList)){
                foreach($wordList as $v){
                        echo $v."\n";
                }
                }
        }
}
function help() {
        echo " 
        ==============================================
        请按以下参数运行
        php getChineseWord.php 文件夹 > 生成的文件名
        ==============================================
";
        die ();
}
 
 
<?php
/**
*@desc: 替换多语言标签
*/
 
$dir = $argv [1];
 
if (empty ( $dir )) {
        help ();
}
 
/**
* 遍历所有目录
*/
$it = new RecursiveDirectoryIterator ( $dir );
foreach ( new RecursiveIteratorIterator ( $it ) as $file ) {
        //  $link = $file->getPath(); 
        //  $name = $file->getFileName(); 
        //  $fileName = $link."/".$name;
        if (strpos ( $file, "svn" )) {
                continue;
        }
        translater ( $file );
}
 
/**
* 获取文件中的中文字符
*
 * @param unknown_type $file           
*/
function translater($file) {
        $arr = explode(".",$file);
        $fileType = end($arr);
        doReplace($file,$fileType);
}
function doReplace($file,$type){
        require "./language.inc.php";
        global $LANGUAGE_CONFIG;
        $newFileCnt = "";
        $cfile = file($file);
        foreach($cfile as &$line){
                if (preg_match_all ( "/([\x{4e00}-\x{9fa5}\x{fe30}-\x{ffa0}]*)/u", $line, $match )) {
                        foreach ( $match[0] as $k => $v ) {
                                if (! empty ( $v )) {
                                        $oldValue[] = $v;
                                        if($type == "php" ){
                                                $newString = $LANGUAGE_CONFIG[$v];
                                        }else if($type == "tpl" || $type=="html"){
                                                $newString = "<{lang l='{$v}'}>";
                                        }
                                        $newValue[] = $newString;
                                }
                        }
                        if(is_array($newValue) && !empty($newValue)){
                                $string = @str_replace($oldValue,$newValue,$line);
                                $newFileCnt .= $string;
                        }else{
                                $newFileCnt .= $line;
                        }
                }
        }
//      echo $newFileCnt;
        file_put_contents($file,$newFileCnt);
}
 
function help() {
        echo " 
                ==============================================
                请按以下参数运行
                php translater.php 文件夹 > 生成的文件名
                ==============================================
                ";
        die ();
}
 




原文地址:https://www.cnblogs.com/firmy/p/2816983.html