Symfony2 语言环境未根据 AJAX 请求保留
Symfony2 locale not persisted on AJAX request
我有一个自定义的 LocaleListner,它在数据库中保留所选的语言环境:
class LocaleListener implements EventSubscriberInterface
{
private $defaultLocale;
private $securityContext;
private $em;
public function __construct($defaultLocale, SecurityContext $securityContext, EntityManager $em)
{
$this->defaultLocale = $defaultLocale;
$this->securityContext = $securityContext;
$this->em = $em;
}
static public function getSubscribedEvents()
{
return array(
// must be registered before the default Locale listener
KernelEvents::REQUEST => array(
array('onKernelRequest', -50)
),
);
}
public function onKernelRequest(GetResponseEvent $event)
{
if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) {
return;
}
$request = $event->getRequest();
// Impersonate account check
if (!$request->hasPreviousSession()) {
return;
}
$user = null;
if ($this->securityContext->getToken()) {
$user = $this->securityContext->getToken()->getUser();
}
if ($locale = $request->get('_locale')) {
$request->getSession()->set('_locale', $locale);
if (null !== $user && $user != 'anon.' && $user->getLocale() !== $locale) {
$user->setLocale($locale);
$this->em->persist($user);
$this->em->flush();
}
} else if ($user instanceof User && null !== $user->getLocale()) {
$request->getSession()->set('_locale', $user->getLocale());
}
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
}
这非常有效,但是如果我用 AJAX 请求 .
调用控制器,则语言环境将被完全忽略
确实,默认语言环境是 fr。如果我选择 en 语言环境并发出 ajax 请求,返回的模板将被翻译成法语,而不是英语。
这怎么可能?
谢谢。
编辑。
这是我的 javascript ajax 电话(咖啡):
refreshUrl = Routing.generate('ticket_index') + location.search
setInterval =>
$.get refreshUrl, { _ajax: 1 }, (data) =>
@tbody.html data
momentFromNow()
, 10000
我已经尝试在 ajax 请求中添加 _locale 参数,但行不通。
post ajax 请求会非常好我猜问题可能来自您在 ajax.
内发送的数据
$.ajax({
url: path_to_your_controller,
data: {'_locale': 'en'} //you probably are missing this part
})
终于找到为什么这不起作用。
选择的优先级导致此问题,将 -50
更改为 17
解决所有问题。
但我完全不明白为什么优先级会影响 AJAX 请求的翻译...
如果有人有解释,我在听! ;)
感谢 Alexandru Olaru 的帮助。
我有一个自定义的 LocaleListner,它在数据库中保留所选的语言环境:
class LocaleListener implements EventSubscriberInterface
{
private $defaultLocale;
private $securityContext;
private $em;
public function __construct($defaultLocale, SecurityContext $securityContext, EntityManager $em)
{
$this->defaultLocale = $defaultLocale;
$this->securityContext = $securityContext;
$this->em = $em;
}
static public function getSubscribedEvents()
{
return array(
// must be registered before the default Locale listener
KernelEvents::REQUEST => array(
array('onKernelRequest', -50)
),
);
}
public function onKernelRequest(GetResponseEvent $event)
{
if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) {
return;
}
$request = $event->getRequest();
// Impersonate account check
if (!$request->hasPreviousSession()) {
return;
}
$user = null;
if ($this->securityContext->getToken()) {
$user = $this->securityContext->getToken()->getUser();
}
if ($locale = $request->get('_locale')) {
$request->getSession()->set('_locale', $locale);
if (null !== $user && $user != 'anon.' && $user->getLocale() !== $locale) {
$user->setLocale($locale);
$this->em->persist($user);
$this->em->flush();
}
} else if ($user instanceof User && null !== $user->getLocale()) {
$request->getSession()->set('_locale', $user->getLocale());
}
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
}
这非常有效,但是如果我用 AJAX 请求 .
调用控制器,则语言环境将被完全忽略确实,默认语言环境是 fr。如果我选择 en 语言环境并发出 ajax 请求,返回的模板将被翻译成法语,而不是英语。
这怎么可能?
谢谢。
编辑。
这是我的 javascript ajax 电话(咖啡):
refreshUrl = Routing.generate('ticket_index') + location.search
setInterval =>
$.get refreshUrl, { _ajax: 1 }, (data) =>
@tbody.html data
momentFromNow()
, 10000
我已经尝试在 ajax 请求中添加 _locale 参数,但行不通。
post ajax 请求会非常好我猜问题可能来自您在 ajax.
内发送的数据$.ajax({
url: path_to_your_controller,
data: {'_locale': 'en'} //you probably are missing this part
})
终于找到为什么这不起作用。
选择的优先级导致此问题,将 -50
更改为 17
解决所有问题。
但我完全不明白为什么优先级会影响 AJAX 请求的翻译...
如果有人有解释,我在听! ;)
感谢 Alexandru Olaru 的帮助。