degola/po-parser

PHP 的 Gettext *.po 解析器

1.0.1 2013-08-08 12:04 UTC

This package is auto-updated.

Last update: 2024-08-29 04:12:17 UTC


README

PHP 的 Gettext *.po 文件解析器。

本包符合 PSR-0PSR-1PSR-2 规范。如果发现不符合规范之处,请通过 pull request 提交补丁。

https://github.com/MAXakaWIZARD/PoParser 分支创建,关于忽略的 pull request。

使用方法

读取文件内容

$parser = new PoParser\Parser();
$parser->read('my-pofile.po');
$entries = $parser->getEntriesAsArrays();
// Now $entries contains every string information in your pofile

echo '<ul>';
foreach ($entries as $entry) {
   echo '<li>'.
   '<b>msgid:</b> '.$entry['msgid'].'<br>'.         // Message ID
   '<b>msgstr:</b> '.$entry['msgstr'].'<br>'.       // Translation
   '<b>reference:</b> '.$entry['reference'].'<br>'. // Reference
   '<b>msgctxt:</b> ' . $entry['msgctxt'].'<br>'.   // Message Context
   '<b>tcomment:</b> ' . $entry['tcomment'].'<br>'. // Translator comment
   '<b>ccomment:</b> ' . $entry['ccomment'].'<br>'. // Code Comment
   '<b>obsolete?:</b> '.(string)$entry['obsolete'].'<br>'. // Is obsolete?
	'<b>fuzzy?:</b> ' .(string)$entry['fuzzy'].     // Is fuzzy?
	'</li>';
}
echo '</ul>';

修改内容

$parser = new PoParser\Parser();
$parser->read('my-pofile.po');
// Entries are stored in array, so you can modify them.

// Use updateEntry method to change messages you want.
$parser->updateEntry('Write your email', 'Escribe tu email');
$parser->write('my-pofile.po');

将 po 文件转换为 jsgettext 内容

用于与来自 http://jsgettext.berlios.de/ 的 jsgettext 库一起使用。

Shell 脚本转换

类似于 jsgettext 库的 perl 脚本 po2json.php,但没有其他依赖。

bin$ po2json.php messages.po messages >messages.json

带有缓存的在线转换

也可以使用库进行实时转换,只要服务器上的 po 文件发生变化即可。在开发过程中提供了更多灵活性。为了避免重复编译 po 文件的开销,还包含了一个简单的缓存功能,该功能仅使用最后修改头和文件mtime 检查。

<?php

define('DOMAIN', 'messages');
define('PO_FILE', '../app/locales/en_US.utf-8/'.DOMAIN.'.po');
define('PO_PARSER_CACHE_DIR', '/tmp/po-parser-converter-cache/');

require '../src/PoParser/Entry.php';
require '../src/PoParser/Parser.php';
require '../src/PoParser/Converter/Cache.php';
require '../src/PoParser/Converter/Driver/jsgettext.php';

header('Content-Type: application/json');

// first load caching class
$ppc = new \PoParser\Converter_Cache(
	// path to po file
	PO_FILE,
	// prefix for caching file, if you use more than one converter driver you have to differentiate here
	'jsgettext-'.DOMAIN,
	// caching path where the caching files are placed
	PO_PARSER_CACHE_DIR
);

// send http header last-modified and if client already cached the file a 304 not modified response
$ppc->setCachingHeaders();
// if client didn't cache the file
if($ppc->isRequestCached() === false) {
	// if the web server didn't cache already the converted po file
	if($ppc->isChanged()) {
		// load, convert and output json file as expected by jsgettext
		$pp = new \PoParser\Converter_Driver_jsgettext(PO_FILE, DOMAIN);
		$localisationData = $pp->getContent();
		$ppc->updateContent($localisationData);

		echo $localisationData;
		unset($localisationData);
		unset($pp);
	} else {
		echo $ppc->getContent();
	}
}
unset($ppc);

待办事项

  • 改进编辑条目的接口。
  • 发现 "#@ " 行的含义。

许可

本库在 MIT 许可下发布。