PHP code example of dkesberg / slim-twig-translation-extension

1. Go to this page and download the library: Download dkesberg/slim-twig-translation-extension library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dkesberg / slim-twig-translation-extension example snippets


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

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;

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');

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');
});



return array(
  'salutation' => array(
    'male'    => 'Dear Mr.',
    'female'  => 'Dear Mrs.',
    'unknown' => 'Dear Sir or Madam'
  )
)

{{ _('mails.salutation.male') }}
bash
$ php composer.phar install

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