2015-11-07 88 views
0

我想取我的WordPress插件,然後讓所有的gettext函數的參數PHP的正則表達式:從碼提取物起paramerts文件

die(_e('Error')) ; 
die('<b>'._e('Error').'</b>') ; 
_e('Error') 

功能爲WordPress gettext的,我用

_e__

以及如何繞過si milar功能

use_eget_emailselect__


我創建的正則表達式此功能,但它不是非常有用

$list_words = array(); 

function po_fetch_code($dir , $preg = "/_\(('|\")(.*?)('|\")\)/"){ 
    if(is_dir($dir)){ 
      $list = scandir($dir) ; 
      foreach($list as $l){ 
       if($l == '.' || $l == '..'){ 
       // Do Nothing 
       }else{ 
       po_fetch_code($dir.'/'.$l , $preg) ; 
       } 

      } 
    }else{ 
      $ext = strrchr($dir , '.') ; 
      //if($ext == '.php' || $ext == '.html' || $ext == 'html'){ 
       //$cont = file_get_contents($dir) ; 
       $list = file($dir) ; 
       global $list_words ; 
       foreach($list as $key => $l){ 
        preg_match_all($preg , $l , $matches); 
        if(!empty($matches[2])){ 
         foreach($matches[2] as $k=>$m){ 
           //$m = str_replace(array("\'",'\\' , '"' , "'",'\''), '' ,$m) ; 
           //$m = str_replace('\'s', 's' ,$m) ; 
           if(!isset($list_words[$m])){ 
            $list_words[$m]['msgid'] = array($m) ; 
            $list_words[$m]['msgstr'] = array($m) ; 
            $list_words[$m]['reference'] = array() ; 
            } 
            $list_words[$m]['reference'][] = $dir.':'.$key ; 
         } 
        } 
       } 

     // } 
    } 
} 

$dir = 'themes/mytheme/' ; 
po_fetch_code($dir , "/__\(('|\")(.*?)('|\")\)/") ; 
po_fetch_code($dir , "/_e\(('|\")(.*?)('|\")\)/") ; 

是否有任何的方式來提高我的功能?? 謝謝

+0

你是說你想用'_e('? – chris85

+0

是的,還有其他的gettext函數__ – Safy

+0

搜索你的代碼庫嗎?你有沒有訪問命令行/ shell接口? – chris85

回答

1

如果您的目標是生成主題的翻譯目錄,則應使用xgettext命令行工具。嘗試手動解析PHP文件確實沒有意義。

## first, collect all the theme’s PHP files into one temporary file 
find -name "*.php" wp-content/themes/yourtheme > /tmp/themefiles.txt 

# create catalog from translatable strings in your theme’s files 
xgettext --from-code=utf-8 --keyword=__ --keyword=_e --keyword="_n:1,2" --keyword="_x:2c,1" --language=PHP -j -o wp-content/themes/yourtheme/yourtheme.pot -f /tmp/themefiles.txt 

如果您沒有訪問到您的網絡服務器Linux外殼,您可以建立一個Linux VM用一個簡單的發行版如Ubuntu或薄荷,做的東西了xgettext那裏。