【问题标题】:Symfony2 locale in route路线中的 Symfony2 语言环境
【发布时间】:2011-08-13 15:04:10
【问题描述】:

我关注了 Symfony2 doc http://symfony.com/doc/2.0/book/translation.html#the-locale-and-the-url 并将语言环境添加到我的路线中。但是,当我将 {{ path('myroute') }} 放在 twig 模板中时,我找不到通过路线携带语言环境的方法,但是语言环境总是获得后备值而不是采用当前语言环境。

我尝试了 {{ path('myroute', {'_locale': _locale}) }} 但出现错误“变量“_locale”不存在”。

有什么想法吗?

【问题讨论】:

标签: php routes translation locale symfony


【解决方案1】:

由于此线程http://www.mail-archive.com/symfony-users@googlegroups.com/msg34838.html 已修复{{ path('address', {'_locale': app.request.attributes.get('_locale')}) }}

【讨论】:

  • 如何获取 _locale 'default' ? {'_locale': defaults_locale } ?因为在我的路由参数中英语是这样设置的:前缀:/{_locale} 默认值:_locale:en
  • @Zagloo 恐怕我不明白你的问题。请考虑更好地解释它或打开一个新问题,详细描述您的问题。
【解决方案2】:

两页:

localhost.lo/xx/about

localhost.lo/xx/hello/{name}

其中 xx - routing.yml 中描述的几个语言环境

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

【讨论】:

  • 要求不适用于导入的路线(资源)...所以请注意,对于这些路线,本地可以是任何东西!
【解决方案3】:

简写:

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

【讨论】:

    【解决方案4】:

    在 Symfony2.1 中,语言环境存储在请求中,所以你必须使用这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-07
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      相关资源
      最近更新 更多