Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 locale in route

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?

like image 454
umpirsky Avatar asked Apr 27 '11 07:04

umpirsky


4 Answers

Fixed with {{ path('address', {'_locale': app.request.attributes.get('_locale')}) }} thanks to this thread http://www.mail-archive.com/[email protected]/msg34838.html.

like image 93
umpirsky Avatar answered Nov 18 '22 16:11

umpirsky


In Symfony2.1 the locale is stored in the request, so you have to use this:

{{ path('address', {'_locale': app.request.locale}) }}
like image 41
benske Avatar answered Nov 18 '22 17:11

benske


Two pages:

localhost.lo/xx/about

localhost.lo/xx/hello/{name}

where xx - several locales described in routing.yml

-- routing.yml

home:
  resource: "@JetInformBundle/Resources/config/routing.yml"
  prefix: /{_locale}
  requirements:
    _locale: ^en|de|ru|uk|pl$

-- JetInformBundle routing.yml

hello:
  pattern:  /hello/{name}
  defaults: { _controller: JetInformBundle:Default:index, name: 'alexander' }

about:
  pattern:  /about
  defaults: { _controller: JetInformBundle:Default:about }

-- DefaultController.php

<?php

namespace Jet\InformBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    public function indexAction($name, Request $request)
    {
        return $this->render('JetInformBundle:Default:index.html.twig',
                             array('name' => $name, 'matches' =>     $this->matchAction($request)));
    }

    public function aboutAction(Request $request)
    {
        return $this->render('JetInformBundle:Default:about.html.twig',
                              array('matches' => $this->matchAction($request)));
    }

    protected function matchAction(Request $request)
    {
        return $this->get('router')->match($request->getRequestUri());
    }
}

-- index.html.twig

{% extends '::base.html.twig' %}

{% block body %}
<h1>{{ 'hello.name'|trans }} {{ name }}!</h1>
<h3>{{ 'your.locale'|trans }} [{{ app.request.get('_locale') }}]</h3>

{% include 'JetInformBundle:Default:locales.html.twig'
            with {
                'uripath': 'hello',
                'params': {
                    'name': app.request.get('name')
                }
            }
%}

{% include 'JetInformBundle:Default:matches.html.twig'
            with { 'matches': matches } %}

<div>
    <p>{{ 'return.to'|trans }} <a href="{{ path('about', { '_locale': app.request.get('_locale') }) }}">About</a></p>
</div>
{% endblock %}

-- about.html.twig

{% extends '::base.html.twig' %}

{% block body %}
<h1>{% trans %}about.page{% endtrans %}</h1>
<h3>{% trans %}your.locale{% endtrans %} [{{ app.request.get('_locale') }}]</h3>

{% include 'JetInformBundle:Default:locales.html.twig'
            with { 'uripath': 'about', 'params': {}} %}

{% include 'JetInformBundle:Default:matches.html.twig'
            with { 'matches': matches } %}

<div>
    <p>{% trans%}return.to{% endtrans%} <a href="{{ path('hello', { 'name': app.request.get('name'), '_locale': app.request.get('_locale') }) }}">Hello</a></p>
</div>
{% endblock %}

-- locales.html.twig

{% if not params %}
    {% set params = {} %}
{% endif %}

<div class="langs">
    <ul>
        <li>
            {% if app.request.get('_locale') == 'ru' %}
                Русский
            {% else %}
                <a href="{{ path(uripath, params|merge({ '_locale': 'ru' })) }}">Русский</a>
            {% endif %}
        </li>
        <li>
            {% if app.request.get('_locale') == 'en' %}
                English
            {% else %}
                <a href="{{ path(uripath, params|merge({ '_locale': 'en' })) }}">English</a>
            {% endif %}
        </li>
        <li>
            {% if app.request.get('_locale') == 'uk' %}
                Украiнська
            {% else %}
                <a href="{{ path(uripath, params|merge({ '_locale': 'uk' })) }}">Украiнська</a>
            {% endif %}
        </li>
        <li>
            {% if app.request.get('_locale') == 'de' %}
                Deutsch
            {% else %}
                <a href="{{ path(uripath, params|merge({ '_locale': 'de' })) }}">Deutsch</a>
            {% endif %}
        </li>
        <li>
            {% if app.request.get('_locale') == 'pl' %}
                Polish
            {% else %}
                <a href="{{ path(uripath, params|merge({ '_locale': 'pl' })) }}">Polish</a>
            {% endif %}
        </li>
    </ul>
</div>

-- matches.html.twig

<h5>Matches</h5>
<ol>
{% for key, value in matches %}
    <li>{{ key }} : {{ value }} </li>
{% endfor %}
</ol>
like image 3
Alexander Vasilenko Avatar answered Nov 18 '22 17:11

Alexander Vasilenko


Shorthand notation:

{{ path('address', {'_locale': app.session.locale}) }}
like image 2
Philipp Rieber Avatar answered Nov 18 '22 16:11

Philipp Rieber