PHPackages                             datalinx/gettext-context - 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. datalinx/gettext-context

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

datalinx/gettext-context
========================

Implementation of context functions for the PHP gettext extension

v2.0.0(3y ago)425.5k↓33.7%1MITPHPPHP &gt;=7.4

Since Sep 26Pushed 2y ago1 watchersCompare

[ Source](https://github.com/DataLinx/gettext-context)[ Packagist](https://packagist.org/packages/datalinx/gettext-context)[ Docs](https://github.com/DataLinx/gettext-context)[ RSS](/packages/datalinx-gettext-context/feed)WikiDiscussions master Synced 1mo ago

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

gettext-context
===============

[](#gettext-context)

Implementation of context functions for the PHP gettext extension

[![Packagist Version](https://camo.githubusercontent.com/e991dce0ba62310cc875e2e1e6fb9eec69bec084f9aacd82ee26b0f39a12cd39/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646174616c696e782f676574746578742d636f6e74657874)](https://camo.githubusercontent.com/e991dce0ba62310cc875e2e1e6fb9eec69bec084f9aacd82ee26b0f39a12cd39/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646174616c696e782f676574746578742d636f6e74657874)[![Packagist PHP Version Support](https://camo.githubusercontent.com/fc9764c0f6810a0635dfce0e08503be6474ebb757cbd70e44af331580058ecf3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f646174616c696e782f676574746578742d636f6e74657874)](https://camo.githubusercontent.com/fc9764c0f6810a0635dfce0e08503be6474ebb757cbd70e44af331580058ecf3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f646174616c696e782f676574746578742d636f6e74657874)[![Packagist Downloads](https://camo.githubusercontent.com/96bd49f5f8ba5f3e586253a30578ab8978fca59d291daadcc3fce6b0aea59497/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f646174616c696e782f676574746578742d636f6e74657874)](https://camo.githubusercontent.com/96bd49f5f8ba5f3e586253a30578ab8978fca59d291daadcc3fce6b0aea59497/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f646174616c696e782f676574746578742d636f6e74657874)[![Tests](https://github.com/DataLinx/gettext-context/actions/workflows/test-runner.yml/badge.svg)](https://github.com/DataLinx/gettext-context/actions/workflows/test-runner.yml)[![codecov](https://camo.githubusercontent.com/caca161d6eecc24c8f0d34081a590ee898b61e390e1cdb39d3cf5f8a918d5bf2/68747470733a2f2f636f6465636f762e696f2f67682f446174614c696e782f676574746578742d636f6e746578742f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d4132375245583230364c)](https://codecov.io/gh/DataLinx/gettext-context)[![Conventional Commits](https://camo.githubusercontent.com/9f7a75f0e3af8cca0597d218e8708da4791128679178573a9806a514be52d2eb/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f436f6e76656e74696f6e616c253230436f6d6d6974732d312e302e302d2532334645353139363f6c6f676f3d636f6e76656e74696f6e616c636f6d6d697473266c6f676f436f6c6f723d7768697465)](https://conventionalcommits.org)[![Packagist License](https://camo.githubusercontent.com/dfe46564adf3cc10da80df94bb7d3774a2d4040e8e0ef8d239d58b8ce42087c8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f646174616c696e782f676574746578742d636f6e74657874)](https://camo.githubusercontent.com/dfe46564adf3cc10da80df94bb7d3774a2d4040e8e0ef8d239d58b8ce42087c8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f646174616c696e782f676574746578742d636f6e74657874)

About
-----

[](#about)

PHP still lacks full support for gettext, because it does not implement the context functions. Until this is sorted out, you can use this package to add context support to your localization efforts.

For example, in English, both persons and products have a "name" attribute. So using a simple gettext:

```
echo _('Name');
```

... would yield "Ime" in Slovenian, which is wrong.

To solve this, you can use a context function provided by this package:

```
echo pgettext('Person', 'Name'); // Echoes "Ime"
echo pgettext('Product', 'Name'); // Echoes "Naziv"
```

Requirements
------------

[](#requirements)

- PHP &gt;= 7.4
- gettext PHP extension

It can be used on lower versions of PHP, but you won't be able to install it with composer (or run tests).

This package is tested for Linux, but it should work for other systems too. Any Windows and Mac feedback is highly welcome. It would be great if you could make a pull request for testing on those platforms (including the [GitHub workflow](.github/workflows/test-runner.yml)).

Installing
----------

[](#installing)

1. Download it with composer: `composer require datalinx/gettext-context`
2. Include the `vendor/datalinx/gettext-context/src/gettext-context.php` file when you need it\*

\* It's not added to the `autoload` directive, since you might not need or want to always include it in runtime. If you want to always load it, just add the source file to your `composer.json` autoload files list:

```
{
    "autoload": {
        "files": [
            "vendor/datalinx/gettext-context/src/gettext-context.php"
        ]
    }
}
```

Usage
-----

[](#usage)

See the documented [src/gettext-context.php](src/gettext-context.php) file for the list of functions and their parameters.

Extracting messages with context support
----------------------------------------

[](#extracting-messages-with-context-support)

### With xgettext

[](#with-xgettext)

You can add extra keyword parameters to your `xgettext` call to include the context functions. For example, this would be used in our package:

```
xgettext --force-po --keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 --keyword=dpgettext:2c,3 --keyword=dnpgettext:2c,3,4 -c -o messages.po tests/Unit/Test.php
```

### With Poedit

[](#with-poedit)

If you're using Poedit, add the following keywords in your Catalog &gt; Properties &gt; Sources Keywords:

- `pgettext:1c,2`
- `npgettext:1c,2,3`
- `dpgettext:2c,3`
- `dnpgettext:2c,3,4`

Then run the "Update from code" procedure :)

Contributing
------------

[](#contributing)

If you have some suggestions how to make this package better, please open an issue or even better, submit a pull request.

Should you want to contribute, please see the development guidelines in the [DataLinx PHP package template](https://github.com/DataLinx/php-package-template).

### Changelog

[](#changelog)

All notable changes to this project are automatically documented in the [CHANGELOG.md](CHANGELOG.md) file using the release workflow, based on the [release-please](https://github.com/googleapis/release-please) GitHub action.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

For all this to work, commit messages must follow the [Conventional commits](https://www.conventionalcommits.org/) specification, which is also enforced by a Git hook.

Credits
-------

[](#credits)

- [This answer](https://stackoverflow.com/questions/16260798/gettext-how-to-handle-homonyms/16263617#16263617) on Stack Overflow for the implementation.
- [This comment](https://www.php.net/manual/en/book.gettext.php#89975) on the PHP documentation for the messages extraction fix.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 97.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 ~427 days

Total

3

Last Release

1206d ago

Major Versions

v1.0.1 → v2.0.02023-01-28

PHP version history (3 changes)v1.0.0PHP &gt;=5.3

v1.0.1PHP &gt;=7.2

v2.0.0PHP &gt;=7.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/131700?v=4)[Omer Sabic](/maintainers/SlimDeluxe)[@SlimDeluxe](https://github.com/SlimDeluxe)

---

Top Contributors

[![SlimDeluxe](https://avatars.githubusercontent.com/u/131700?v=4)](https://github.com/SlimDeluxe "SlimDeluxe (45 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (1 commits)")

---

Tags

gettextphpgettextContextpgettext

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  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)[tio/laravel

Add this package to localize your Laravel application (PHP, JSON or GetText).

170318.5k](/packages/tio-laravel)[phpmyadmin/motranslator

Translation API for PHP using Gettext MO files

591.8M8](/packages/phpmyadmin-motranslator)[zerospam/laravel-gettext

Adds localization support to laravel applications in an easy way using Poedit and GNU gettext.

102207.4k](/packages/zerospam-laravel-gettext)[gettext/translator

Gettext translator functions

114.6M10](/packages/gettext-translator)

PHPackages © 2026

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