我正在寻找一种从 .po 本地化文件创建 excel 或 CSV 文件的简单方法。
我无法通过 Google 找到任何内容,所以我想自己用 PHP 编写它。
PO文件有这样的结构
msgstr "标题"
msgstr "标题"
所以我想我需要我的 PHP 脚本来解析 .po 文件,以寻找“每次出现关键字 msgstr 后逗号之间的第一位文本”。
我认为这是正则表达式的工作,所以我试过了,但它没有返回任何东西:
$po_file = '/path/to/messages.po';
if(!is_file($po_file)){
die("you got the filepath wrong dude.");
}
$str = file_get_contents($po_file);
// find all occurences of msgstr "SOMETHING"
preg_match('@^msgstr "([^/]+)"@i', $str, $matches);
$msgstr = $matches[1];
var_dump($msgstr);
最佳答案
有一个不错的梨图书馆。 File_Gettext
如果您查看源 File/Gettext/PO.php,您会看到您需要的正则表达式模式:
$matched = preg_match_all('/msgid\s+((?:".*(?<!\\\\)"\s*)+)\s+' .
'msgstr\s+((?:".*(?<!\\\\)"\s*)+)/',
$contents, $matches);
for ($i = 0; $i < $matched; $i++) {
$msgid = substr(rtrim($matches[1][$i]), 1, -1);
$msgstr = substr(rtrim($matches[2][$i]), 1, -1);
$this->strings[parent::prepare($msgid)] = parent::prepare($msgstr);
}
或者只使用梨库:
include 'File/Gettext/PO.php';
$po = new File_Gettext_PO();
$po->load($poFile);
$poArray = $po->toArray();
foreach ($poArray['strings'] as $msgid => $msgstr) {
// write your csv as you like...
}
关于php - 将 .po 文件导出到 .csv,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20476520/