Search code examples
localesymfony4

in synfony4 How to force locale 'en' without taking the prefered language


in synfony4 i took the symfony demo app, and o want to know How can I force defaultlocale to 'en' to my homepage without taking the prefered language of the browser 'fr'. I just want to keed the choice to change the language manually only

SERVICE.yaml

parameters:
    locale: 'en'
    # This parameter defines the codes of the locales (languages) enabled in the application
    app_locales: en|fr|de|es|cs|nl|ru|uk|ro|pt_BR|pl|it|ja|id|ca|sl|hr|zh_CN|bg|tr|lt
    app.notifications.email_sender: [email protected]

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        bind:               # defines the scalar arguments once and apply them to any service defined/created in this file
            $locales: '%app_locales%'
            $defaultLocale: 'en'
            $emailSender: '%app.notifications.email_sender%'

FRAMEWORK.yaml

framework:
    default_locale: 'en'

I expected the first redirection will be 127.0.0.1:8000/en or 127.0.0.1:8000

But the actual output is 127.0.0.1:8000/fr


Solution

  • I found it.

    I change this in the getpreferedLanguage method, it works, it does what i expected.

    if ($preferredLanguage !== $this->defaultLocale) {
        $response = new RedirectResponse($this->urlGenerator->generate('homepage', ['_locale' => 'en']));
        $event->setResponse($response); 
    }