PHPackages                             darkalchemy/twig-translate - 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. darkalchemy/twig-translate

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

darkalchemy/twig-translate
==========================

A Twig Translation Extension

v0.3.5(4y ago)0326[1 PRs](https://github.com/darkalchemy/Twig-Translate/pulls)1MITPHPPHP ^7.4 || ^8.0

Since Feb 23Pushed 3y ago1 watchersCompare

[ Source](https://github.com/darkalchemy/Twig-Translate)[ Packagist](https://packagist.org/packages/darkalchemy/twig-translate)[ Docs](https://github.com/darkalchemy/twig-translate)[ RSS](/packages/darkalchemy-twig-translate/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (10)Dependencies (9)Versions (19)Used By (1)

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

[](#twig-translation-extension)

[![Codacy Badge](https://camo.githubusercontent.com/d48111c83f7e8ed8b8c215fade3c2fd07852b2602a052333f92330bfb4b1d0dc/68747470733a2f2f6170702e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f6635363964343337633533353462323162316130393461383135353232373761)](https://www.codacy.com/gh/darkalchemy/Twig-Translate/dashboard?utm_source=github.com&utm_medium=referral&utm_content=darkalchemy/Twig-Translate&utm_campaign=Badge_Grade)

A Twig Translation Extension.
This twig extension has been forked from [Odan/Twig-Translation](https://github.com/odan/twig-translation) because I wanted more flexibility with available methods for translating.
So, this extension uses [delight-im/PHP-I18N](https://github.com/delight-im/PHP-I18N) to handle the translations.

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

[](#requirements)

- PHP 7.4 or PHP 8.0

Installation - These instructions assume using php-di for the dependency injection
----------------------------------------------------------------------------------

[](#installation---these-instructions-assume-using-php-di-for-the-dependency-injection)

```
composer require darkalchemy/twig-translate

```

Integration
-----------

[](#integration)

### Register the Twig Extension and, the locale codes that will be available

[](#register-the-twig-extension-and-the-locale-codes-that-will-be-available)

#### This makes the functions available for all strings that are in twig templates

[](#this-makes-the-functions-available-for-all-strings-that-are-in-twig-templates)

In your container

```
\Delight\I18n\I18n::class => DI\factory(function () {
    return new \Delight\I18n\I18n([
        \Delight\I18n\Codes::EN_US,
        \Delight\I18n\Codes::FR_FR,
    ]);
}),

\Slim\Views\Twig::class => function (\Psr\Container\ContainerInterface $container) {
    $settings = $container->get(Configuration::class)->all();
    $twig = \Slim\Views\Twig::create($settings['twig']['path'], [
        'cache' => $settings['twig']['cache'] ?? false,
    ]);
    $twig->addExtension(new \darkalchemy\Twig\TwigTranslationExtension($container->get(\Delight\I18n\I18n::class)));

    return $twig;
}

```

### Add an $i18n into app.php, so that it can be called by global in helpers.php

[](#add-an-i18n-into-appphp-so-that-it-can-be-called-by-global-in-helpersphp)

```
$i18n = $container->get(\Delight\I18n\I18n::class);

```

### Add the functions, in a file like helpers.php. This makes the functions available for all strings not in twig templates

[](#add-the-functions-in-a-file-like-helpersphp-this-makes-the-functions-available-for-all-strings-not-in-twig-templates)

```
function compile_twig_templates(\Psr\Container\ContainerInterface $container)
{
    $settings    = $container->get(\Selective\Config\Configuration::class)->all();
    $twig_config = $settings['twig'];
    $cache       = $twig_config['cache'] ?? $settings['root'] . '/resources/views/cache/';
    $twig        = $container->get(Twig::class)->getEnvironment();
    $compiler = new \darkalchemy\Twig\TwigCompiler($twig, $cache);

    try {
        $compiler->compile();
    } catch (Exception $e) {
        die($e->getMessage());
    }

    echo "\nCompiling twig templates completed\n\n";
    echo "to fix the permissions, you should run:\nsudo chown -R www-data:www-data {$cache}\nsudo chmod -R 0775 {$cache}\n";

    return 0;
}

function __f(string $text, ...$replacements)
{
    global $i18n;

    return $i18n->translateFormatted($text, ...$replacements);
}

function __fe(string $text, ...$replacements)
{
    global $i18n;

    return $i18n->translateFormattedExtended($text, ...$replacements);
}

function __p(string $text, string $alternative, int $count)
{
    global $i18n;

    return $i18n->translatePlural($text, $alternative, $count);
}

function __pf(string $text, string $alternative, int $count, ...$replacements)
{
    global $i18n;

    return $i18n->translatePluralFormatted($text, $alternative, $count, ...$replacements);
}

function __pfe(string $text, string $alternative, int $count, ...$replacements)
{
    global $i18n;

    return $i18n->translatePluralFormattedExtended($text, $alternative, $count, ...$replacements);
}

function __c(string $text, string $context)
{
    global $i18n;

    return $i18n->translateWithContext($text, $context);
}

function __m(string $text)
{
    global $i18n;

    return $i18n->markForTranslation($text);
}

```

### Copy i18n.sh from I18n to the bin directory

[](#copy-i18nsh-from-i18n-to-the-bin-directory)

```
cp vendor/delight-im/i18n/i18n.sh bin/
chmod a+x bin/i18n.sh

```

### In order to translate your twig templates, you first need to compile the templates. Create a file bin/translate.php and add this to it

[](#in-order-to-translate-your-twig-templates-you-first-need-to-compile-the-templates-create-a-file-bintranslatephp-and-add-this-to-it)

```
