dev-master
9999999-devA stack middleware for detecting URI locales.
MIT
The Requires
- php >=5.3.0
- symfony/http-foundation ~2.1
- symfony/http-kernel ~2.1
The Development Requires
by Martin Bastien
middleware stack locale localization
Wallogit.com
2017 © Pedro Peláez
A stack middleware for detecting URI locales.
This stack middleware gives localization features to your application. It will start looking for the locale in the URL. If none is present, it will default to the one you provide., (*1)
No default will result in getting the HTTP_ACCEPT_LANGUAGE from the user and try to match it to an available locale., (*2)
This middleware can detect language specified on a URI prefix. ex.:, (*3)
http://mydomain.com/en http://mydomain.com/fr
The locale will be accessible on the Request getLocale() method and be removed from the request pathinfo for easier routing., (*4)
<?php
require_once __DIR__.'/../vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
$app = new Silex\Application();
// Accessible on /{locale}/
$app->get('/', function (Request $request) {
// The locale should be set.
return $request->getLocale();
});
// Accessible on /{locale}/test
$app->get('/test', function (Request $request) {
// The locale should be set.
return $request->getLocale().'/test';
});
$locales = ['en', 'fr', 'es'];
$fallback = 'en';
$redirectDefault = true;
$stack = new Stack\Builder();
$stack->push('Frenzy\Localize\StackLocalize', $locales, $fallback, $redirectDefault);
$app = $stack->resolve($app);
$request = Request::createFromGlobals();
$response = $app->handle($request)->send();
$app->terminate($request, $response);
<?php
$params = [
'locales' => ['fr', 'en', 'es'],
'fallback' => 'fr',
'redirectDefault' => true,
];
App::middleware('Frenzy\Localize\StackLocalize', $params);
// Be sure to set the locale from the request when booting your app.
App::setLocale(Request::getLocale());
A stack middleware for detecting URI locales.
MIT
middleware stack locale localization