Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2: Locale Switcher implementation without session/controller
    primarykey
    data
    text
    <p>So, seems everything ok after 3.5 hours......</p> <p>Two pages:</p> <p>localhost.lo/xx/about</p> <p>localhost.lo/xx/hello/{name}</p> <p>where xx - several locales described in routing.yml</p> <p>-- routing.yml</p> <pre><code>home: resource: "@JetInformBundle/Resources/config/routing.yml" prefix: /{_locale} requirements: _locale: ^en|de|ru|uk|pl$ </code></pre> <p>-- JetInformBundle routing.yml</p> <pre><code>hello: pattern: /hello/{name} defaults: { _controller: JetInformBundle:Default:index, name: 'alexander' } about: pattern: /about defaults: { _controller: JetInformBundle:Default:about } </code></pre> <p>-- DefaultController.php</p> <pre><code>&lt;?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-&gt;render('JetInformBundle:Default:index.html.twig', array('name' =&gt; $name, 'matches' =&gt; $this-&gt;matchAction($request))); } public function aboutAction(Request $request) { return $this-&gt;render('JetInformBundle:Default:about.html.twig', array('matches' =&gt; $this-&gt;matchAction($request))); } protected function matchAction(Request $request) { return $this-&gt;get('router')-&gt;match($request-&gt;getRequestUri()); } } </code></pre> <p>-- index.html.twig</p> <pre><code>{% extends '::base.html.twig' %} {% block body %} &lt;h1&gt;{{ 'hello.name'|trans }} {{ name }}!&lt;/h1&gt; &lt;h3&gt;{{ 'your.locale'|trans }} [{{ app.request.get('_locale') }}]&lt;/h3&gt; {% include 'JetInformBundle:Default:locales.html.twig' with { 'uripath': 'hello', 'params': { 'name': app.request.get('name') } } %} {% include 'JetInformBundle:Default:matches.html.twig' with { 'matches': matches } %} &lt;div&gt; &lt;p&gt;{{ 'return.to'|trans }} &lt;a href="{{ path('about', { '_locale': app.request.get('_locale') }) }}"&gt;About&lt;/a&gt;&lt;/p&gt; &lt;/div&gt; {% endblock %} </code></pre> <p>-- about.html.twig</p> <pre><code>{% extends '::base.html.twig' %} {% block body %} &lt;h1&gt;{% trans %}about.page{% endtrans %}&lt;/h1&gt; &lt;h3&gt;{% trans %}your.locale{% endtrans %} [{{ app.request.get('_locale') }}]&lt;/h3&gt; {% include 'JetInformBundle:Default:locales.html.twig' with { 'uripath': 'about', 'params': {}} %} {% include 'JetInformBundle:Default:matches.html.twig' with { 'matches': matches } %} &lt;div&gt; &lt;p&gt;{% trans%}return.to{% endtrans%} &lt;a href="{{ path('hello', { 'name': app.request.get('name'), '_locale': app.request.get('_locale') }) }}"&gt;Hello&lt;/a&gt;&lt;/p&gt; &lt;/div&gt; {% endblock %} </code></pre> <p>-- locales.html.twig</p> <pre><code>{% if not params %} {% set params = {} %} {% endif %} &lt;div class="langs"&gt; &lt;ul&gt; &lt;li&gt; {% if app.request.get('_locale') == 'ru' %} Русский {% else %} &lt;a href="{{ path(uripath, params|merge({ '_locale': 'ru' })) }}"&gt;Русский&lt;/a&gt; {% endif %} &lt;/li&gt; &lt;li&gt; {% if app.request.get('_locale') == 'en' %} English {% else %} &lt;a href="{{ path(uripath, params|merge({ '_locale': 'en' })) }}"&gt;English&lt;/a&gt; {% endif %} &lt;/li&gt; &lt;li&gt; {% if app.request.get('_locale') == 'uk' %} Украiнська {% else %} &lt;a href="{{ path(uripath, params|merge({ '_locale': 'uk' })) }}"&gt;Украiнська&lt;/a&gt; {% endif %} &lt;/li&gt; &lt;li&gt; {% if app.request.get('_locale') == 'de' %} Deutsch {% else %} &lt;a href="{{ path(uripath, params|merge({ '_locale': 'de' })) }}"&gt;Deutsch&lt;/a&gt; {% endif %} &lt;/li&gt; &lt;li&gt; {% if app.request.get('_locale') == 'pl' %} Polish {% else %} &lt;a href="{{ path(uripath, params|merge({ '_locale': 'pl' })) }}"&gt;Polish&lt;/a&gt; {% endif %} &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre> <p>-- matches.html.twig</p> <pre><code>&lt;h5&gt;Matches&lt;/h5&gt; &lt;ol&gt; {% for key, value in matches %} &lt;li&gt;{{ key }} : {{ value }} &lt;/li&gt; {% endfor %} &lt;/ol&gt; </code></pre> <p>Solution without session based R/W and without LocalesController with additional request. Hey, All, what do you think about ? May be other solutions, another opinions ?? Just interesting... Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload