Commit 7a46007a authored by Emma's avatar Emma 🏳🌈

select locale based on Accept-Language header

parent d3d8b221
Pipeline #57705650 passed with stage
in 1 minute and 2 seconds
......@@ -48,6 +48,8 @@ services:
$serializer: "@serializer"
App\EventListener\LocaleListener:
arguments:
$availableLocales: "%env(csv:AVAILABLE_LOCALES)%"
tags:
- { name: kernel.event_listener, event: kernel.request, priority: 20 }
- { name: kernel.event_listener, event: security.interactive_login, method: onInteractiveLogin }
......
......@@ -129,7 +129,7 @@ final class UserController extends AbstractController {
}
$data = new UserData();
$data->setLocale($this->defaultLocale);
$data->setLocale($request->getLocale());
$form = $this->createForm(UserType::class, $data);
$form->handleRequest($request);
......
......@@ -30,10 +30,16 @@ final class LocaleListener {
*/
private $translator;
/**
* @var string[]
*/
private $availableLocales;
public function __construct(
SessionInterface $session,
TokenStorageInterface $tokenStorage,
TranslatorInterface $translator
TranslatorInterface $translator,
array $availableLocales
) {
if (!$translator instanceof LocaleAwareInterface) {
throw new \InvalidArgumentException(
......@@ -44,18 +50,21 @@ final class LocaleListener {
$this->session = $session;
$this->tokenStorage = $tokenStorage;
$this->translator = $translator;
$this->availableLocales = $availableLocales;
}
public function onKernelRequest(GetResponseEvent $event) {
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
if ($request->hasPreviousSession()) {
$locale = $this->session->get('_locale');
}
$locale = $request->getSession()->get('_locale');
if (!isset($locale)) {
$locale = $request->getPreferredLanguage($this->availableLocales);
}
if ($locale) {
if (isset($locale)) {
$request->setLocale($locale);
}
}
......
......@@ -481,7 +481,7 @@ submissions:
vote_stats: (+%up%, −%down%)
ip_ban: IP-forbaro
edit_info: (redaktis %edited_at%)
moderator_info: (kontrolanto redaktis %edit_at%)
moderator_info: (kontrolanto redaktis %edited_at%)
new_comments: '{1} (%count% nova)|[1,Inf[ (%count% novaj)'
time:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment