Last active
November 15, 2017 20:48
-
-
Save peterlafferty/f80a1baf4b23faee6e3854c4599e4639 to your computer and use it in GitHub Desktop.
parsing Accept-Language and setting Content-Language headers in Silex
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 | |
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