Symfony - 如何切换和使用语言环境
Symfony - how switch and work with locale
在 Twig 模板中使用语言环境的最佳实践是什么:
- 如何在语言之间切换(使用模板中的语言切换器)以及如何保持现有 URL 仅更改语言
- 如何创建具有区域设置支持的 URL
例如:
我有 2 种语言(ES 和 EN),默认语言是 ES,对于我的主页,我为 /
创建了 2 个路由注释(对于默认语言,在本例中为 ES)和我的控制器文件中的 /{_locale}/
(对于其他语言)。
现在我需要将我的 twig 模板的语言环境参数获取到我的 URL,但前提是我不会使用我的默认语言。
手动重写 URL 工作正常,但是有什么简单的方法可以在我的树枝模板上创建时将语言环境参数添加到 URL 吗?
变量locale的实际值可以传递给Controller,但是有没有更好的方法在twig中得到它?
编辑:
/**
* @Route(
* "/{_locale}/branches", name="branches",
* requirements={"_locale":"%app_locales%"}
* )
*
*/
public function indexAction(Request $request) {
return $this->render('branches/index.html.twig');
}
index.html.twig
<li class="{% if app.request.attributes.get('_route') starts with 'branches' %}active{% endif %}">
<a href="{{ path('branches') }}" class="">{{ 'header.menu.branches'|trans }}</a>
</li>
我得到了
找不到 "GET /branches" 的路线 我使用这个 URL http://localhost:8080/en/branches (works OK) and http://localhost:8080/branches (错误)我必须使用这样的东西:
/**
* @Route(
* "/branches/", name="branches_def",
* defaults={"_locale":"%locale%"},
* )
* @Route(
* "/{_locale}/branches/", name="branches",
* requirements={"_locale":"%app_locales%"}
* )
*
*/
public function indexAction(Request $request) {
return $this->render('branches/index.html.twig');
}
URL 使用路径生成是可以的,但是如果我在浏览器中从我的 URL 中删除语言环境参数,我会收到此错误。
非常感谢任何有用的建议
将您的语言环境保存到 cookie 中,然后在您的 twig 文件中创建类似 <a href="{{ path('your-path-name', {'locale': app.request.cookies.get('LOCALE_COOKIE')}) }}">
的链接
如果未设置 cookie,我不确定您是否会收到错误或空值(这将帮助您在控制器路由中使用默认区域设置),但您应该尝试一下。
- 在 config.yml
中取消注释行 translator: { fallbacks: ['%locale%'] }
在同一个文件中声明需要的变量:
parameters:
locale: en
app.locales: en|lv|ru
在 routing.yml 中声明您需要的路线,如下所示:
contact_us:
path: /{_locale}/contact
defaults:
_controller: 'AppBundle:Default:contact'
_locale: '%app.locales%'
- 使用不带参数的
{{ path('another_route') }}
,以防 another_route
使用 _locale 占位符声明。同样对于单个文本翻译使用 {{ 'home.title'|trans }}
.
在 Twig 模板中使用语言环境的最佳实践是什么:
- 如何在语言之间切换(使用模板中的语言切换器)以及如何保持现有 URL 仅更改语言
- 如何创建具有区域设置支持的 URL
例如:
我有 2 种语言(ES 和 EN),默认语言是 ES,对于我的主页,我为 /
创建了 2 个路由注释(对于默认语言,在本例中为 ES)和我的控制器文件中的 /{_locale}/
(对于其他语言)。
现在我需要将我的 twig 模板的语言环境参数获取到我的 URL,但前提是我不会使用我的默认语言。
手动重写 URL 工作正常,但是有什么简单的方法可以在我的树枝模板上创建时将语言环境参数添加到 URL 吗?
变量locale的实际值可以传递给Controller,但是有没有更好的方法在twig中得到它?
编辑:
/**
* @Route(
* "/{_locale}/branches", name="branches",
* requirements={"_locale":"%app_locales%"}
* )
*
*/
public function indexAction(Request $request) {
return $this->render('branches/index.html.twig');
}
index.html.twig
<li class="{% if app.request.attributes.get('_route') starts with 'branches' %}active{% endif %}">
<a href="{{ path('branches') }}" class="">{{ 'header.menu.branches'|trans }}</a>
</li>
我得到了
找不到 "GET /branches" 的路线 我使用这个 URL http://localhost:8080/en/branches (works OK) and http://localhost:8080/branches (错误)我必须使用这样的东西:
/**
* @Route(
* "/branches/", name="branches_def",
* defaults={"_locale":"%locale%"},
* )
* @Route(
* "/{_locale}/branches/", name="branches",
* requirements={"_locale":"%app_locales%"}
* )
*
*/
public function indexAction(Request $request) {
return $this->render('branches/index.html.twig');
}
URL 使用路径生成是可以的,但是如果我在浏览器中从我的 URL 中删除语言环境参数,我会收到此错误。
非常感谢任何有用的建议
将您的语言环境保存到 cookie 中,然后在您的 twig 文件中创建类似 <a href="{{ path('your-path-name', {'locale': app.request.cookies.get('LOCALE_COOKIE')}) }}">
如果未设置 cookie,我不确定您是否会收到错误或空值(这将帮助您在控制器路由中使用默认区域设置),但您应该尝试一下。
- 在 config.yml 中取消注释行
在同一个文件中声明需要的变量:
parameters: locale: en app.locales: en|lv|ru
在 routing.yml 中声明您需要的路线,如下所示:
contact_us: path: /{_locale}/contact defaults: _controller: 'AppBundle:Default:contact' _locale: '%app.locales%'
- 使用不带参数的
{{ path('another_route') }}
,以防another_route
使用 _locale 占位符声明。同样对于单个文本翻译使用{{ 'home.title'|trans }}
.
translator: { fallbacks: ['%locale%'] }