Skip to content

Instantly share code, notes, and snippets.

@klickreflex
Created April 7, 2014 14:20
Show Gist options
  • Save klickreflex/10021185 to your computer and use it in GitHub Desktop.
Save klickreflex/10021185 to your computer and use it in GitHub Desktop.
<?php
namespace Bolt\Controllers\Esperanto;
use Bolt\Application;
use Bolt\Controllers\Frontend as BoltFrontend;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class Controller
{
/**
* Enforce locale requirement
*/
public static function before(Request $request, Application $app)
{
BoltFrontend::before($request, $app);
$locales = $app['config']->get('general/i18n_locales', NULL);
if ($locales && $request->getLocale()) {
if (!in_array($request->getLocale(), $locales)) {
$app->abort(404, $app['translator']->trans('Unknown or unsupported locale.'));
}
}
}
/**
* Redirect / to /{default_locale}
*/
public static function i18nFront(Request $request, Application $app)
{
$locales = $app['config']->get('general/i18n_locales', NULL);
if ($locales) {
return $app->redirect($app['paths']['root'] . $request->getPreferredLanguage($locales));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment