# Integration of I18n function in Plates templates

**URL:** https://discourse.laminas.dev/t/integration-of-i18n-function-in-plates-templates/407
**Category:** Mezzio
**Created:** [December 21, 2017, 3:56pm UTC](https://discourse.laminas.dev/t/integration-of-i18n-function-in-plates-templates/407 "2017-12-21T15:56:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jbelien](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jbelien/32/251_2.png) [@jbelien](https://discourse.laminas.dev/u/jbelien)
#### Post date: [December 21, 2017, 3:56pm UTC](https://discourse.laminas.dev/t/integration-of-i18n-function-in-plates-templates/407/1 "2017-12-21T15:56:19Z")

</div>

Hello everyone,

I followed this _cookbook_ ([https://docs.zendframework.com/zend-expressive/cookbook/setting-locale-without-routing-parameter/](https://docs.zendframework.com/zend-expressive/cookbook/setting-locale-without-routing-parameter/)) and it works just fine to define the locale !

But I’m struggling to use the I18n functions inside my _Plates_ templates. Now my locale is defined correctly, I have absolutely no idea how to translate the strings that are in my templates.

I tried to find documentation about that integration but couldn’t find any…  
Can someone help me with that ?

Thanks a lot ! 🙂

---

<div class="post-metadata">

### Author: ![jbelien](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jbelien/32/251_2.png) [@jbelien](https://discourse.laminas.dev/u/jbelien)
#### Post date: [January 7, 2018, 12:37pm UTC](https://discourse.laminas.dev/t/integration-of-i18n-function-in-plates-templates/407/2 "2018-01-07T12:37:35Z")

</div>

Anyone has a suggestion on how do this ?

Thanks 🙂

---

<div class="post-metadata">

### Author: ![jbelien](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/jbelien/32/251_2.png) [@jbelien](https://discourse.laminas.dev/u/jbelien)
#### Post date: [January 18, 2018, 10:31am UTC](https://discourse.laminas.dev/t/integration-of-i18n-function-in-plates-templates/407/3 "2018-01-18T10:31:54Z")

</div>

Here is my solution :

# Middleware

```auto
<?php

namespace App\Middleware;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Locale;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class LocalizationMiddleware implements MiddlewareInterface
{
  public const LOCALIZATION_ATTRIBUTE = 'locale';

  public function process(ServerRequestInterface $request, DelegateInterface $delegate) : ResponseInterface
  {
    $cookies = $request->getCookieParams();
    $query = $request->getQueryParams();
    $server = $request->getServerParams();

    if (isset($query['lang']) && preg_match('/^(?P<locale>[a-z]{2,3}([-_][a-zA-Z]{2}|))$/', $query['lang'])) {
      Locale::setDefault(Locale::canonicalize($query['lang']));

      if (isset($cookies['lang'])) {
        setcookie('lang', FALSE, NULL, NULL, NULL, (!empty($server['HTTPS'])), FALSE);
      }
      setcookie('lang', Locale::getDefault(), NULL, NULL, NULL, (!empty($server['HTTPS'])), FALSE);
    } else if (isset($cookies['lang'])) {
      Locale::setDefault(Locale::canonicalize($cookies['lang']));
    } else if (isset($server['HTTP_ACCEPT_LANGUAGE']) ) {
      $locale = Locale::acceptFromHttp($server['HTTP_ACCEPT_LANGUAGE']);
      Locale::setDefault(Locale::canonicalize($locale));
    } else {
      Locale::setDefault('en_US');
    }

    return $delegate->process($request->withAttribute(self::LOCALIZATION_ATTRIBUTE, Locale::getDefault()));
  }
}

```

# Extension

```auto
<?php

namespace App\Extension;

use League\Plates\Engine;
use League\Plates\Extension\ExtensionInterface;
use Zend\I18n\Translator\Translator;

class TranslateExtension implements ExtensionInterface
{
  private $translator;

  public function __construct(Translator $translator) {
    $this->translator = $translator;
  }

  public function register(Engine $engine)
  {
    $engine->registerFunction('translate', [$this, 'generateTranslate']);
  }

  public function generateTranslate(string $var) : string
  {
    return $this->translator->translate($var, 'my-application');
  }
}

```

# Extension factory

```auto
<?php

namespace App\Extension\Factory;

use App\Extension\TranslateExtension;
use App\Middleware\LocalizationMiddleware;
use Psr\Container\ContainerInterface;
use Zend\I18n\Translator\Translator;

class TranslateFactory
{
  public function __invoke(ContainerInterface $container)
  {
    $translator = new Translator();
    $translator->addTranslationFilePattern('gettext', '../locale', '%s/my-application.mo', 'my-application');

    return new TranslateExtension($translator);
  }
}

```

# Configuration

Edit `config/templates.global.php` :

```auto
<?php

use Zend\Expressive\Plates\PlatesRendererFactory;
use Zend\Expressive\Template\TemplateRendererInterface;

return [
  'dependencies' => [
    'factories' => [
      TemplateRendererInterface::class => PlatesRendererFactory::class,
      App\Extension\TranslateExtension::class => App\Extension\Factory\TranslateFactory::class,
    ],
  ],

  'templates' => [
    'extension' => 'phtml',
  ],

  'plates' => [
    'extensions' => [
      App\Extension\TranslateExtension::class,
    ],
  ]
];

```

Add the middleware in `config/pipelines.php` :

```auto
$app->pipe(App\Middleware\LocalizationMiddleware::class);

```

You can now use `$this->translate()` in your _Plates_ templates 🎉
