PHPackages                             sweetchuck/po-parser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. sweetchuck/po-parser

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

sweetchuck/po-parser
====================

GetText PO/POT file parser

2.x-dev(3mo ago)00[4 issues](https://github.com/Sweetchuck/po-parser/issues)GPL-3.0-or-laterPHPPHP &gt;=8.4CI passing

Since Jan 31Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/Sweetchuck/po-parser)[ Packagist](https://packagist.org/packages/sweetchuck/po-parser)[ Docs](https://github.com/Sweetchuck/po-parser)[ RSS](/packages/sweetchuck-po-parser/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelogDependencies (11)Versions (2)Used By (0)

Gettext PO Parser
=================

[](#gettext-po-parser)

[![CircleCI](https://camo.githubusercontent.com/dd01f52e2630094a6917f44300003bd8a959422a837223e94d9fc8ca1e53467a/68747470733a2f2f636972636c6563692e636f6d2f67682f5377656574636875636b2f706f2d7061727365722f747265652f322e782e7376673f7374796c653d737667)](https://circleci.com/gh/Sweetchuck/po-parser/?branch=2.x)[![codecov](https://camo.githubusercontent.com/6ee5b534715e8fdbe99a2ffd98d7eb4dc13f042e456517d643e0e97e7a8e3842/68747470733a2f2f636f6465636f762e696f2f67682f5377656574636875636b2f706f2d7061727365722f6272616e63682f322e782f67726170682f62616467652e7376673f746f6b656e3d48534631364f47507972)](https://app.codecov.io/gh/Sweetchuck/po-parser/branch/2.x)

A PHP library for parsing and reading Gettext PO and POT files. Parse translations, headers, and message context from localization files programmatically.

Features
--------

[](#features)

- Parse Gettext PO/POT files efficiently
- Read translation metadata and headers
- Support for pluralization forms
- Message context (msgctxt) support
- Multiline string handling
- Seekable file iteration for large files
- Memory-efficient streaming parser

Quick Start
-----------

[](#quick-start)

```
use Sweetchuck\PoParser\PoReader;
use Sweetchuck\PoParser\PoHeader;

// Open a PO file.
$fileHandler = fopen('translations.hu.po', 'r');
$reader = new PoReader();
$reader->setFileHandler($fileHandler);

// Read and process all items.
while ($reader->valid()) {
    $item = $reader->current();
    echo $item->getMsgid();
    echo $item->getMsgstr();
    $reader->next();
}

// Or read the header.
$reader->seek(0);
$headerItem = $reader->current();
$header = PoHeader::createFromItem($headerItem);
echo $header->getProjectIdVersion();
```

Usage
-----

[](#usage)

### Parsing Files

[](#parsing-files)

```
$fileHandler = fopen('path/to/file.po', 'r');
$reader = new PoReader();
$reader->setFileHandler($fileHandler);

// Iterate through all messages
$reader->rewind();
while ($reader->valid()) {
    $item = $reader->current();
    // Process $item
    $reader->next();
}
```

### Accessing Message Data

[](#accessing-message-data)

Each `PoItem` contains:

- `getMsgid()` - Original message string
- `getMsgstr()` - Translated message string(s)
- `getMsgctxt()` - Message context
- `getMsgidPlural()` - Plural form of the message
- `getComments()` - Associated comments

### Working with Headers

[](#working-with-headers)

```
$header = PoHeader::createFromItem($item);
echo $header->getProjectIdVersion();
echo $header['Language'];
echo $header['Content-Type'];

// Iterate through all header fields
foreach ($header as $key => $value) {
    echo "$key: $value";
}
```

### Seeking to Specific Items

[](#seeking-to-specific-items)

```
$reader->seek(5);
$item = $reader->current();
```

### Generating PO Files

[](#generating-po-files)

You can create and manipulate PO file content programmatically:

```
use Sweetchuck\PoParser\PoComment;
use Sweetchuck\PoParser\PoReader;
use Sweetchuck\PoParser\PoHeader;
use Sweetchuck\PoParser\PoItem;

// Create a header item
$header = new PoHeader();
$header->setProjectIdVersion('MyProject (1.0.0)');
// or
$header['Project-Id-Version'] = 'MyProject (1.0.0)';
$header['Language'] = 'hu_HU';
$header['Content-Type'] = 'text/plain; charset=UTF-8';
$header['Plural-Forms'] = 'nplurals=2; plural=(n!=1);';

$item0 = PoItem::createFromHeader($header);

// Create a simple message item.
$item1 = PoItem::__set_state([
    'comments' => [
        '# Translator comment',
        '#. Extracted comment about forms',
        '#: src/main.php:42',
        '#, fuzzy',
        '#, c-format',
        '#| msgid "Old Form"',
        '# Additional context line 1',
        '# Additional context line 2',
    ],
    'msgid' => ['Forms'],
    'msgstr' => ['' => ['Űrlapok']],
]);

// Create a plural form item.
$item2Comments = new PoComment();
$item2Comments->setFlag('range', '1..100');
$item2 = new PoItem();
$item2->comments = $item2Comments->toItemValue();
$item2->msgid = ['1 minute'];
$item2->msgidPlural = ['@count minutes'];
$item2->msgstr = [
    0 => ['1 perc'],
    1 => ['@count perc'],
];

// Build file content.
$content = '';
foreach ([$item0, $item1, $item2] as $item) {
    $content .= (string) $item;
    $content .= "\n";
}
```

This generates:

```
msgid ""
msgstr ""
"Project-Id-Version: MyProject (1.0.0)\n"
"Language: hu_HU\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Plural-Forms: nplurals=2; plural=(n!=1);\n"

# Translator comment
#. Extracted comment about forms
#: src/main.php:42
#, fuzzy
#, c-format
#| msgid "Old Form"
# Additional context line 1
# Additional context line 2
msgid "Forms"
msgstr "Űrlapok"

#, range 1..100
msgid "1 minute"
msgid_plural "@count minutes"
msgstr[0] "1 perc"
msgstr[1] "@count perc"

```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance80

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 88% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

107d ago

Major Versions

1.x-dev → 2.x-dev2026-01-31

PHP version history (2 changes)1.x-devPHP &gt;=8.1

2.x-devPHP &gt;=8.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/591103?v=4)[Andor](/maintainers/Sweetchuck)[@Sweetchuck](https://github.com/Sweetchuck)

---

Top Contributors

[![Sweetchuck](https://avatars.githubusercontent.com/u/591103?v=4)](https://github.com/Sweetchuck "Sweetchuck (22 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")

---

Tags

gettextparsergettextpopot

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sweetchuck-po-parser/health.svg)

```
[![Health](https://phpackages.com/badges/sweetchuck-po-parser/health.svg)](https://phpackages.com/packages/sweetchuck-po-parser)
```

###  Alternatives

[nikic/php-parser

A PHP parser written in PHP

17.4k902.6M1.8k](/packages/nikic-php-parser)[doctrine/lexer

PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.

11.2k910.8M118](/packages/doctrine-lexer)[erusev/parsedown

Parser for Markdown.

15.0k151.8M732](/packages/erusev-parsedown)[league/commonmark

Highly-extensible PHP Markdown parser which fully supports the CommonMark spec and GitHub-Flavored Markdown (GFM)

2.9k404.0M702](/packages/league-commonmark)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
