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);