Last active
April 26, 2021 14:09
-
-
Save reynadan/3d1fa5a9eb0ae310f7f207ce8e6dbcf5 to your computer and use it in GitHub Desktop.
Change locales symfony 5.2
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
{% set route = app.request.attributes.get('_route') %} | |
{% set route_params = app.request.attributes.get('_route_params') %} | |
{% set params = route_params|merge(app.request.query.all) %} | |
<a class="dropdown-toggle" href="#"> | |
<img src="{{ asset('build/media/images/flags/' ~ app.request.locale ~ '.png') }}" alt="{{ app.request.locale }}" /> | |
</a> | |
<div class="dropdown"> | |
{% for locale in locales %} | |
<a class="dropdown-item" href="{{ path(route, params|merge({ _locale: locale })) }}"> | |
<img src="{{ asset('build/media/images/flags/' ~ locale ~ '.png') }}" alt="{{ locale }}" width="20" > | |
<span>{{ locale }}</span> | |
</a> | |
{% endfor %} | |
</div> |
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 | |
namespace App\Core\Locale; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Session\SessionInterface; | |
class LocaleStorage | |
{ | |
const DEFAULT_LANGUAGE = 'fr'; | |
const FALLBACK_LANGUAGE = 'en'; | |
const LANGUAGES = [self::DEFAULT_LANGUAGE, self::FALLBACK_LANGUAGE]; | |
const SESSION_STORAGE_KEY = '_locale'; | |
/** | |
* @var SessionInterface | |
*/ | |
private $session; | |
/** | |
* LocaleStorage constructor. | |
* | |
* @param SessionInterface $session | |
*/ | |
public function __construct(SessionInterface $session) | |
{ | |
$this->session = $session; | |
} | |
/** | |
* @return string | |
*/ | |
public function get() | |
{ | |
return $this->session->get(static::SESSION_STORAGE_KEY, static::DEFAULT_LANGUAGE); | |
} | |
/** | |
* @param string $locale | |
*/ | |
public function set(string $locale) | |
{ | |
if (!\in_array($locale, static::LANGUAGES)) { | |
$locale = static::FALLBACK_LANGUAGE; | |
} | |
$this->session->set(static::SESSION_STORAGE_KEY, $locale); | |
} | |
/** | |
* @param Request $request | |
*/ | |
public function setFromPreferred(Request $request) | |
{ | |
if ($this->session->has(static::SESSION_STORAGE_KEY)) { | |
return; | |
} | |
$preferred = $request->getPreferredLanguage(static::LANGUAGES); | |
$this->set($preferred); | |
} | |
} |
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 | |
namespace App\Core\Locale; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
use Symfony\Component\HttpKernel\Event\KernelEvent; | |
use Symfony\Component\HttpKernel\KernelEvents; | |
class RequestLocaleListener implements EventSubscriberInterface | |
{ | |
/** | |
* @var LocaleStorage | |
*/ | |
private $storage; | |
/** | |
* RequestLocaleListener constructor. | |
* | |
* @param LocaleStorage $storage | |
*/ | |
public function __construct(LocaleStorage $storage) | |
{ | |
$this->storage = $storage; | |
} | |
/** | |
* @param KernelEvent $event | |
*/ | |
public function onKernelRequest(KernelEvent $event) | |
{ | |
$request = $event->getRequest(); | |
if (!$request->hasPreviousSession()) { | |
return; | |
} | |
$locale = $request->attributes->get('_locale', $request->query->get('_locale')); | |
if (null !== $locale) { | |
$this->storage->set($locale); | |
$request->setLocale($locale); | |
return; | |
} | |
$this->storage->setFromPreferred($request); | |
$request->setLocale($this->storage->get()); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function getSubscribedEvents() | |
{ | |
return [ | |
KernelEvents::REQUEST => [['onKernelRequest', 20]], | |
]; | |
} | |
} |
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
framework: | |
default_locale: !php/const App\Core\Locale\LocaleStorage::DEFAULT_LANGUAGE | |
translator: | |
default_path: '%kernel.project_dir%/translations' | |
fallbacks: | |
- !php/const App\Core\Locale\LocaleStorage::FALLBACK_LANGUAGE |
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
twig: | |
globals: | |
locales: !php/const App\Core\Locale\LocaleStorage::LANGUAGES |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment