2011-04-27 22 views
6

में सिम्फनी 2 लोकेल मैंने सिम्फनी 2 डॉक्टर http://symfony.com/doc/2.0/book/translation.html#the-locale-and-the-url का पालन किया और मेरे मार्गों में लोकेल जोड़ा। लेकिन, मुझे मार्गों के माध्यम से लोकेल को ले जाने का तरीका नहीं मिल रहा है क्योंकि मैं twig टेम्पलेट में {{path ('myroute')}} रखता हूं, लेकिन लोकेल हमेशा मौजूदा लोकेल लेने के बजाय फ़ॉलबैक मान प्राप्त करता है।मार्ग

मैंने {{path ('myroute', {'_locale': _locale})}} की कोशिश की लेकिन मुझे त्रुटि मिली "परिवर्तनीय" _locale "मौजूद नहीं है"।

कोई विचार?

+0

इसे देखें http://stackoverflow.com/questions/7094728/symfony2-locale-switcher-implementation-without-ession-controller –

उत्तर

12

{{ path('address', {'_locale': app.request.attributes.get('_locale')}) }} के साथ फिक्स्ड http://www.mail-archive.com/[email protected]/msg34838.html के लिए धन्यवाद।

+0

_locale 'डिफ़ॉल्ट' कैसे प्राप्त करें? {'_locale': defaults_locale}? चूंकि अंग्रेजी मेरे रूटिंग पैरामीटर में इस तरह सेट है: उपसर्ग:/{_ लोकेल} डिफ़ॉल्ट: _locale: en – Zagloo

+0

@Zagloo मुझे डर है कि मैं आपके प्रश्न को समझ नहीं पा रहा हूं। कृपया अपनी समस्या के विस्तृत विवरण के साथ इसे बेहतर समझाएं या एक नया प्रश्न खोलें। – umpirsky

3

दो पृष्ठों:

localhost.lo/xx/के बारे में

localhost.lo/xx/हैलो/{नाम}

जहां 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

आवश्यकताएं आयातित मार्ग (संसाधन) के साथ काम नहीं करती हैं ... इसलिए ध्यान दें कि स्थानीय इन मार्गों के लिए कुछ भी हो सकता है! –

2

आशुलिपि संकेतन:

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

Symfony2.1 में स्थान अनुरोध में संग्रहीत किया जाता है, तो आप इस का उपयोग करने के लिए है:

{{ path('address', {'_locale': app.request.locale}) }} 
संबंधित मुद्दे