geekwright/po
最新稳定版本:v2.0.2
Composer 安装命令:
composer require geekwright/po
包简介
Objects to assist in reading, manipulating and creating GNU gettext style PO files
README 文档
README
Po is a set of objects to assist in reading, manipulating and creating GNU gettext style PO files.
Installing
The recommended installation method is using composer. To add "geekwright/po" to your composer managed project, use this command:
composer require geekwright/po
PHP Support
Po version 1 supports PHP 5.3 and above. Begining with version 2, Po requires a minimum of PHP 7.1.
Namespace
All Po classes are in the Geekwright\Po namespace.
Examples
Po provides the capability to create, read, and modify PO and POT files, including the ability to scan PHP sources for gettext style calls to build a POT file. You can connect the pieces however you need, but here are a few examples for common situations.
Reading a PO File
try { $poFile = new PoFile(); $poFile->readPoFile('test.po'); // list all the messages in the file $entries = $poFile->getEntries(); foreach($entries as $entry) { echo $entry->getAsString(PoTokens::MESSAGE); } } catch (UnrecognizedInputException $e) { // we had unrecognized lines in the file, decide what to do } catch (FileNotReadableException $e) { // the file couldn't be read, nothing happened }
Get the Plural-Forms Header
$pluralRule = $poFile->getHeaderEntry()->getHeader('plural-forms');
Add a New Entry
$entry = new PoEntry; $entry->set(PoTokens::MESSAGE, 'This is a message.'); $entry->set(PoTokens::FLAG, 'fuzzy'); $poFile->addEntry($entry);
Get the Translation for an Entry
The translation for an entry can be a string, or an array of strings if the Entry is a plural form. This code fragment will assign the translation to $msgstr appropriate for either case.
$msgid_plural = $entry->get(PoTokens::PLURAL); if (empty($msgid_plural)) { $msgstr = $entry->getAsString(PoTokens::TRANSLATED); } else { $msgstr = $entry->getAsStringArray(PoTokens::TRANSLATED); }
Writing a PO File
try { $poFile->writePoFile('test.po'); } catch (FileNotWriteableException $e) { // the file couldn't be written }
Create a POT File from PHP sources
$poFile = new PoFile(); $poInit = new PoInitPHP($poFile); foreach (glob("*.php") as $filename) { try { $poInit->msginitFile($filename); } catch (FileNotReadableException $e) { // the souce file couldn't be read, decide what to do } } try { $poFile->writePoFile('default.pot'); } catch (FileNotWriteableException $e) { // the file couldn't be written }
API
For more information, see the full Po API documentation.
统计信息
- 总下载量: 180.81k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 1
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2015-03-01