PHPackages                             simplesamlphp/twig-configurable-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. [Templating &amp; Views](/categories/templating)
4. /
5. simplesamlphp/twig-configurable-i18n

AbandonedArchivedProject[Templating &amp; Views](/categories/templating)

simplesamlphp/twig-configurable-i18n
====================================

This is an extension on top of Twig's i18n extension, allowing you to customize which functions to use for translations.

v2.3.5(3y ago)36.0M—0.3%23LGPL-2.1PHPPHP &gt;=7.1

Since Aug 25Pushed 3y agoCompare

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

READMEChangelog (1)Dependencies (6)Versions (17)Used By (3)

Configurable Internationalization with Twig
===========================================

[](#configurable-internationalization-with-twig)

This is a hack to get Twig's native internationalization extension to work with other translation systems than PHP's native *gettext*extension. That way, you can use other implementation, such as [gettext/gettext](https://github.com/oscarotero/Gettext) or even your own one, provided a function is available to perform translations for singular strings, and another one for plural strings.

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

[](#installation)

If you are using composer, installation is as easy as requiring this package:

```
composer require jaimeperez/twig-configurable-i18n

```

If you are not using composer, you will need to manually include all (four) source files in this project from your script, and make sure you have the `twig/extension` package installed as well.

Usage
-----

[](#usage)

Using this extension is extremely simple, and won't require you to modify your code or existing templates, but just a couple of modifications to the PHP scripts where you want internationalization.

First, **create aliases** for two classes so that the ones defined in this extension override the default ones:

```
use SimpleSAML\TwigConfigurableI18n\Twig\Environment as Twig_Environment;
use SimpleSAML\TwigConfigurableI18n\Twig\Extensions\Extension\I18n as Twig_Extensions_Extension_I18n;
```

The first line allows you to redefine the twig environment class, so that you can keep the configuration options you pass to the constructor. The second line installs this extension instead of the native one. Your code will use now this extension without further modifications, and PHP's native gettext will continue to be used.

Now, you are ready to configure the extension to use different translation functions. You will do that by passing options to the Twig environment class you have just imported:

```
$twig = new Twig_Environment($loader, array(
    'translation_function' => 'translate',
    'translation_function_plural' => 'translate_plural'
));
```

These are the only two supported options. `translation_function` lets you specify a function that is capable to translate strings in singular, being equivalent to `gettext()`. On the other hand, `translate_function_plural` lets you specify a function to handle the translation of strings in plural. This one is equivalent to `ngettext()`.

You are all set. Twig will use the `translate()` and `translate_plural()` functions you have just specified, instead of the native `gettext()` and `ngettext()` functions. Of course, you will need to specify some functions that actually **exist**, but that depends on what implementation of gettext you want to use. If, for example, you plan to use Oscar Otero's pure-PHP implementation [gettext/gettext](https://github.com/oscarotero/Gettext), you can do the following:

```
use \Gettext\Translator as Translator;

...

$this->translator = new Translator();
$this->translator->register();

...

$twig = new Twig_Environment($loader, array(
    'translation_function' => '__',
    'translation_function_plural' => 'n__'
));
```

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity47

Moderate usage in the ecosystem

Community18

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 54.1% 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 ~190 days

Recently: every ~206 days

Total

13

Last Release

1267d ago

Major Versions

v1.2 → v2.02018-10-10

### Community

Maintainers

![](https://www.gravatar.com/avatar/579a16ea8afccc6ab420c393f0e573296fe46dcd293aa4f3b71eefc158ebf3e4?d=identicon)[jaime](/maintainers/jaime)

![](https://www.gravatar.com/avatar/9221e348303eeda74e85236a8bff9b919a90d10e3a478fe1cbb9d833f68d0150?d=identicon)[thijskh](/maintainers/thijskh)

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

---

Top Contributors

[![jaimeperez](https://avatars.githubusercontent.com/u/1942728?v=4)](https://github.com/jaimeperez "jaimeperez (20 commits)")[![tvdijen](https://avatars.githubusercontent.com/u/841045?v=4)](https://github.com/tvdijen "tvdijen (17 commits)")

---

Tags

internationalizationi18ntwigtranslationgettextextension

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/simplesamlphp-twig-configurable-i18n/health.svg)

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

###  Alternatives

[jblond/twig-trans

Twig 3 translation extension

13169.9k2](/packages/jblond-twig-trans)[delight-im/i18n

Internationalization and localization for PHP

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

PHPackages © 2026

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