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.
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
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(),
);
In your twig template you would write:
{{ translate('mails.salutation.male') }}
You can also use the shorthand:
{{ _('mails.salutation.male') }}
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');
});
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') }}