如何在 Symfony 布局中获取 _locale 变量?

发布于 2024-11-27 11:21:19 字数 695 浏览 3 评论 0原文

我正在使用 Symfony 2 在一个有 2 种语言的网站上工作, 我想根据用户区域设置语言更改路线模式!

示例:

user_login_en:
    pattern:  /en/user/login.html
    defaults: { _controller: SfErrorsAppBundle:User:login, _locale: en }

user_login_fr:
    pattern:  /fr/utilisateur/connexion.html
    defaults: { _controller: SfErrorsAppBundle:User:login, _locale: fr}

在模板内,这并不困难,我只需将 $this->get('session')->getLocale() 从控制器传递到模板...

要工作,我必须调用我的路线:

$router->generate('user_login_'.$locale, array());

但是在我的布局中,我当然有一个菜单和侧边栏,其中有链接...所以我想获取区域设置变量来使用它!所以我的问题很简单:如何在“布局”模板中获取此变量?否则,您有什么想法根据语言改变模式吗?

原因是我希望为所有用户提供漂亮的路线,无论他们是英语还是法语......而且也是出于 SEO 的原因!

I'm working with Symfony 2 on a site which having 2 languages,
and I want to change patterns of my routes depending on user locale language !

Example:

user_login_en:
    pattern:  /en/user/login.html
    defaults: { _controller: SfErrorsAppBundle:User:login, _locale: en }

user_login_fr:
    pattern:  /fr/utilisateur/connexion.html
    defaults: { _controller: SfErrorsAppBundle:User:login, _locale: fr}

Inside a template, this is not difficult, i just have to pass the $this->get('session')->getLocale() from the controller to the template...

To work, I have to call my routes:

$router->generate('user_login_'.$locale, array());

But inside my layouts, I have of course a menu, and sidebars, which have links... So I want to get the locale variable to use it ! So my question is simple: how to get this variable inside a "layout" template ? Otherwise, have you got any idea to change the pattern depending on the language ?

The reasons are that I want beautiful routes for all users, whether they're english or french... And also for a SEO reason !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

舂唻埖巳落 2024-12-04 11:21:21

---从评论中更新---

作为 Symfony 2.1,您必须使用

{{ app.request.locale }}

or

{{ app.request.getLocale() }}

返回 app.request.locale (如果可用)和 app.request.defaultLocale 如果 app.request.locale 未设置。

---UPDATED FROM THE COMMENTS---

As Symfony 2.1, you must use

{{ app.request.locale }}

or

{{ app.request.getLocale() }}

which returns app.request.locale if available and app.request.defaultLocale if app.request.locale is not set.

终陌 2024-12-04 11:21:21

由于 Symfony 2.1 将“区域设置”存储在请求中而不是会话中,因此您必须使用以下命令:

{{ app.request.getLocale() }}

而不是 app.session.locale

As Symfony 2.1 stores the "locale" in the Request instead of the session you have to use this:

{{ app.request.getLocale() }}

instead of app.session.locale

眼泪都笑了 2024-12-04 11:21:21

另外,您可能想简化路由(一条规则):

user_login:
    pattern:  /{_locale}/user/login.html
    defaults: { _controller: SfErrorsAppBundle:User:login }

如果您只想允许某些语言,您可以添加一项要求:

user_login:
    pattern:  /{_locale}/user/login.html
    defaults: { _controller: SfErrorsAppBundle:User:login }
    requirements:
       _locale: fr|en

Also, you might want to simplify your routing (one single rule):

user_login:
    pattern:  /{_locale}/user/login.html
    defaults: { _controller: SfErrorsAppBundle:User:login }

If you want to allow only some languages you can add a requirement:

user_login:
    pattern:  /{_locale}/user/login.html
    defaults: { _controller: SfErrorsAppBundle:User:login }
    requirements:
       _locale: fr|en
从此见与不见 2024-12-04 11:21:21

在我看来,这是自动检测语言环境的最简单且可维护的方法,而不必担心 Symfony 版本:

{% if not app.session.locale is null %} {# Prior to Symfony 2.1 you must get from session, it will be null if upper #}
    Locale: {{ app.session.locale }}
{% else %} {# With Symfony 2.1 or upper you only can get the locale from request #}
    Locale: {{ app.request.locale }}
{% endif %}

此外,如果您愿意,您可以使用对象类似 Twig 模板引擎中的符号

{% if not app.getSession().getLocale() is null %} {# Prior to Symfony 2.1 you must get from session, it will be null if upper #}
    Locale: {{ app.getSession().getLocale() }}
{% else %} {# With Symfony 2.1 or upper you only can get the locale from request #}
    Locale: {{ app.getRequest().getLocale() }}
{% endif %}

参见 Symfony 2.1.0 发行说明 了解更多信息

In my opinion, this is the most easy and maintenable way to autodetect the locale without worrying for Symfony version:

{% if not app.session.locale is null %} {# Prior to Symfony 2.1 you must get from session, it will be null if upper #}
    Locale: {{ app.session.locale }}
{% else %} {# With Symfony 2.1 or upper you only can get the locale from request #}
    Locale: {{ app.request.locale }}
{% endif %}

Also, if you prefer it you can use a object like notation in Twig template engine:

{% if not app.getSession().getLocale() is null %} {# Prior to Symfony 2.1 you must get from session, it will be null if upper #}
    Locale: {{ app.getSession().getLocale() }}
{% else %} {# With Symfony 2.1 or upper you only can get the locale from request #}
    Locale: {{ app.getRequest().getLocale() }}
{% endif %}

See Symfony 2.1.0 release notes for more info

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文