php - 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 回退到路径?
解决方案
所以,经过相当多的调查,似乎没有办法让它像 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);
}
}
推荐阅读
- sql - 获取 SQLite 中每个值的百分比?
- javascript - 根据特定行中的单选按钮选择显示/隐藏文本框
请参阅随附的代码以进行理解。HTML 表格根据特定标准显示元素。每当“Extra KM/Hr”中的单选按钮?是,则应启用其右侧的所有列,否则应禁用。我在这个“displayTable”id 上使用 PHP 填充表值。
<html lang="en"> <head> <!-- Meta tags --> <me
- powershell - 如何杀死在文件夹下运行的所有进程?
- javascript - 如何指定 Puppeteer 启动的监视器?
- python - 检查 Python 字符串是否是有效的 Excel 单元格
- typescript - 如何在 cucumber-js-tsflow 中使用 Hooks(之前/之后)?
- r - 在r中一次按组多个变量求和
- laravel - App:setLocale() 不能在 Laravel 上翻译
- ios - iOS 13+ 中的后台任务
- docker - 无法使用 Aws ECS fargate 容器连接到 Aurora MySql