在symfony 4中如何在不使用首选语言的情况下强制使用语言环境'en'

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

在 synfony4 中,我使用了 symfony 演示应用程序,我想知道如何在不使用浏览器 'fr' 的首选语言的情况下,如何将 defaultlocale 强制设置为 'en' 到我的主页。我只想保留手动更改语言的选择

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: anonymous@example.com

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'

我预计第一个重定向将是 127.0.0.1:8000/en 或 127.0.0.1:8000

但实际输出是127.0.0.1:8000/fr

您可以删除所有区域设置和默认区域设置参数,因此在 services.yaml 中您将剩下:

parameters:
    locale: 'en'
    app.notifications.email_sender: anonymous@example.com

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
            $emailSender: '%app.notifications.email_sender%'

framework.yaml 中,您可以简单地删除该行。

当只有一个区域设置时,不会重定向到区域设置,因此将转到 built-in 服务器上的 127.0.0.1:8000。

我找到了。

我在 getpreferedLanguage 方法中更改了它,它起作用了,它符合我的预期。

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