2011-04-27 160 views
6

我跟着Symfony2文档http://symfony.com/doc/2.0/book/translation.html#the-locale-and-the-url并将语言环境添加到我的路线中。但是,我找不到通过路径传递区域设置的方式,因为我将{{path('myroute')}}放在树枝模板中,但区域设置总是获取fallback值,而不是采用当前的区域设置。Symfony2语言环境

我试过{{path('myroute',{'_locale':_locale})}}但我得到错误“Variable”_locale“does not exist”。

有什么想法?

+0

看看这个http://stackoverflow.com/questions/7094728/symfony2-locale-switcher-implementation-without-session-controller – 2011-08-17 15:06:13

回答

12

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

+0

如何获得_locale'默认'? {'_locale':defaults_locale}?因为在我的路由参数中英文是这样设置的:前缀:/ {_ locale}默认: _locale:en – Zagloo 2014-12-08 11:37:59

+0

@Zagloo我恐怕我不明白你的问题。请考虑更好地解释它,或者打开一个新的问题,详细描述你的问题。 – umpirsky 2014-12-09 13:42:52

3

两个页面:

localhost.lo/XX /约

localhost.lo/XX /你好/ {name}的

其中xx - 在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 %} 

- 语言环境.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> 
+1

需求不适用于导入的路由(资源)...所以要注意,本地可以是这些路由的任何东西! – 2012-01-12 08:33:44

2

速记符号:

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

在Symfony2.1区域设置存储在请求,所以你必须使用:

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