路线中的 Symfony2 语言环境
我关注了 Symfony2 文档 http://symfony.com /doc/2.0/book/translation.html#the-locale-and-the-url 并将区域设置添加到我的路线中。但是,当我将 {{ path('myroute') }} 放置在 twig 模板中时,我找不到通过路由携带语言环境的方法,但语言环境总是获取回退值而不是采用当前语言环境。
我尝试了 {{ path('myroute', {'_locale': _locale}) }} 但出现错误“变量“_locale”不存在”。
有什么想法吗?
I followed Symfony2 doc http://symfony.com/doc/2.0/book/translation.html#the-locale-and-the-url and added locale to my routes. But, I can't find the way to carry locale through routes as I place {{ path('myroute') }} in twig template, but locale always gets fallback value instead taking current locale.
I tried {{ path('myroute', {'_locale': _locale}) }} but I got error "Variable "_locale" does not exist".
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(4)
感谢此线程 http://www.mail-archive.com/[电子邮件受保护]/msg34838.html。
Fixed with
{{ path('address', {'_locale': app.request.attributes.get('_locale')}) }}
thanks to this thread http://www.mail-archive.com/[email protected]/msg34838.html.在 Symfony2.1 中,区域设置存储在请求中,因此您必须使用:
In Symfony2.1 the locale is stored in the request, so you have to use this:
两个页面:
localhost.lo/xx/about
localhost.lo/xx/hello/{name}
其中 xx -routing.yml 中描述的几个语言环境 --routing.yml
--
JetInformBundlerouting.yml
-- DefaultController.php
-- index .html.twig
-- about.html.twig
-- locales.html.twig
-- matches.html.twig
Two pages:
localhost.lo/xx/about
localhost.lo/xx/hello/{name}
where xx - several locales described in routing.yml
-- routing.yml
-- JetInformBundle routing.yml
-- DefaultController.php
-- index.html.twig
-- about.html.twig
-- locales.html.twig
-- matches.html.twig
简写符号:
Shorthand notation: