Perl 操作 .po 文件 示例代碼

Perl , .po 都是古老的技術。Perl 存在感一直不錯,.po 也是易於上手的。

最近做本地化時,發現這方面的內容不多,下面給出實用的代碼,希望幫助採用此項技術的同仁能快速瞭解使用的最佳實踐。

use Locale::PO;
......
#load the po file
my $aref = Locale::PO->load_file_asarray($from_file);
foreach my $tpo (@$aref) {
# lines that begin with #~  are obsolete
  if ( $tpo->obsolete() || length(trim(Locale::PO->dequote($tpo->msgid())))<1){
    ...
  }else{
#dequote removes the quotes.
       my $theEn = Locale::PO->dequote($tpo->msgstr());

       if ( length( $theEn) < 1) {  #if msgstr is "", use msgid
            $theEn = Locale::PO->dequote($tpo->msgid());
       }

       if( length( $theEn) > 0 ){
            ...   
            Encode::_utf8_on($theZh);
            $tpo->msgstr($theZh);
        }
  }
}
#save the po
my $res = Locale::PO->save_file_fromarray($to_file,$aref);

 

發佈了39 篇原創文章 · 獲贊 14 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章