Last active
December 10, 2015 14:09
-
-
Save miguelramos/4446058 to your computer and use it in GitHub Desktop.
Umpirsky Silex I18nRouting
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 | |
/** | |
* This is the application file that extends from silex application. | |
* | |
*/ | |
public function __construct(Config $config) | |
{ | |
parent::__construct(); | |
/** | |
* Register translator.domains outside register service was no effect. It only appears on translator | |
* array if it is register here, in the new instance. | |
*/ | |
$this->register(new TranslationServiceProvider(),array( | |
'locale' => 'pt', | |
'locale_fallback' => 'en', | |
/*'translator.domains' => array( | |
'routes' => array( | |
'pt' => array( | |
'/pt' => '/' | |
), | |
'en' => array( | |
'/en' => '' | |
) | |
) | |
)*/ | |
)); | |
$this['translator'] = $this->share($app->extend('translator', function($translator, $app) { | |
$translator->addLoader('yaml', new YamlFileLoader()); | |
$translator->addResource('yaml', $app['root'].'/locales/en.yml', 'en'); | |
$translator->addResource('yaml', $app['root'].'/locales/pt.yml', 'pt'); | |
return $translator; | |
})); | |
//Service Umpirsky\Silex\I18nRouting | |
$this->register(new RouteServiceProvider()); | |
//Before | |
$this->before(function($config) use ($app){ | |
\ChromePhp::log($app['request']->get('locale')); | |
}); | |
} | |
public function mount($prefix, $app) | |
{ | |
parent::mount( | |
$this['translator']->trans( | |
$prefix, | |
array(), | |
isset($this['translator.domains.route']) ? $this['translator.domains.route'] : 'routes' | |
), | |
$app | |
); | |
} | |
/** | |
* This is the routes.php file, here i mount all controllers. As you can see | |
* i have one mount with prefix language. | |
*/ | |
$app->mount('/pt', new Website\Controller\MainController()); | |
/** | |
* This is my controller file where all magic happens. On msg you will see | |
* that i'm calling a translation from file that previous register. | |
* | |
*/ | |
public function connect(Application $app) | |
{ | |
$route = $app['controllers_factory']; | |
$route->get('/', function(Request $request) use ($app) { | |
$content = $app['twig']->render('index.twig', array( | |
'title' => 'Homepage', | |
'msg' => $app['translator']->trans('hello'), | |
'css' => $app['url_short']->short('css-'.date('Ymd')).'.css', | |
'js' => $app['url_short']->short('js-'.date('Ymd')).'.js', | |
'debuger' => $app['translator'] | |
)); | |
$response = new Response($content, 200, array( | |
'Cache-Control' => 's-maxage=3600' | |
)); | |
if ('gzip' === substr($request->headers->get('Accept-Encoding'), 0, 4)) { | |
$response->setContent(gzencode($response->getContent())); | |
$response->headers->set('Content-Encoding', 'gzip'); | |
} | |
return $response; | |
})->bind('homepage'); | |
return $route; | |
} | |
/** | |
* Translation file. Only one atribute to test | |
* | |
*/ | |
hello: Olá zé |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment