dkesberg / slim-twig-translation-extension
Twig function for Illuminate\Translation\Translator in Slim framework
Installs: 1 019
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.3.0
- illuminate/translation: >=4.0
- slim/slim: >=2.3.0
- slim/views: >=0.1.2
- twig/twig: >=1.16.0
This package is not auto-updated.
Last update: 2025-03-25 03:35:07 UTC
README
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
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
Slim
Set up your twig views as described in the SlimViews Repository. Add the extension to your parser extensions.
$view->parserExtensions = array( new \Dkesberg\Slim\Twig\Extension\TranslationExtension(), );
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
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
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: http://laravel.com/docs/4.2/localization
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.
<?php return array( 'salutation' => array( 'male' => 'Dear Mr.', 'female' => 'Dear Mrs.', 'unknown' => 'Dear Sir or Madam' ) )
The first part of the string passed to the translate function is the name of the language file, the second part is the key you want to retrieve. The second part uses the dot notation for multidimensional arrays.
{{ _('mails.salutation.male') }}