Forked from mTorres/silex-php+twig+TwigBrige+translation.php
Created
November 5, 2011 11:53
-
-
Save igorw/1341428 to your computer and use it in GitHub Desktop.
Gist Sample to enable Twig + TwigTranslation bridge to Silex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/../lib/vendor/Silex/silex.phar'; | |
$app = new Silex\Application(); | |
$app['debug'] = true; | |
// Registering Symfony\Yaml and Symfony\Config | |
$app['autoloader']->registerNamespace('Symfony', __DIR__.'/../lib/vendor/symfony/src'); | |
// Registerying Extensions | |
// Twig for rendering | |
$app->register(new Silex\Provider\TwigServiceProvider(), array( | |
'twig.path' => __DIR__ . '/../lib/views', | |
'twig.class_path' => __DIR__ . '/../lib/vendor/Twig/lib', | |
'twig.options' => array('cache' => __DIR__ . '/../lib/twig.cache'), | |
)); | |
// Translator | |
$app->register(new Silex\Provider\TranslationServiceProvider(), array( | |
'locale_fallback' => 'en', | |
'translation.class_path' => __DIR__. '/../lib/vendor/symfony/src', | |
)); | |
// Messages | |
$app['translator.messages'] = array( | |
'en' => __DIR__.'/../lib/locales/en.yml', | |
'fr' => __DIR__.'/../lib/locales/fr.yml', | |
'de' => __DIR__.'/../lib/locales/de.yml', | |
); | |
// Enable translator Yaml Loader | |
$app['translator.loader'] = new Symfony\Component\Translation\Loader\YamlFileLoader(); | |
// Enable Twig translation extension (allows to use tag {% trans %} and filter | trans in templates) | |
$app['twig']->addExtension(new Symfony\Bridge\Twig\Extension\TranslationExtension($app['translator'])); | |
$app->before(function () use ($app) { | |
if ($locale = $app['request']->get('locale')) { | |
$app['locale'] = $locale; | |
} | |
}); | |
// Testing i18n | |
$app->get('/{locale}/{message}/{name}', function ($message, $name) use ($app) { | |
return $app['translator']->trans($message, array('%name%' => $name)); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/../lib/vendor/Silex/silex.phar'; | |
$app = new Silex\Application(); | |
$app['debug'] = true; | |
// Registering Symfony\Yaml and Symfony\Config | |
$app['autoloader']->registerNamespace('Symfony', __DIR__.'/../lib/vendor/symfony/src'); | |
// Registerying Extensions | |
// Twig for rendering | |
$app->register(new Silex\Provider\TwigServiceProvider(), array( | |
'twig.path' => __DIR__ . '/../lib/views', | |
'twig.class_path' => __DIR__ . '/../lib/vendor/Twig/lib', | |
'twig.options' => array('cache' => __DIR__ . '/../lib/twig.cache'), | |
)); | |
// Translator | |
$app->register(new Silex\Provider\TranslationServiceProvider(), array( | |
'locale_fallback' => 'en', | |
'translation.class_path' => __DIR__. '/../lib/vendor/symfony/src', | |
)); | |
// Messages | |
$app['translator.messages'] = array( | |
'en' => __DIR__.'/../lib/locales/en.yml', | |
'fr' => __DIR__.'/../lib/locales/fr.yml', | |
'de' => __DIR__.'/../lib/locales/de.yml', | |
); | |
// Enable translator Yaml Loader | |
$app['translator.loader'] = new Symfony\Component\Translation\Loader\YamlFileLoader(); | |
// Enable Twig translation extension (allows to use tag {% trans %} and filter | trans in templates) | |
$app['twig']->addExtension(new Symfony\Bridge\Twig\Extension\TranslationExtension($app['translator'])); | |
$app->before(function () use ($app) { | |
if ($locale = $app['request']->get('locale')) { | |
$app['locale'] = $locale; | |
} | |
}); | |
// Testing i18n | |
$app->get('/{locale}/{message}/{name}', function ($message, $name) use ($app) { | |
return $app['translator']->trans($message, array('%name%' => $name)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment