在synfony4中如何在不采用首选语言的情况下强制区域设置“ en”

时间:2019-07-05 19:56:02

标签: locale symfony4

在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

2 个答案:

答案 0 :(得分:0)

您可以删除所有语言环境和默认语言环境参数,因此在 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 中,您只需删除该行即可。

如果仅设置了一个语言环境,则不会重定向到语言环境,因此将转到内置服务器上的127.0.0.1:8000。

答案 1 :(得分:0)

我找到了。

我在getpreferedLanguage方法中对此进行了更改,它可以正常工作,并且达到了我的预期。

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