-
Notifications
You must be signed in to change notification settings - Fork 57
Documentation 5.0
Sepia\PoParser\Parser
is the service you need to use to parse PO data.
It requires a SourceHandler
implementation in order to read data from a source. This library includes two SourceHandler
implementations:
-
Sepia\PoParser\SourceHandler\FileSystem
: To read from the file system. -
Sepia\PoParser\SourceHandler\StringSource
: To read from a string.
Feel free to implement your own SourceHandler
if those two doesn't fit you.
use Sepia\PoParser\SourceHandler\FileSystem;
use Sepia\PoParser\Parser;
$fileHandler = new FileSystem('./translations/en.po');
$parser = new Parser($fileHandler);
Once you've instantiated a Parser
object, you can just call parse()
method to parse your PO data:
$catalog = $parser->parse();
parse()
method returns a Catalog
, which contains the list of entries found in your PO data source.
Previous examples can be replaced by just a call to the helper method parseFile()
:
$catalog = Sepia\PoParser\Parser::parseFile('./translations.po');
These are the two helper methods that Parser
class offers:
-
parseString($string)
: Parses PO data stored in a string. -
parseFile($filePath)
: Parses PO data stored in a file located in the file system.
Sepia\PoParser\Parser::parse()
returns a Catalog
object containing all entries found in your PO data source.
This Catalog
contains following methods to read its content:
-
getEntries()
: Returns full content of the Catalog. Returns an array ofEntry
's. -
getEntry($msgId, $msgCtxt)
: Tries to find an entry by its msgid and (if supplied) its msgctxt. If the Entry has a context defined, you must supply it. Returns a singleEntry
, ornull
if entry was not found. -
getHeader()
: Returns aHeader
object containing all parsed headers. -
getHeaders()
: Returns a string[] containing all parsed headers.
Entries inside the catalog can be identified by its msgid (message id) and its msgctxt (message context).
If these concepts look new to you, please read some documentation about PO files for more details.
These two methods return Entry
objects, which are representations of every entry found in the PO data.
This Entry object contains the following properties which can be accessed by a series of intuitive getters
and setters
:
getMsgId(): string
getMsgStr(): string
getMsgIdPlural(): array[]
getMsgStrPlurals(): string[]
getMsgCtxt(): string[]
getPreviousEntry(): ?Entry
getFlags(): string[]
getTranslatorComments(): string[]
getDeveloperComments(): string[]
getReference(): string[]
isObsolete(): bool
isFuzzy(): bool
isPlural(): bool
Entry
object offers setters to modify each property available:
setMsgId($msgId)
setMsgStr($msgStr)
setMsgIdPlural($msgIdPlural)
setMsgStrPlurals($msgStrPlurals)
setMsgCtxt($msgCtxt)
setPreviousEntry($previousEntry)
setObsolete($obsolete)
setFlags($flags)
setTranslatorComments($translatorComments)
setDeveloperComments($developerComments)
setReference($reference)
Catalog
also contains a method to add new entries:
$catalog->addEntry(new Entry('welcome.user', 'Welcome %user%!'));
As expected, there is also a method to remove an entry:
$msgid = 'welcome.user';
$catalog->removeEntry($msgid);
If the Entry has a context defined, you must supply it:
$msgid = 'welcome.user';
$msgctxt = 'register.welcome';
$catalog->removeEntry($msgid, $msgctxt);
Use PoCompiler
class to convert a Catalog
back to a string ready to be dumped into a file or wherever you need: