2014-01-23 23 views

回答

0

試試這個正則表達式,

$total = array(); 
$translated = array(); 
$extra =''; 


// If fuzzy true then translated count = fuzzy count 
if($fuzzy) { 
    $extra = '#, fuzzy\n'; 
} 

$matched = preg_match_all('/'.$extra.'msgid\s+((?:".*(?<!\\\\)"\s*)+)\s+'.'msgstr\s+((?:".*(?<!\\\\)"\s*)+)/', $po_content, $matches); 

    for ($i = 0; $i < $matched; $i++) { 
     if(trim(substr(rtrim($matches[1][$i]), 1, -1))!="") { 
      $total[] = substr(rtrim($matches[1][$i]), 1, -1); 
     } 
     if(trim(substr(rtrim($matches[2][$i]), 1, -1))!="") { 
      if (strpos(substr(rtrim($matches[2][$i]), 1, -1), 'Language-Team')===false && strpos(substr(rtrim($matches[2][$i]), 1, -1), 'MIME-Version')===false) { 
       $translated[] = substr(rtrim($matches[2][$i]), 1, -1); 
      } 
     } 
    } 

總數=總數($總數); 翻譯的計數= count($翻譯);

3

gettext PO文件非常古老而且無處不在,它們是事實上的行業標準,得到了各種工具的大力支持。嘗試在這裏使用regexen重新創建解決方案似乎非常不合適,因爲您可以使用許多PO文件解析器中的一個來代替。例如oscarotero/Gettext

$translations = Gettext\Extractors\Po::extract('messages.po'); 

$total = $translated = $fuzzy = 0; 

foreach ($translations as $translation) { 
    $total++; 

    if (!$translation->hasTranslation()) { 
     $untranslated++; 
    } 

    if (in_array('fuzzy', $translation->getComments())) { 
     $fuzzy++; 
    } 
} 

(未經測試,而是應該立即開始工作或略有改動。)

其實不過,也有工具來做到這一點已經:Translate ToolkitPology,對於那些我所知道的:

$ pocount locale/ko/LC_MESSAGES/ 

data/locale/ko/LC_MESSAGES/messages.po 
type    strings  words (source) words (translation) 
translated:  3 ( 0%)   7 ( 0%)    28 
fuzzy:   0 ( 0%)   0 ( 0%)    n/a 
untranslated: 729 (99%)  1065 (99%)    n/a 
Total:   732    1072      28 

unreviewed:  3 ( 0%)   7 ( 0%)    28 
empty:   729 (99%)  1065 (99%)    0 

$ posieve stats locale/ko/ 
-    msg msg/tot w-or w/tot-or w-tr ch-or ch-tr 
translated  3  0.4%  15  0.9%  26  93  114 
fuzzy   0  0.0%  0  0.0%  0  0  0 
untranslated 729  99.6% 1708  99.1%  0 17323  0 
total   732   - 1723   -  26 17416  114 
obsolete   0   -  0   -  0  0  0