如何让 Symfony 4 项目中的所有 URL 包含 _locale
How to get all URLs in a Symfony 4 project to contain the _locale
我正在尝试为 Symfony 4 项目(使用 Twig)创建语言切换器。基本上当点击 link 时,网站的语言会改变。
我的配置是这样的:
config\packages\translation.yaml
framework:
default_locale: en
translator:
default_path: '%kernel.project_dir%/translations'
fallbacks:
- en
这是我在
中得到的重要台词
config\packages\services.yaml
:
parameters:
locale: 'en'
# This parameter defines the codes of the locales (languages) enabled in the application
app_locales: en|fr
我的控制器:
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraints\Json;
// Class name really should match Controller name
class ControllerBase extends AbstractController
{
/**
* @Route("/{_locale}", name="app_landing", defaults={"_locale" = "en"}, requirements={"_locale" = "en|fr"})
*/
public function index(Request $request)
{
$locale = $request->getLocale();
return $this->render('landing/index.html.twig');
}
}
实际的语言切换器只是模板文件中包含的两个列表元素。它们看起来像这样:
<li><a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale': 'en'})) }}">English</a></li>
<li><a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale': 'fr'})) }}">French</a></li>
这很好用。
单击网站上的另一个 link 时出现问题。它不包含 _locale,站点默认返回 en
.
我真的希望 _locale 自动添加到每个 link(包括索引路径“/”)。
经过一段时间的试验,我找到了答案。
我试图用两种不同的方式做同样的事情。在我的例子中,最好只使用模板文件中的代码并省略控制器中的 {_locale} 参数。
就这样一切正常。
我正在尝试为 Symfony 4 项目(使用 Twig)创建语言切换器。基本上当点击 link 时,网站的语言会改变。
我的配置是这样的:
config\packages\translation.yaml
framework:
default_locale: en
translator:
default_path: '%kernel.project_dir%/translations'
fallbacks:
- en
这是我在
中得到的重要台词config\packages\services.yaml
:
parameters:
locale: 'en'
# This parameter defines the codes of the locales (languages) enabled in the application
app_locales: en|fr
我的控制器:
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraints\Json;
// Class name really should match Controller name
class ControllerBase extends AbstractController
{
/**
* @Route("/{_locale}", name="app_landing", defaults={"_locale" = "en"}, requirements={"_locale" = "en|fr"})
*/
public function index(Request $request)
{
$locale = $request->getLocale();
return $this->render('landing/index.html.twig');
}
}
实际的语言切换器只是模板文件中包含的两个列表元素。它们看起来像这样:
<li><a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale': 'en'})) }}">English</a></li>
<li><a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale': 'fr'})) }}">French</a></li>
这很好用。
单击网站上的另一个 link 时出现问题。它不包含 _locale,站点默认返回 en
.
我真的希望 _locale 自动添加到每个 link(包括索引路径“/”)。
经过一段时间的试验,我找到了答案。
我试图用两种不同的方式做同样的事情。在我的例子中,最好只使用模板文件中的代码并省略控制器中的 {_locale} 参数。
就这样一切正常。