Symfony2:没有会话/控制器的区域设置切换器实现
所以,3.5小时后似乎一切都好......
两个页面:
localhost.lo/xx/about
localhost.lo/xx/hello/{name}
其中 xx -routing.yml 中描述的几个语言环境
--routing.yml
home:
resource: "@JetInformBundle/Resources/config/routing.yml"
prefix: /{_locale}
requirements:
_locale: ^en|de|ru|uk|pl$
-- JetInformBundlerouting.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>
没有基于会话的 R/W 且没有 LocalesController 的解决方案额外的要求。 嘿,大家,你们觉得怎么样? 可能是其他解决方案,其他意见? 只是有趣... 谢谢。
So, seems everything ok after 3.5 hours......
Two pages:
localhost.lo/xx/about
localhost.lo/xx/hello/{name}
where xx - several locales described in 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 %}
-- 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>
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论