如何从php数组生成po文件?

时间:2012-01-05 22:04:53

标签: localization translation poedit po mo

我有一个php语言文件,列出数组中的值。

我想将其转换为.po文件。

php文件如下所示:

<?php 

$LANG_ = array(

// advanced search buttons
"_search1"              => "Start Search",
"_search2"              => "Hide Search Box",
"_search3"              => "Search",

// page numbering
"_pn1"              => "Page %CURRENT_PAGE% of %TOTAL_PAGES%",
"_pn2"              => "<< First",
"_pn3"              => "Last >>",

// _tpl_article-add.php
"_tpl_article-add1"             => "Article Submission Form",
"_tpl_article-add2"             => "Submit Article",
"_tpl_article-add3"             => "Article Submitted Successfully",


// errors
"_err14"            => "Delete",
"_err144"           => "Display Image",
"_err15"            => "Edit",
"_err16"            => "View",

);
?>

这只是一个例子,文件本身很庞大,超过3000行。将手机中的每一个插入到po目录中都会让我失望。有什么东西可以为我自动化吗?

我正在使用poedit。

谢谢,我是新手,所以任何见解都会有用......

2 个答案:

答案 0 :(得分:3)

尝试php2po将PHP数组转换为PO文件,并确保所有转义都正确无误。然后可以在Poedit或Virtaal等工具中编辑PO文件。

答案 1 :(得分:2)

第一个条目是文件头,查看现有的 gettext 文件(-po)所需的内容。我用addslashes做了逃避;也许你需要做更多。

$fh = fopen("en.po", 'w');
fwrite($fh, "#\n");
fwrite($fh, "msgid \"\"\n");
fwrite($fh,  "msgstr \"\"\n");

foreach ($LANG_ as $key => $value) {
    $key = addslashes($key);
    $value = addslashes($value);
    fwrite($fh, "\n");
    fwrite($fh, "msgid \"$key\"\n");
    fwrite($fh, "msgstr \"$value\"\n");
}
fclose($fh);