72

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 !

Sybio
  • 8,565
  • 3
  • 44
  • 53

4 Answers4

183

---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.

Potherca
  • 13,207
  • 5
  • 76
  • 94
brki
  • 2,672
  • 1
  • 17
  • 12
  • 101
    As Symfony 2.1, you must use `app.request.locale` instead. – Damien Jul 18 '12 at 08:29
  • @Damien and in a php template? – DomingoSL Jul 15 '13 at 09:27
  • 1
    @DomingoSL `$view['request']->getLocale()` – iizno Mar 07 '14 at 21:45
  • 5
    Also in twig i would prefer `{{ app.request.getLocale() }}` since it returns `app.request.locale` if available and `app.request.defaultLocale` if `app.request.locale` is not set. – Anticom Oct 22 '14 at 12:59
  • Updated the answer with the comments of [Damien](http://stackoverflow.com/users/665923/damien) and [Anticom](http://stackoverflow.com/users/966530/anticom) – Potherca Oct 23 '14 at 08:49
  • 4
    Actually, these two options both do the same thing. In Twig, the dot syntax can be used to access getter methods if there's no public property with the name provided (as here - the `locale` property on requests is protected). For the exact rules of how the dot operator works in Twig, see the "Implementation" section under http://twig.sensiolabs.org/doc/templates.html#variables – Sam Sep 09 '15 at 11:18
106

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

benske
  • 3,922
  • 4
  • 24
  • 22
5

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
Vincent Pazeller
  • 1,448
  • 18
  • 28
  • in order to add a requirement you have to use the "requirements" keyword. you missed the final "s" => http://symfony.com/doc/current/book/routing.html#adding-requirements – Francesco Casula Jun 13 '13 at 09:19
2

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

shakaran
  • 10,612
  • 2
  • 29
  • 46