语言包自动化处理 [一]

利用php,js等语言便捷的字符串处理能力,在整理老项目语言包时候,可以使用脚本程序读取文件每行,匹配中文,然后以自己想要的形式输出.

<?php

//中文 正则表达式匹配
$cn_pattern = "/([\x{4e00}-\x{9fa5}]+)/u";

//待检测代码后缀
$suffix = 'php';
$dir_path = '/Users/away/workspace/svn/nis/web/application/views/page';
//忽略含有关键词的文件名
$ignore_file_keywords = [];

// $suffix = 'js';
// $dir_path = '/Users/away/workspace/svn/nis/web/static';
// $ignore_file_keywords = ['jquery', 'echart', 'liquidFill', 'plugins', 'laydate'];


//获取文件列表
get_files_path($dir_path);

foreach ($file_list as $file) 
{
	search_file($file);
}


//遍历获取文件路径
function get_files_path($dir) {
    global $file_list,$suffix, $ignore_file_keywords;
    $suffix_len = strlen($suffix);

    $files = array();
    if(@$handle = opendir($dir)) 
    { 
        while(($file = readdir($handle)) !== false) 
        {
            if($file != ".." && $file != ".") { 
                $ignore = false;
                foreach ($ignore_file_keywords as $keyword) {
                    if (strstr($file, $keyword)) {
                        $ignore = true;
                        break;
                    }
                }
                if ($ignore) continue;
                if (is_dir($dir."/".$file)) { 
                    get_files_path($dir."/".$file);
                } else {
                    if(substr($file, -$suffix_len) == $suffix){
                        $file_list[] = $dir."/".$file;
                    }
                }
            }
        }
        closedir($handle);
        return;
    }
}


//搜索单个文件,查找中文
function search_file($file_path){
    global $cn_pattern;
    $file = fopen($file_path, 'r');
    if (empty($file)) return;

    $line_index = 0;
    while (!feof($file)) {
        $line_index++;
        $line = fgets($file);
        preg_match_all($cn_pattern, $line, $matches); 
        if (empty($matches[0])) continue;

        foreach ($matches[1] as $string) {
        	// if (strstr($line, "//")) continue;
        	// if (strstr($line, "<!--")) continue;

            echo "[path:line]\t",$file_path,":{$line_index}\n";
            echo "[content:]\t",trim($line), "\n";
            echo "[string:]\t",$string, "\n\n";
        }
    }

    fclose($file);
}

命令行用php执行该脚本,

php  /path/run.php

输出结果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值