首页 > 解决方案 > Symfony 4 具有默认回退的多语言路由?

问题描述

我正在尝试利用 Symfony 4 的多语言(或多语言环境)路由模式。

我的应用程序是真正的国际化应用程序,并支持超过 25 种不同的语言,尽管翻译是渐进式的,而且许多路线还没有翻译成某些语言。

在这种情况下,我希望他们回到英语默认状态。

我的config/packages/translation.yaml样子是这样的:

framework:
    default_locale: en
    translator:
        default_path: '%kernel.project_dir%/translations'
        fallbacks:
            - en

我的路线是在routes.yaml文件中定义的。例如:

about_index:
    path:
        en: /about-us
        pl: /o-nas
    controller: Symfony\Bundle\FrameworkBundle\Controller\TemplateController
    defaults:
        template: About/index.html.twig

pl现在,每当我使用其中一个或区域设置打开站点时en- 一切都按预期工作,但是当例如我将其设置为 时de,我得到"Unable to generate a URL for the named route "about_index" as such route does not exist."错误。

en当所需区域设置中的路由尚不存在时,如何强制 Symfony 回退到路径?

标签: phpsymfonylocalizationyamlurl-routing

解决方案


所以,经过相当多的调查,似乎没有办法让它像 Symfony 的默认方法那样工作。

我采用了“解决方法”方法,并使用我自己的 Twig 函数扩展了 Symfony 的 Twig Bridge 的路由扩展autopath()

namespace App\Twig;

use Symfony\Bridge\Twig\Extension\RoutingExtension;
use Twig\TwigFunction;

// auto-wired services
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Extends Symfony's Twig Bridge's routing extension providing more flexible localization.
 *
 * @author Kyeno
 */
class KyAutoPathExtension extends RoutingExtension
{
    private $router;
    private $request;

    public function __construct(UrlGeneratorInterface $router, RequestStack $requestStack)
    {
        $this->router = $router;
        $this->request = $requestStack->getCurrentRequest();

        parent::__construct($router);
    }

    /**
     * {@inheritdoc}
     *
     * @return TwigFunction[]
     */
    public function getFunctions()
    {
        return [
            new TwigFunction('autopath', [$this, 'getPathAuto'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']])
        ];
    }

    /**
     * @param string $name
     * @param array  $parameters
     * @param bool   $relative
     *
     * @return string
     */
    public function getPathAuto($name, $parameters = [], $relative = false)
    {
        // obtain current and default locales from request object
        $localeRequested = $this->request->getLocale();
        $localeDefault = $this->request->getDefaultLocale();

        // build localized route name
        // NOTE: Symfony does NOT RECOMMEND this way in their docs, but it's the fastest that popped in my mind
        foreach([sprintf('%s.%s', $name, $localeRequested), sprintf('%s.%s', $name, $localeDefault)] as $nameLocalized) {

            // if such route exists, link to it and break the loop
            if($this->router->getRouteCollection()->get($nameLocalized)) {

                return $this->router->generate($nameLocalized, $parameters, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH);
            }
        }

        // when no matches found, attempt relying on Symfony Twig Bridge's original path() function
        // (and likely fail with exception, unless they fix/allow it)
        return parent::getPath($name, $parameters, $relative);
    }
}

推荐阅读