PHPackages                             cooperaj/better-twig-i18n - 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. cooperaj/better-twig-i18n

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

cooperaj/better-twig-i18n
=========================

Provides i18n functionality based on gettext language files.

v3.2.0(9mo ago)252.4k↓26.4%1mitPHPPHP ^8.1 || ^8.2 || ^8.3 || ^8.4CI passing

Since Aug 26Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/cooperaj/better-twig-i18n)[ Packagist](https://packagist.org/packages/cooperaj/better-twig-i18n)[ RSS](/packages/cooperaj-better-twig-i18n/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (6)Versions (10)Used By (0)

Better Twig Gettext i18n
========================

[](#better-twig-gettext-i18n)

[![Build](https://github.com/cooperaj/better-twig-i18n/workflows/Build/badge.svg)](https://github.com/cooperaj/better-twig-i18n/workflows/Build/badge.svg)[![codecov](https://camo.githubusercontent.com/ed921bc78a993a161191807577da9f39f9c4f090d2f08dc088d1a4bc0034bae3/68747470733a2f2f636f6465636f762e696f2f67682f636f6f706572616a2f6265747465722d747769672d6931386e2f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/cooperaj/better-twig-i18n)[![Psalm](https://camo.githubusercontent.com/6c2da0fca27075370d595131c72b7d593d5133bf359cb01cb62ffa5121872f82/68747470733a2f2f73686570686572642e6465762f6769746875622f636f6f706572616a2f6265747465722d747769672d6931386e2f636f7665726167652e737667)](https://shepherd.dev/github/cooperaj/better-twig-i18n)

Allows the use of a POT/PO/MO (gettext) set of language definitions with Twig, using an identical syntax to the current official [Twig translation extension](https://github.com/symfony/twig-bridge).

It supports full extraction of Twig templates into language catalogues from the [gettext/gettext](https://github.com/php-gettext/Gettext) library, which you can use to write out POT files if desired.

Additionally support for extraction of text values from PHP is possible when directly using the `translate` function from`\Acpr\I18n\Translator` in your code. For instance, you may be creating translated flash messages and storing them in your session to be used by subsequent twig templates.

### Supports

[](#supports)

#### Twig

[](#twig)

- Translation tags (`{% trans %} ... {% endtrans %}`) and filters (`| trans`)
- Variable interpolation `{% trans with { '%var%': var } %}%var%{% endtrans %}`
- Pluralisation `{% trans count 3 %}%count% item|%count% items{% endtrans %}`
- Message specific domains `{% trans from 'errors' %} ... {% endtrans %}`
- Message contexts `{% trans %} ... {% context %}Some context{% endtrans %}`
- Notes/comments for the translation `{% trans %} ... {% notes %}A translation note{% endtrans %}`
- or some horrid combination of all of them

    ```
    {% trans count 5 with { '%name%': 'Adam' } from 'errors' %}
      %name% has %count% apple|%name% has %count% apples
      {% notes %}A translation note
      {% context %}Some context to the translation
    {% endtrans %}
    ```

    The extraction of which would result in a `errors.pot` file that contains:

    ```
    #. A translation note
    msgctxt "Some context to the translation"
    msgid "%name% has %count% apple"
    msgid_plural "%name% has %count% apples"
    msgstr[0] ""

    ```

    And the (default, i.e. no language supplied) output of which would look like

    ```
    Adam has 5 apples

    ```

#### PHP

[](#php)

The PHP extraction works by parsing the text of your PHP files through [nikic/php-parser](https://github.com/nikic/PHP-Parser). This requires your PHP to be valid in order to work.

You could have a PHP file

```
/** @var $translator \Acpr\I18n\Translator **/
$pluralApples = $translator->translate(
    '%name% has %count% apple',
    [
        '%name' => 'Adam'
    ],
    'errors',
    'Some context to the translation',
    '%name% has %count% apples',
    5
);

// Assuming no translations had been loaded
// $pluralApples == 'Adam has 5 apples'
```

The extraction of which would result in a `errors.pot` file that contains:

```
msgctxt "Some context to the translation"
msgid "%name% has %count% apple"
msgid_plural "%name% has %count% apples"
msgstr[0] ""

```

##### Limitations

[](#limitations)

- The extraction specifically looks for usages of a `translate` function with the correct signature. This may result in false positives dependent on your code base.
- It is not currently possible to add notes/comments to a translation entry.
- For the correct values to be parsed, the string arguments to the `translate` function **must** be inlined strings (quoted or heredoc). It is **not** possible to use variables.

    ```
    // This will *not* work
    $var = 'I have an apple';
    $value = $translator->translate($var);

    // This will
    $value = $translator->translate('I have an apple');
    ```

Usage
-----

[](#usage)

> See [extract.php](example/extract.php) and [index.php](example/index.php) for example usage.

Upgrading
---------

[](#upgrading)

### Upgrading to v3.0.0 from prior versions

[](#upgrading-to-v300-from-prior-versions)

v3 introduces a new method of extracting keys that removes whitespace of more than 2 characters from the primary language keys ([see the release notes for more information](https://github.com/cooperaj/better-twig-i18n/releases/tag/v3.0.0))

There is a [script](./scripts/v3_po_update.php) available in the [scripts folder](./scripts) that will help to migrate any existing PO files you have to the newer keys and minimise the pain of poedit deciding everything is new rather than different. It will likely need editing to your requirements but is usage is simple.

```
$ cp scripts/v3_po_update.php folder/containing/po.files
$ cd folder/containing/po.files
# edit script as appropriate for your translation domains
$ php v3_po_update.php
```

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance58

Moderate activity, may be stable

Popularity33

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity78

Established project with proven stability

 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 ~227 days

Recently: every ~441 days

Total

9

Last Release

275d ago

Major Versions

v0.6.1 → v1.0.02020-09-01

v1.1.0 → v2.0.02022-10-24

v2.0.0 → v3.0.02023-07-07

PHP version history (2 changes)v2.0.0PHP ^8.1.0

v3.2.0PHP ^8.1 || ^8.2 || ^8.3 || ^8.4

### Community

Maintainers

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

---

Top Contributors

[![cooperaj](https://avatars.githubusercontent.com/u/400210?v=4)](https://github.com/cooperaj "cooperaj (116 commits)")

---

Tags

hactoberfestinternationalizationphptwig-extension

### Embed Badge

![Health badge](/badges/cooperaj-better-twig-i18n/health.svg)

```
[![Health](https://phpackages.com/badges/cooperaj-better-twig-i18n/health.svg)](https://phpackages.com/packages/cooperaj-better-twig-i18n)
```

###  Alternatives

[simplesamlphp/simplesamlphp

A PHP implementation of a SAML 2.0 service provider and identity provider.

1.1k12.4M193](/packages/simplesamlphp-simplesamlphp)[php-translation/extractor

Extract translations form the source code

1294.4M8](/packages/php-translation-extractor)[barryvdh/laravel-translation-manager

Manage Laravel Translations

1.7k3.6M17](/packages/barryvdh-laravel-translation-manager)[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.0k15.4k](/packages/prestashop-prestashop)[jms/translation-bundle

Puts the Symfony Translation Component on steroids

42510.8M63](/packages/jms-translation-bundle)[php-translation/symfony-bundle

3244.1M9](/packages/php-translation-symfony-bundle)

PHPackages © 2026

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