Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save igorw/1341428 to your computer and use it in GitHub Desktop.
Save igorw/1341428 to your computer and use it in GitHub Desktop.
Gist Sample to enable Twig + TwigTranslation bridge to Silex
<?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));
});
<?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