PHPackages                             dkesberg/slim-twig-translation-extension - 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. dkesberg/slim-twig-translation-extension

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

dkesberg/slim-twig-translation-extension
========================================

Twig function for Illuminate\\Translation\\Translator in Slim framework

v0.1.0(11y ago)61.0kMITPHPPHP &gt;=5.3.0

Since Jul 17Pushed 10y ago2 watchersCompare

[ Source](https://github.com/dkesberg/slim-twig-translation-extension)[ Packagist](https://packagist.org/packages/dkesberg/slim-twig-translation-extension)[ Docs](http://github.com/dkesberg/slim-twig-translation-extension)[ RSS](/packages/dkesberg-slim-twig-translation-extension/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

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

[](#slim-twig-translation-extension)

This repository provides a twig extension class for the twig view parser. The class adds a translate helper function for the use in twig templates. The translator function tries to call the trans() function of an Illuminate\\Translation\\Translator object in the slim container.

How to install
--------------

[](#how-to-install)

#### Using [Composer](http://getcomposer.org/)

[](#using-composer)

Create a composer.json file in your project root:

```
{
    "require": {
        "dkesberg/slim-twig-translation-extension": "v0.1.0"
    }
}
```

Then run the following composer command:

```
$ php composer.phar install
```

How to use
----------

[](#how-to-use)

### Slim

[](#slim)

Set up your twig views as described in the [SlimViews Repository](https://github.com/codeguy/Slim-Views). Add the extension to your parser extensions.

```
$view->parserExtensions = array(
    new \Dkesberg\Slim\Twig\Extension\TranslationExtension(),
);
```

### Twig template

[](#twig-template)

In your twig template you would write:

```
  {{ translate('mails.salutation.male') }}

```

You can also use the shorthand:

```
  {{ _('mails.salutation.male') }}

```

### Adding Illuminate/Translation/Translator to slim

[](#adding-illuminatetranslationtranslator-to-slim)

Simple injection:

```
use Illuminate\Translation\Translator;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Translation\FileLoader;

$translator = new Translator(new FileLoader(new Filesystem(), __DIR__ . '/lang'), 'en');
$translator->setFallback('en');
$app->translator = $translator;
```

Singleton ressource:

```
use Illuminate\Translation\Translator;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Translation\FileLoader;

$app->container->singleton('translator', function() {
  return new Translator(new FileLoader(new Filesystem(), __DIR__ . '/lang'), 'en');
});
$app->translator->setFallback('en');
```

Using slim hooks and singleton:

```
use Illuminate\Translation\Translator;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Translation\FileLoader;

// detect language and set translator
$app->hook('slim.before', function() use ($app) {
  $env = $app->environment();

  // Extract locale
  $locale = Locale::acceptFromHttp($env['HTTP_ACCEPT_LANGUAGE']);
  $app->locale = substr($locale,0,2);

  // Set translator instance
  $app->container->singleton('translator', function($app) {
    return new Translator(new FileLoader(new Filesystem(), __DIR__ . '/lang'), $app->locale);
  });
  $app->translator->setFallback('en');
});
```

### Language files

[](#language-files)

I am wrapping the Illuminate\\Translation\\Translator. The language files for the translator use the same structure used in the Laravel framework. For more information see also:

In the lang directory there should be a subdirectory for each language.

```
/lang
       /en
            mails.php
            validation.php
       /de
            mails.php
            validation.php

```

The language files simply return an array of keyed strings.

```
