How to get _locale variable inside symfony layout?

I work with Symfony 2 on a site that has 2 languages, and I want to change the patterns of my routes depending on the user's 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 the template, this is not complicated, I just need to pass $ 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 menus and sidebars that have links ... Therefore, I want the locale variable to use it! So my question is simple: how to get this variable inside the "layout" template? Otherwise, do you have an idea to change the template depending on the language?

The reasons are that I want beautiful routes for all users, whether English or French ... And also because of SEO!

+48
layout templates symfony routing translation
Aug 02 2018-11-21T00:
source share
4 answers

--- UPDATED FROM COMMENTS ---

Like Symfony 2.1, you should use

 {{ app.request.locale }} 

or

 {{ app.request.getLocale() }} 

which returns app.request.locale if available, and app.request.defaultLocale if app.request.locale not installed.

+117
Sep 16 '11 at 7:45
source share

Since Symfony 2.1 saves "locale" in the request instead of a session, you should use this:

 {{ app.request.getLocale() }} 

instead of app.session.locale

+101
Oct 12
source share

In addition, you can simplify your routing (one single rule):

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

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

 user_login:
     pattern: /{_localeasure/user/login.html
     defaults: {_controller: SfErrorsAppBundle: User: login}
     requirements:
        _locale: fr | en
+3
Apr 24 2018-12-12T00:
source share

In my opinion, this is the easiest and most reliable way to automatically detect the language without worrying about the version of 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 %} 

Alternatively, if you prefer it, you can use the object as a designation in 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 %} 

Learn more about Symfony 2.1.0 Release Notes for more information.

+2
May 24 '13 at
source share



All Articles