PHPackages                             gggeek/polyfill-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. gggeek/polyfill-gettext

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

gggeek/polyfill-gettext
=======================

A replacement for the php gettext extension. Based on php-gettext by Danilo Segan.

2.0.0-beta3(8mo ago)2143[1 issues](https://github.com/gggeek/polyfill-gettext/issues)GPL-2.0-or-laterPHPPHP &gt;=5.3.0CI passing

Since Jul 20Pushed 8mo agoCompare

[ Source](https://github.com/gggeek/polyfill-gettext)[ Packagist](https://packagist.org/packages/gggeek/polyfill-gettext)[ RSS](/packages/gggeek-polyfill-gettext/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (2)Versions (4)Used By (0)

Polyfill-Gettext
================

[](#polyfill-gettext)

A pure-php implementation of the API provided by the [PHP gettext extension](https://www.php.net/manual/en/book.gettext.php).

Evolved from the php-gettext codebase available at .

[![License](https://camo.githubusercontent.com/2eb6c67ce51845702ba67931ffb8d3848ad2bbbf67379bc1d0aee501fd34c9c4/68747470733a2f2f706f7365722e707567782e6f72672f67676765656b2f706f6c7966696c6c2d676574746578742f6c6963656e7365)](https://packagist.org/packages/gggeek/polyfill-gettext)[![Latest Stable Version](https://camo.githubusercontent.com/207eeb26ec37b29f90fcaaa08367df9122f633381dee870287f1d1ea1f063332/68747470733a2f2f706f7365722e707567782e6f72672f67676765656b2f706f6c7966696c6c2d676574746578742f762f737461626c65)](https://packagist.org/packages/gggeek/polyfill-gettext)[![Total Downloads](https://camo.githubusercontent.com/4bfbf71b8cd262f447cb624ed554f10351cafe5721c5ec11795d06615241da81/68747470733a2f2f706f7365722e707567782e6f72672f67676765656b2f706f6c7966696c6c2d676574746578742f646f776e6c6f616473)](https://packagist.org/packages/gggeek/polyfill-gettext)

[![Build Status](https://github.com/gggeek/polyfill-gettext/actions/workflows/ci.yaml/badge.svg)](https://github.com/gggeek/polyfill-gettext/actions/workflows/ci.yaml)[![Code Coverage](https://camo.githubusercontent.com/c751b0eeba9ccfaf11b56bf0489ddb194371ab8ede7621b5b93ebc80b4ae3f91/68747470733a2f2f636f6465636f762e696f2f67682f67676765656b2f706f6c7966696c6c2d676574746578742f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://app.codecov.io/gh/gggeek/phpxmlrpc)

Licensed under the GPLv2 (or any later version), see [LICENSE](LICENSE)

Copyright 2003, 2006, 2009 -- Danilo "angry with PHP\[1\]" Segan.

Introduction
------------

[](#introduction)

Polyfill-Gettext implements an API for internationalization of your php application, i.e. it provides methods which can be used to translate to different languages the strings displayed by the user interface.

Translations are read from GNU gettext MO files. Those are binary containers for translations, produced by e.g. GNU msgfmt.

Note that this library does not provide anything to create, edit or manage the translation files, however it should not be too hard to find tutorials on how to do that using a variety of tools and accommodating different workflows.

Features
--------

[](#features)

- Support for simple translations
- Support for `ngettext` calls (plural forms).

    You may also use plural forms. Translations in MO files need to provide this, and they must also provide "plural-forms" header. Please see `info gettext` for more details.
- Support for reading translations from straight files, or strings.

    Since different backends can conceivably be used for storing and reading in the MO file data, a class implementing `StreamReaderInterface` can be provided to handle all the input. For your convenience, two classes for reading files are already implemented: `FileReader` and `StringReader` (`CachedFileReader` being a combination of the two: it loads entire file contents into a string, and then works on that). You can for instance use `StringReader`when you read in data from a database, or you can create your own implementation of `StreamReaderInterface` for anything you like.

    See the example below for more details.

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

[](#installation)

Install the library using Composer, then be sure to require the Composer autoloader in your code before calling any gettext function.

Usage
-----

[](#usage)

### Standard gettext API emulation

[](#standard-gettext-api-emulation)

Basically, you will be able to use in your code all the standard gettext functions documented on:

```
   https://www.php.net/gettext

```

regardless of the fact that the php gettext extension is available and enabled (via php ini settings) or not.

This makes the polyfill a useful inclusion for every php application which might see widespread usage on shared hosting, where there is often little control over php configuration and the installed extensions.

The only improvement you might want to make to your code is to check the return value of the `setlocale()` call to see if the chosen locale is system supported or not. If it is not, making a call to `PGettext\T::setlocale($locale)` will make translation calls work in any case - but only if the native php gettext extension is disabled.

See the example file `examples/pigs_dropin.php` for more details.

### Usage as fallback for unsupported locales

[](#usage-as-fallback-for-unsupported-locales)

By using the functions provided by this library instead of the gettext API, your translations will be used via gettext emulation whenever:

- either the php gettext extension is not available,
- or the php gettext extension is enabled, but *the desired locale is not installed in the system*.

This is an improvement over the API emulation usage described above, as it allows to use gettext functions regardless of the fact that the desired locale has been installed on the system (as a reminder: for gettext to work, the locales to be used have to be manually installed on the system. This is achieved separately from php setup/configuration).

Note that, if the php gettext extension is available, and the desired locale is installed in the system, the native gettext functions will be used transparently, for maximum execution speed.

See the example file `examples/pigs_fallback.php` for more details.

### Managing the translation files (.mo, .po)

[](#managing-the-translation-files-mo-po)

\[To be documented...\]

### Customizing library usage

[](#customizing-library-usage)

#### Providing translation files from custom storage

[](#providing-translation-files-from-custom-storage)

You can create custom 'stream reader' classes (a class that implements `StreamReaderInterface`) which will provide data for the `gettext_reader`, and/or custom 'reader' classes (a class that implements `ReaderInterface`).

After having created your custom classes, you can make use of them, in various ways.

The shortest version:

Set the names of your classes to `T::$reader_class` and/or `T::$stream_reader_class`. Done! Note that this requires compatibility of your new classes constructor arguments with the existing ones.

The medium version:

Create a subclass of `Pgettext\T`, and override its methods `T::build_reader` and `T::build_stream_reader`. Use your subclass in translation calls. Note that this does not support transparent emulation of the php native extension functions.

The long version:

Create one `StreamReaderInterface` instance which will provide data for the `ReaderInterface`, with eg.

```
$streamer = new MyFileStream('data.mo');

```

Then, use that as a parameter to reader constructor:

```
$wohoo = new MyGettextReader($streamer, $whatever, $args...);

```

If you want to disable pre-loading of entire message catalog in memory (if, for example, you have a multi-thousand message catalog which you'll use only occasionally), use `false` for the second parameter to the gettext\_reader constructor:

```
$wohoo = new PGettext\gettext_reader($streamer, false);

```

From now on, you have all the benefits of gettext data at your disposal, so may run:

```
print $wohoo->translate("This is a test");
print $wohoo->ngettext("%d bird", "%d birds", $birds);

```

You might need to pass parameter `-k` to `xgettext` to make it extract all the strings from the php source code. For the above example, try with

```
xgettext -ktranslate -kngettext:1,2 file.php

```

That should create `messages.po` which contains two messages for translation.

I suggest creating simple aliases for those functions.

Examples
--------

[](#examples)

See in the `examples/` subdirectory, there are a couple of files. `pigs_dropin.php` and `pigs_fallback.php` are example usages, `locale/xx_XX/LC_MESSAGES/messages.po` is a translation for each language, and `messages.mo` is the corresponding binary version, generated with

```
msgfmt -o messages.mo messages.po

```

There is also a simple `update.sh` script that can be used to generate the PO and MO files via calls to `xgettext` and `msgfmt`.

Known limitations
-----------------

[](#known-limitations)

The following are known limitations in the emulation of the native gettext extension API:

- not all warnings / exceptions are emulated, which would be thrown by the native gettext extension, when invalid parameters are passed to functions such as f.e. passing an empty string to `textdomain` calls

Bugs
----

[](#bugs)

Report bugs and feature requests at

Todo
----

[](#todo)

- support other means than using mbstring of converting between character sets
- expand the test suite and improve the emulation of the native gettext API to cover errors, returned values, support for all environment variables, etc...
- Improve speed
- Try to use hash tables in MO files: with pre-loading, would it be useful at all?

Frequently asked questions
--------------------------

[](#frequently-asked-questions)

- Why yet another reimplementation of the gettext extension API?

    Because at the time I was looking for one, I did not find any that fit the bill.

    The original php-gettext package has not seen any commit or release since 2015.

    On github, there is , but it does not attempt to be a transparent drop-in.

    Same goes for all the packages which can be found on Packagist while searching for 'gettext', such as .

    After starting this, in July 2025, I found out the  project, which does in fact stay close to the original php-gettext functionality, and might be a good candidate to use instead of this library. The main differences, as far as I can tell from a cursory analysis, are: it does require php 8.2, and symfony/expression-language, while we are self-contained and require php 5.3 or later, and it does not automatically reimplement the php gettext api when the extension is not loaded, registering instead the same functions prefixed with `_`.

Original readme
---------------

[](#original-readme)

See [README\_original.md](README_original.md)

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance49

Moderate activity, may be stable

Popularity17

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity19

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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 ~14 days

Total

3

Last Release

267d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f641f7d6edabf83c56f6c4998b04e6c59a00b46bf51ca6699c31b09f9c948677?d=identicon)[gggeek](/maintainers/gggeek)

---

Top Contributors

[![gggeek](https://avatars.githubusercontent.com/u/308634?v=4)](https://github.com/gggeek "gggeek (89 commits)")

---

Tags

gettextl10nl10ntranslationgettext

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[gettext/gettext

PHP gettext manager

70530.2M102](/packages/gettext-gettext)[fisharebest/localization

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

3191.1k2](/packages/fisharebest-localization)[delight-im/i18n

Internationalization and localization for PHP

625.2k3](/packages/delight-im-i18n)[loco/loco

Loco SDK for PHP, including REST API client

19626.8k4](/packages/loco-loco)[inpsyde/multilingual-press

Simply THE multisite-based free open source plugin for your multilingual websites.

2414.0k1](/packages/inpsyde-multilingual-press)[gettext/php-scanner

PHP scanner for gettext

15471.2k12](/packages/gettext-php-scanner)

PHPackages © 2026

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