我跟随symfony2 doc http://symfony.com/doc/2.0/book/translation.html#the-locale-and-the-url 并在我的路线上添加了语言环境。但是,当我将{{path('myroute')}}放置在twig模板中的路线时,我找不到通过路线携带语言环境的方法,但是语言环境始终会获得后退值,而是占据当前的语言环境。

我尝试了{{path('myRoute',{'_locale':_locale})}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}。

任何想法?

有帮助吗?

解决方案

修复了 {{ path('address', {'_locale': app.request.attributes.get('_locale')}) }} 多亏了这个线程 http://www.mail-archive.com/symfony-users@googlegroups.com/msg34838.html.

其他提示

在symfony2.1中,该语言环境存储在请求中,因此您必须使用以下方式:

{{ path('address', {'_locale': app.request.locale}) }}

两页:

localhost.lo/xx/关于

localhost.lo/xx/hello/{name}

其中xx-路由中描述的几个位置。

- 路由

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 %}

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

速记符号:

{{ path('address', {'_locale': app.session.locale}) }}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top