亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Symfony 4 多語言路由具有默認回退功能嗎?

Symfony 4 多語言路由具有默認回退功能嗎?

PHP
烙印99 2023-09-15 21:19:53
我正在嘗試利用 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.twigpl現在,每當我使用或區域設置打開站點時en- 一切都會按預期工作,但是當例如我將其設置為 時de,我會收到"Unable to generate a URL for the named route "about_index" as such route does not exist."錯誤。en當所需語言環境中的路由尚不存在時,如何強制 Symfony 回退到路徑?
查看完整描述

2 回答

?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

適用于 SF 4.4,使用注釋:您可以聲明雙 @Route 注釋,一個帶有本地化路由,另一個沒有本地化。如果與第一個注釋不匹配,將使用非本地化路由。


 * @Route({

 *      "fr": "/bonjour",

 *      "de": "/guten-tag"

 * }, name="hello_path")

 * @Route("/hello", name="hello_path")


查看完整回答
反對 回復 2023-09-15
?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

因此,經過相當多的調查后,似乎沒有辦法讓它像 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);

    }

}


查看完整回答
反對 回復 2023-09-15
  • 2 回答
  • 0 關注
  • 129 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號