首页 > 解决方案 > Symfony 5 在某些请求中没有检测到正确的语言环境

问题描述

我正在为 Symfony 如何设置语言环境而苦苦挣扎。

这是我的annotations.yaml文件:

controllers_frontend:
resource: ../../src/Controller/Frontend
type: annotation
host:
  'de-DE': example.de
  'de-AT': example.at
  'de-CH': example.ch
  'fr-CH': example.ch
prefix:
  de-DE: '' # don't prefix URLs for German
  de-AT: ''
  de-CH: '/de'
  fr-CH: '/fr'

这非常适用于控制器 A 中的路由“some-route”:

example.de/some-route -> locale = de-DE
example.at/some-route -> locale = de-AT
example.ch/de/some-route -> locale = de-CH

然后我有一个不同的控制器 B,在这里我有以下问题:

example.de/swatch/checkout/confirm -> locale not detected, it goes back to the fall back locale
example.at/swatch/checkout/confirm -> locale not detected, it goes back to the fall back locale
example.ch/de/swatch/checkout/confirm -> locale = de-CH

这是该路线的代码

<?php

namespace App\Controller\Frontend;
...

class OrderController extends AbstractController
{

    /**
     * @Route("/swatch/checkout/confirm", priority="10", name="swatch_checkout_confirm")
     */
    public function checkout_confirm(Request $request, OrderHelper $orderHelper)
    {
        $country = $orderHelper->getCountry($request->getLocale());

        return $this->render('frontend/order/checkout_confirm.html.twig',
            array(
                'country' => $country
            )
        );
    }

由于我在 annotations.yaml 中的语言环境设置适用于“some-route”,我相信它是正确的。

标签: phpsymfonyannotationslocale

解决方案


我找到了解决方案。当我从路由中删除“优先级”时,可以正确检测到语言环境。不确定这是 Symfony 中的错误还是我的语言环境设置错误。


推荐阅读