Yii语言包词条提取
据说是哥半年前写的一个用户管理程序,而现在老大要求程序能切换语言;将原来的词条用英文添进去之后,发现写语言包的时候一个一个提取比较麻烦,所以就写了个程序提取。(目前还只能提取Yii::t里面只有两个参数的情况)
<?php
$fileinfo = new FilesystemIterator('group'); // 目录
$words = array();
while($fileinfo->valid()) {
if($fileinfo->getExtension() === 'php') {
$filename = $fileinfo->getFilename();
$fileobject = $fileinfo->openFile();
while($fileobject->valid()) {
if(preg_match_all('/Yii::t\(([\'"])([^\'"]*?)\\1\s*,\s*([\'"])(.*?)\\3\)/', $fileobject->current(), $matches)) {
foreach($matches[4] as $match) {
$words[$match] = isset($words[$match]) ? $words[$match] . '; ' . $filename . ' # ' . $fileobject->key() : '// ' . $filename . ' # ' . $fileobject->key();
}
}
$fileobject->next();
}
}
$fileinfo->next();
}
function save_to_file($content = '') {
$fileobject = new SplFileObject('user.trans.php', 'w');
$fileobject->fwrite($content);
}
ob_start('save_to_file');
echo '<?php
/**
* @author XiaoZi<245565986@qq.com>
*/
return array(
';
foreach($words as $word => $comment) {
echo "\t" . $comment . PHP_EOL;
echo "\t" . '\'' . $word . '\' => \'\',' . PHP_EOL;
}
echo ');
';
ob_end_clean();