【问题标题】:Get translated count and fuzzy count ... from po file using regex使用正则表达式从 po 文件中获取翻译计数和模糊计数...
【发布时间】:2014-02-13 13:39:54
【问题描述】:

我需要一些正则表达式来拆分 PO(语言翻译文件)文件的翻译计数、模糊计数和总字符串计数。

我用PHP编写程序,我到处搜索但找不到。

请帮帮我。

【问题讨论】:

    标签: php regex gettext po


    【解决方案1】:

    gettext PO 文件非常古老且无处不在,它们是事实上的行业标准,并得到各种工具的大力支持。当您可以使用许多 PO 文件解析器之一来代替时,尝试在这里使用 regexen 重新发明解决方案似乎非常不合适。例如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
    

    【讨论】:

      【解决方案2】:

      试试这个正则表达式,

      $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($total); 翻译计数 = count($translated);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多