Skip to content

Instantly share code, notes, and snippets.

@peterlafferty
Last active November 15, 2017 20:48
Show Gist options
  • Save peterlafferty/f80a1baf4b23faee6e3854c4599e4639 to your computer and use it in GitHub Desktop.
Save peterlafferty/f80a1baf4b23faee6e3854c4599e4639 to your computer and use it in GitHub Desktop.
parsing Accept-Language and setting Content-Language headers in Silex
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$app = new Silex\Application();
$app->before(function (Request $request) use ($app) {
$locale = $request->getPreferredLanguage([
'ko', 'en', 'de', 'pt', 'pt_BR', 'fr', 'ja', 'zh', 'zh-Hans-CH',
]);
$app['locale'] = $locale;
$app['contentLanguage'] = str_replace('_', '-', $locale);
});
$app->after(function (Request $request, Response $response) use ($app) {
$response->headers->set('Content-Language', $app['contentLanguage']);
});
$app->get('/', function () use ($app) {
return new JsonResponse(
[
'locale' => $app['locale'],
'contentLanguage' => $app['contentLanguage']
]
);
});
$app->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment