PHPackages                             epiconcept/gettext - 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. [Localization &amp; i18n](/categories/localization)
4. /
5. epiconcept/gettext

ActiveLibrary[Localization &amp; i18n](/categories/localization)

epiconcept/gettext
==================

PHP gettext manager

v5.6.1(4y ago)06.3k↓30%MITPHPPHP ^7.2|^8.0

Since Dec 1Pushed 2y agoCompare

[ Source](https://github.com/Epiconcept-Paris/Gettext)[ Packagist](https://packagist.org/packages/epiconcept/gettext)[ Docs](https://github.com/php-gettext/Gettext)[ Fund](https://paypal.me/oscarotero)[ GitHub Sponsors](https://github.com/oscarotero)[ RSS](/packages/epiconcept-gettext/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (6)Versions (86)Used By (0)

Gettext
=======

[](#gettext)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5ae96a62ece8e5c7b7a1be92dbfcdd64323c99a49ad5e5cd30c107a6d15e4176/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f676574746578742f676574746578742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/gettext/gettext)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![ico-ga](https://github.com/php-gettext/Gettext/workflows/testing/badge.svg)](https://github.com/php-gettext/Gettext/workflows/testing/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/57993b64ab83293fe2ec0ceed752baf0b5b21955df7b08d6d0e48f2419da589e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f676574746578742f676574746578742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/gettext/gettext)

> Note: this is the documentation of the new 5.x version. Go to [4.x branch](https://github.com/php-gettext/Gettext/tree/4.x) if you're looking for the old 4.x version

Created by Oscar Otero   (MIT License)

Gettext is a PHP (^7.2) library to import/export/edit gettext from PO, MO, PHP, JS files, etc.

Installation
------------

[](#installation)

```
composer require gettext/gettext

```

Classes and functions
---------------------

[](#classes-and-functions)

This package contains the following classes:

- `Gettext\Translation` - A translation definition
- `Gettext\Translations` - A collection of translations (under the same domain)
- `Gettext\Scanner\*` - Scan files to extract translations (php, js, twig templates, ...)
- `Gettext\Loader\*` - Load translations from different formats (po, mo, json, ...)
- `Gettext\Generator\*` - Export translations to various formats (po, mo, json, ...)

Usage example
-------------

[](#usage-example)

```
use Gettext\Loader\PoLoader;
use Gettext\Generator\MoGenerator;

//import from a .po file:
$loader = new PoLoader();
$translations = $loader->loadFile('locales/gl.po');

//edit some translations:
$translation = $translations->find(null, 'apple');

if ($translation) {
    $translation->translate('Mazá');
}

//export to a .mo file:
$generator = new MoGenerator();
$generator->generateFile($translations, 'Locale/gl/LC_MESSAGES/messages.mo');
```

Translation
-----------

[](#translation)

The `Gettext\Translation` class stores all information about a translation: the original text, the translated text, source references, comments, etc.

```
use Gettext\Translation;

$translation = Translation::create('comments', 'One comment', '%s comments');

$translation->translate('Un comentario');
$translation->translatePlural('%s comentarios');

$translation->getReferences()->add('templates/comments/comment.php', 34);
$translation->getComments()->add('To display the amount of comments in a post');

echo $translation->getContext(); // comments
echo $translation->getOriginal(); // One comment
echo $translation->getTranslation(); // Un comentario

// etc...
```

Translations
------------

[](#translations)

The `Gettext\Translations` class stores a collection of translations:

```
use Gettext\Translations;

$translations = Translations::create('my-domain');

//You can add new translations:
$translation = Translation::create('comments', 'One comment', '%s comments');
$translations->add($translation);

//Find a specific translation
$translation = $translations->find('comments', 'One comment');

//Edit headers, domain, etc
$translations->getHeaders()->set('Last-Translator', 'Oscar Otero');
$translations->setDomain('my-blog');
```

Loaders
-------

[](#loaders)

The loaders allows to get gettext values from any format. For example, to load a .po file:

```
use Gettext\Loader\PoLoader;

$loader = new PoLoader();

//From a file
$translations = $loader->loadFile('locales/en.po');

//From a string
$string = file_get_contents('locales2/en.po');
$translations = $loader->loadString($string);
```

This package includes the following loaders:

- `MoLoader`
- `PoLoader`

And you can install other formats with loaders and generators:

- [Json](https://github.com/php-gettext/Json)

Generators
----------

[](#generators)

The generators export a `Gettext\Translations` instance to any format (po, mo, etc).

```
use Gettext\Loader\PoLoader;
use Gettext\Generator\MoGenerator;

//Load a PO file
$poLoader = new PoLoader();

$translations = $poLoader->loadFile('locales/en.po');

//Save to MO file
$moGenerator = new MoGenerator();

$moGenerator->generateFile($translations, 'locales/en.mo');

//Or return as a string
$content = $moGenerator->generateString($translations);
file_put_contents('locales/en.mo', $content);
```

This package includes the following generators:

- `MoGenerator`
- `PoGenerator`

And you can install other formats with loaders and generators:

- [Json](https://github.com/php-gettext/Json)

Scanners
--------

[](#scanners)

Scanners allow to search and extract new gettext entries from different sources like php files, twig templates, blade templates, etc. Unlike loaders, scanners allows to extract gettext entries with different domains at the same time:

```
use Gettext\Scanner\PhpScanner;
use Gettext\Translations;

//Create a new scanner, adding a translation for each domain we want to get:
$phpScanner = new PhpScanner(
    Translations::create('domain1'),
    Translations::create('domain2'),
    Translations::create('domain3')
);

//Set a default domain, so any translations with no domain specified, will be added to that domain
$phpScanner->setDefaultDomain('domain1');

//Extract all comments starting with 'i18n:' and 'Translators:'
$phpScanner->extractCommentsStartingWith('i18n:', 'Translators:');

//Scan files
foreach (glob('*.php') as $file) {
    $phpScanner->scanFile($file);
}

//Get the translations
list('domain1' => $domain1, 'domain2' => $domain2, 'domain3' => $domain3) = $phpScanner->getTranslations();
```

This package does not include any scanner by default. But there are some that you can install:

- [PHP Scanner](https://github.com/php-gettext/PHP-Scanner)
- [JS Scanner](https://github.com/php-gettext/JS-Scanner)

Merging translations
--------------------

[](#merging-translations)

You will want to update or merge translations. The function `mergeWith` create a new `Translations` instance with other translations merged:

```
$translations3 = $translations1->mergeWith($translations2);
```

But sometimes this is not enough, and this is why we have merging options, allowing to configure how two translations will be merged. These options are defined as constants in the `Gettext\Merge` class, and are the following:

ConstantDescription`Merge::TRANSLATIONS_OURS`Use only the translations present in `$translations1``Merge::TRANSLATIONS_THEIRS`Use only the translations present in `$translations2``Merge::TRANSLATION_OVERRIDE`Override the translation and plural translations with the value of `$translation2``Merge::HEADERS_OURS`Use only the headers of `$translations1``Merge::HEADERS_REMOVE`Use only the headers of `$translations2``Merge::HEADERS_OVERRIDE`Overrides the headers with the values of `$translations2``Merge::COMMENTS_OURS`Use only the comments of `$translation1``Merge::COMMENTS_THEIRS`Use only the comments of `$translation2``Merge::EXTRACTED_COMMENTS_OURS`Use only the extracted comments of `$translation1``Merge::EXTRACTED_COMMENTS_THEIRS`Use only the extracted comments of `$translation2``Merge::FLAGS_OURS`Use only the flags of `$translation1``Merge::FLAGS_THEIRS`Use only the flags of `$translation2``Merge::REFERENCES_OURS`Use only the references of `$translation1``Merge::REFERENCES_THEIRS`Use only the references of `$translation2`Use the second argument to configure the merging strategy:

```
$strategy = Merge::TRANSLATIONS_OURS | Merge::HEADERS_OURS;

$translations3 = $translations1->mergeWith($translations2, $strategy);
```

There are some typical scenarios, one of the most common:

- Scan php templates searching for entries to translate
- Complete these entries with the translations stored in a .po file
- You may want to add new entries to the .po file
- And also remove those entries present in the .po file but not in the templates (because they were removed)
- But you want to update some translations with new references and extracted comments
- And keep the translations, comments and flags defined in .po file

For this scenario, you can use the option `Merge::SCAN_AND_LOAD` with the combination of options to fit this needs (SCAN new entries and LOAD a .po file).

```
$newEntries = $scanner->scanFile('template.php');
$previousEntries = $loader->loadFile('translations.po');

$updatedEntries = $newEntries->mergeWith($previousEntries);
```

More common scenarios may be added in a future.

Contributors
------------

[](#contributors)

Thanks to all [contributors](https://github.com/oscarotero/Gettext/graphs/contributors) specially to [@mlocati](https://github.com/mlocati).

---

Please see [CHANGELOG](CHANGELOG.md) for more information about recent changes and [CONTRIBUTING](CONTRIBUTING.md) for contributing details.

The MIT License (MIT). Please see [LICENSE](LICENSE) for more information.

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity84

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 68.8% 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 ~44 days

Recently: every ~175 days

Total

83

Last Release

924d ago

Major Versions

v4.8.0 → v5.0.02019-11-04

v4.8.1 → v5.2.02019-11-25

v4.8.2 → v5.2.12019-12-08

v4.8.3 → v5.5.32020-12-01

v4.8.6 → v5.6.02021-11-05

PHP version history (4 changes)v1.0PHP &gt;=5.3.0

v4.0.0PHP &gt;=5.4.0

v5.0.0PHP ^7.2

v5.5.3PHP ^7.2|^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/28560976?v=4)[epi-fwk](/maintainers/epi-fwk)[@epi-fwk](https://github.com/epi-fwk)

---

Top Contributors

[![oscarotero](https://avatars.githubusercontent.com/u/377873?v=4)](https://github.com/oscarotero "oscarotero (454 commits)")[![mlocati](https://avatars.githubusercontent.com/u/928116?v=4)](https://github.com/mlocati "mlocati (62 commits)")[![schlessera](https://avatars.githubusercontent.com/u/83631?v=4)](https://github.com/schlessera "schlessera (42 commits)")[![swissspidy](https://avatars.githubusercontent.com/u/841956?v=4)](https://github.com/swissspidy "swissspidy (14 commits)")[![briedis](https://avatars.githubusercontent.com/u/500491?v=4)](https://github.com/briedis "briedis (14 commits)")[![ScreamingDev](https://avatars.githubusercontent.com/u/2559177?v=4)](https://github.com/ScreamingDev "ScreamingDev (8 commits)")[![soukicz](https://avatars.githubusercontent.com/u/1814750?v=4)](https://github.com/soukicz "soukicz (7 commits)")[![asmecher](https://avatars.githubusercontent.com/u/200411?v=4)](https://github.com/asmecher "asmecher (7 commits)")[![eusonlito](https://avatars.githubusercontent.com/u/644551?v=4)](https://github.com/eusonlito "eusonlito (5 commits)")[![hmpf](https://avatars.githubusercontent.com/u/11956096?v=4)](https://github.com/hmpf "hmpf (5 commits)")[![remicollet](https://avatars.githubusercontent.com/u/270445?v=4)](https://github.com/remicollet "remicollet (4 commits)")[![vvh-empora](https://avatars.githubusercontent.com/u/5236105?v=4)](https://github.com/vvh-empora "vvh-empora (3 commits)")[![Darkmet98](https://avatars.githubusercontent.com/u/13910563?v=4)](https://github.com/Darkmet98 "Darkmet98 (3 commits)")[![delmicio](https://avatars.githubusercontent.com/u/2908209?v=4)](https://github.com/delmicio "delmicio (3 commits)")[![maxhelias](https://avatars.githubusercontent.com/u/12966574?v=4)](https://github.com/maxhelias "maxhelias (3 commits)")[![ocean90](https://avatars.githubusercontent.com/u/617637?v=4)](https://github.com/ocean90 "ocean90 (3 commits)")[![velosipedist](https://avatars.githubusercontent.com/u/653773?v=4)](https://github.com/velosipedist "velosipedist (3 commits)")[![ArthurHoaro](https://avatars.githubusercontent.com/u/1962678?v=4)](https://github.com/ArthurHoaro "ArthurHoaro (2 commits)")[![lbfeenix](https://avatars.githubusercontent.com/u/74542573?v=4)](https://github.com/lbfeenix "lbfeenix (2 commits)")[![ideag](https://avatars.githubusercontent.com/u/3252474?v=4)](https://github.com/ideag "ideag (2 commits)")

---

Tags

i18ntranslationJSgettextpomo

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/epiconcept-gettext/health.svg)

```
[![Health](https://phpackages.com/badges/epiconcept-gettext/health.svg)](https://phpackages.com/packages/epiconcept-gettext)
```

###  Alternatives

[gettext/gettext

PHP gettext manager

70130.2M102](/packages/gettext-gettext)[sepia/po-parser

Gettext \*.PO file parser for PHP.

1271.5M19](/packages/sepia-po-parser)[phpmyadmin/motranslator

Translation API for PHP using Gettext MO files

591.8M8](/packages/phpmyadmin-motranslator)[om/potrans

Command line tool for translate Gettext with Google Translator API or DeepL API

10515.0k4](/packages/om-potrans)[gettext/php-scanner

PHP scanner for gettext

15471.2k12](/packages/gettext-php-scanner)[fisharebest/localization

A lightweight localization database and translation tools, with data from the CLDR, IANA, ISO, etc.

3191.1k2](/packages/fisharebest-localization)

PHPackages © 2026

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