PHPackages                             odan/twig-translation - 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. odan/twig-translation

Abandoned → [symfony/twig-bridge](/?search=symfony%2Ftwig-bridge)ArchivedLibrary[Templating &amp; Views](/categories/templating)

odan/twig-translation
=====================

A Twig Translation Extension

3.1.0(5y ago)1413.9k32MITPHPPHP ^7.3 || ^8.0

Since Nov 26Pushed 4y ago2 watchersCompare

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

READMEChangelog (10)Dependencies (10)Versions (20)Used By (2)

Twig Translation Extension
==========================

[](#twig-translation-extension)

A Twig Translation Extension.

[![Latest Version on Packagist](https://camo.githubusercontent.com/b7e0b5486a69f1f37b5545b7c81973fb6398535bef92521dc53db6b4bc3bf4a7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6f64616e2f747769672d7472616e736c6174696f6e2e737667)](https://github.com/odan/twig-translation/releases)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)[![Build Status](https://github.com/odan/twig-translation/workflows/build/badge.svg)](https://github.com/odan/twig-translation/actions)[![Code Coverage](https://camo.githubusercontent.com/68281eb7f3d329ede634b00ce93a29fb5aa54e9c9bc6af315056e0a20f9b6c4c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6f64616e2f747769672d7472616e736c6174696f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/odan/twig-translation/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/4f0092f5250067f3fe6cbf22efd335919ab8346bdb042d4daadb0cbb01c107f9/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6f64616e2f747769672d7472616e736c6174696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/odan/twig-translation/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/ae0350e0ddc08a0972931d044d6d41599867871c2cc479d522990e5fbee64c89/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f64616e2f747769672d7472616e736c6174696f6e2e737667)](https://packagist.org/packages/odan/twig-translation)

**Please read this first!**

The [symfony/twig-bridge](https://github.com/symfony/twig-bridge) also provides a Twig 3 [TranslationExtension](https://github.com/symfony/twig-bridge/blob/5.x/Extension/TranslationExtension.php)to translate messages with the [trans](https://symfony.com/doc/current/translation.html#translations-in-templates) filter. For this reason the `odan/twig-translation` component is just redundant and will be deprecated in the near future. I strongly recommend you to use the `symfony/twig-bridge` TranslationExtension instead.

Here you can find an installation guide:

-

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

[](#requirements)

- PHP 7.3+ or 8.0+

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

[](#installation)

```
composer require odan/twig-translation

```

### Registering the extension

[](#registering-the-extension)

This example uses the [symfony/translation](https://github.com/symfony/translation) component:

```
composer require symfony/translation

```

Register the Twig Extension:

```
$loader = new \Twig\Loader\FilesystemLoader('/path/to/templates');
$twig = new \Twig\Environment($loader, array(
    'cache' => '/path/to/twig-cache',
));

$translator = new \Symfony\Component\Translation\Translator(
    'en_US',
    new MessageFormatter(new IdentityTranslator()),
    null
);

$translator->addLoader('mo', new MoFileLoader());

$twig->addExtension(new \Odan\Twig\TwigTranslationExtension($translator));
```

Slim 4 integration
------------------

[](#slim-4-integration)

To install the [symfony/translation](https://github.com/symfony/translation) component, run:

```
composer require symfony/translation

```

Add settings:

```
// Locale settings
$settings['locale'] = [
    'path' => '/path/to/resources/locale',
    'cache' => '/path/to/locale-cache',
    'locale' => 'en_US',
    'domain' => 'messages',
    // Should be set to false in production
    'debug' => false,
];
```

Add a new container definition:

```
