我创建了一个Symfony 4.4.8项目,将其翻译成英语和法语,因此我遵循了以下步骤:
https://symfony.com/doc/current/translation.html
并设置:
config/packages/translation.yaml
framework:
default_locale: 'fr'
translator:
default_path: '%kernel.project_dir%/translations'
fallbacks: ['en', 'fr']config/routes/annotations.yaml
controllers:
resource: ../../src/Controller/
type: annotation
prefix: /{_locale}/
requirements:
_locale: fr|en和一个src/Controller/HomeController.php
class HomeController extends AbstractController
{
private $session;
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
/**
* @Route("/", name="home")
*/
public function index() {
return $this->render('home/index.html.twig', [
'controller_name' => 'HomeController',
]);
}翻译文件夹中有翻译文件,当我运行localhost:8000/fr或localhost:8000/en时,就会显示我的home/index.html.twig。
问题是,当我运行localhost:8000/,时,它会显示默认欢迎到Symfony页面“您看到这个页面是因为您没有配置任何主页URL”。
this solution for sf2将在sf4上工作吗?
有人知道怎么解决吗?
我还测试了如何将HomeController中的路由更改为:
@Route("/{_locale}", name="home", defaults={"_locale"="fr"}
但它返回异常:
Route pattern "/{_locale}/{_locale}" cannot reference variable name "_locale" more than once.
发布于 2020-10-09 09:48:00
解决办法,补充说:
RedirectMatch ^/$ /fr
在我的虚拟主机中(使用apache2服务器)
纯正的symfony溶液应该更好!
发布于 2020-10-09 06:18:44
我猜是个糟糕的路由配置。我不记得在URL中添加区域代码来设置区域设置的默认行为,所以我想这是您添加的;)
Symfony按照声明路由的相同顺序解析该路由。因此,一种解决方案是导入相同路由的2倍,但前缀为:
# config/route.yaml
app_front:
type: annotation
resource: '../src/Controller/*.php'
prefix: /
options:
expose: true
app_localized_front:
type: annotation
resource: '../src/Controller/*.php'
name_prefix: localized_
prefix: /{locale}
options:
expose: true我知道前面的配置是可行的,但我不知道这是否是一种更优雅的方法:)
https://stackoverflow.com/questions/64264257
复制相似问题