Stop Symfony from overwriting the localefrom the query string when set_locale_from_accept_language is true#3397
Closed
Stop Symfony from overwriting the localefrom the query string when set_locale_from_accept_language is true#3397
Conversation
…t_locale_from_accept_language is true
Contributor
Author
|
In case that MR is not accepted for some reason (in its current form or another), the issue it fixes can also be fixed by creating a subscriber in the App namespace: <?php
declare(strict_types=1);
namespace App\Event\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class FixLocaleSubscriber implements EventSubscriberInterface
{
public function onKernelRequest(RequestEvent $event): void
{
$request = $event->getRequest();
/**
* Symfony's listener has a lower priority and will be called after Bolt's listener.
* It will not check that the locale was set already and in case useAcceptLanguageHeader is
* set, it will overwrite the locale just set by Bolt (since it does not care about the
* query, only the attributes).
*
* This listener registers the locale as an attribute (if not already the case) to ensure
* the _locale query parameter has a greater weight than the HTTP_ACCEPT_LANGUAGE header.
* It does not set the locale: either Bolt did it or Symfony will.
*/
if (!$request->attributes->has('_locale') && $request->query->has('_locale')) {
$locale = $request->query->get('_locale');
$request->attributes->set('_locale', $locale);
}
}
public static function getSubscribedEvents(): array
{
return [
// must be registered before (i.e. with a higher priority than) the default Bolt's Locale listener
KernelEvents::REQUEST => [['onKernelRequest', 25]],
];
}
} |
|
In an effort to keep our pull request list manageable, we are marking this PR as stale because it has been 60 days without activity. If this PR is still relevant, please let us know by posting a quick comment or update so that it can be assigned and hopefully merged. |
Contributor
Author
|
Why wouldn't it be relevant anymore? |
Member
|
It is not recommended to have the locale in a query string, which is also why Symfony doesn't use it by default. So, in its current form it will not be merged, but you can still use your own subscriber for your specific use case. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi!
Context
A bit of context to begin with: I am using Bolt with 3 languages, each page being translated in each of this language.
Some of the visitors do not speak the default language and won't take the time use the language switcher.
I want the pages do be displayed in the language the user chose in its browser configuration.
That's what symfony provides through https://symfony.com/doc/current/reference/configuration/framework.html#set-locale-from-accept-language.
However I'd still like to be able to change the locale, through the language switcher, which simply appends
?_locale=XXto the query string.Issue
When
framework.set_locale_from_accept_language: trueis set (with `framework.enabled_locales: ['en', 'fr', 'it']) the preferred language overwrites (Symfony's subscriber is called after Bolt's subscriber) the language set in the query string: symfony only checks the attributes, not the query string.Solution
When the
_localeparameter from the query string is set, also set it in the attributes, which Symfony will check.I do not know what that might imply (besides fixing my issue). This pull request is kind of here for discussions if that is not the way things should be done (I might not know Symfony well enough).
See https://github.com/symfony/symfony/blob/5.4/src/Symfony/Component/HttpKernel/EventListener/LocaleListener.php#L71