Symfony2语言环境语言整页事件监听器 [英] Symfony2 locale languages whole page event listener
问题描述
我需要创建一个类似于facebook lang系统的系统,当用户单击语言以显示例如france('fr')的示例时,页面将重新加载,并且message.fr.yml中的所有内容都将显示... >
我试图制作类似于/language/{localExtension}
的路线,但是当我在那里设置setLocale然后重定向它不起作用...我不知道我是否为某些页面而不是全局设置了它?因为当我在控制器的顶部设置setLocale('fr')
时,它可以工作...
我在使用www.example.com/contact/en
,/contact/fr
等
但是我想从以前选择的语言中显示/contact
和内容.或像现在一样在用户未更改时默认为'en'...
我整天都在搜索,我认为应该使用... service => listener和... on kernel.request来完成?或类似的东西.
这里有有趣的链接 Symfony2错误的语言环境检测?我认为这是我需要的吗?或者?我试图设置服务并创建侦听器,但出现了一些错误,我什至不知道这是否是创建它的方法:/
是的,由于某些原因,您需要使用侦听器:
<?php
namespace Your\Bundle\Listener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LocaleListener implements EventSubscriberInterface
{
private $defaultLocale;
public function __construct($defaultLocale = 'en')
{
$this->defaultLocale = $defaultLocale;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}
if ($locale = $request->attributes->get('_locale')) {
$request->getSession()->set('_locale', $locale);
} else {
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
}
static public function getSubscribedEvents()
{
return array(
// must be registered before the default Locale listener
KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
);
}
}
?>
在service.xml中注册您的侦听器:
<service id="my.listener" class="Your\Bundle\Listener\LocaleListener">
<argument>%locale%</argument>
<tag name="kernel.event_subscriber"/>
</service>
如何在树枝模板中实现语言切换器的示例:
{% for locale in ['en', 'fr','zh'] %}
<li>
<a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale' : locale})) }}">
{% if locale == 'en' %}
<img title="English" src="{{ asset('bundles/fkmywebsite/images/UnitedStates.png') }}" alt="English" height="30" width="30"/>
{% elseif locale == 'fr' %}
<img title="Français" src="{{ asset('bundles/fkmywebsite/images/France.png') }}" alt="Français" height="30" width="30"/>
{% endif %}
</div>
</li>
{% endfor %}
I need to create a system like the facebook lang system when a user clicks on language to example france('fr') the page will reload and all the content in messages.fr.yml will be displayed...
I tried to make route like /language/{localExtension}
but when I setLocale there and then redirected it didn't work... I don't know i assuming its just for certain page not global? Because when i set setLocale('fr')
at the top of my controller it works...
I found some articles when is using the www.example.com/contact/en
, /contact/fr
etc
But i want /contact
and content displayed from previous chosen language. Or default 'en' like now when user didn't change it...
I was googling all day and i think it should be done with... service => listener and... on kernel.request? or something like that.
Here are intresting link Symfony2 wrong locale detection? i think that's what i need? or? I tried to set service and create listener but some errors appear and i don't even know if this is the way how to create it :/
Yes for some reason you need to use a listener:
<?php
namespace Your\Bundle\Listener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LocaleListener implements EventSubscriberInterface
{
private $defaultLocale;
public function __construct($defaultLocale = 'en')
{
$this->defaultLocale = $defaultLocale;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}
if ($locale = $request->attributes->get('_locale')) {
$request->getSession()->set('_locale', $locale);
} else {
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
}
static public function getSubscribedEvents()
{
return array(
// must be registered before the default Locale listener
KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
);
}
}
?>
Register your listener in your service.xml:
<service id="my.listener" class="Your\Bundle\Listener\LocaleListener">
<argument>%locale%</argument>
<tag name="kernel.event_subscriber"/>
</service>
An example how to implement the language switcher in your twig template:
{% for locale in ['en', 'fr','zh'] %}
<li>
<a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale' : locale})) }}">
{% if locale == 'en' %}
<img title="English" src="{{ asset('bundles/fkmywebsite/images/UnitedStates.png') }}" alt="English" height="30" width="30"/>
{% elseif locale == 'fr' %}
<img title="Français" src="{{ asset('bundles/fkmywebsite/images/France.png') }}" alt="Français" height="30" width="30"/>
{% endif %}
</div>
</li>
{% endfor %}
这篇关于Symfony2语言环境语言整页事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!