【问题标题】:Symfony2 locale not persisted on AJAX requestSymfony2 语言环境未在 AJAX 请求上保留
【发布时间】:2015-03-20 03:45:16
【问题描述】:

我有一个自定义 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 参数,不工作。

【问题讨论】:

    标签: php jquery ajax symfony


    【解决方案1】:

    终于找到了为什么这不起作用。

    选择的优先级导致此问题,将-50更改为17解决所有问题。

    但我完全不明白为什么优先级会影响 AJAX 请求的翻译...

    如果有人有解释,我会听到的! ;)

    感谢Alexandru Olaru 的帮助。

    【讨论】:

      【解决方案2】:

      发布 ajax 请求会非常好,我猜问题可能来自您在 ajax 中发送的数据。

      $.ajax({
         url: path_to_your_controller,
         data: {'_locale': 'en'} //you probably are missing this part
      })
      

      【讨论】:

      • 更新了我的帖子。我已经尝试在 ajax 请求上添加 _locale 参数,不工作。
      • 我在您的代码中注意到的是:KernelEvents::REQUEST => array(array('onKernelRequest', -50)),通常在文档和 symfony 2 书中,他们使用 18 而不是-50。我想你应该试试这个数字。
      • 本地默认监听的值不是18吗?
      • 是的,默认语言环境是 18,我使用 17 作为我的粘性语言环境代码。
      猜你喜欢
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多