【问题标题】:symfony2.1 translatable saved, but not retrievedsymfony2.1 可翻译已保存,但未检索
【发布时间】:2012-08-08 12:44:37
【问题描述】:

基本上,我遇到了和这里一样的问题:

Symfony2 & Translatable : entity's locale is empty

翻译保存在ext_translations 表中,但未显示。

添加建议的修复程序后,它确实起作用了。

今天我从 2.0 升级到 2.1,到目前为止,我设法让几乎所有东西都能正常工作。

但现在我的可翻译内容再次无法正确显示(它们仍在正确保存)。

我认为这与 2.1 与 2.0 相比用户区域设置在何处以及如何存储的变化有关......但我无法弄清楚这一点。

【问题讨论】:

  • 因为显然我无法回答我自己的问题
  • 我有解决方案,但我不能发布它:( ..

标签: doctrine-orm symfony-2.1


【解决方案1】:

通过注册自定义监听器解决了这个问题

namespace XXX;

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', $request->getLocale());
       } else {
           $request->setDefaultLocale($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)),
       );
   }
}

后来变了

$request->setDefaultLocale($request->getSession()->get('_locale', $this->defaultLocale));

$request->setLocale($request->getSession()->get('_locale'));

使用过

$this->getRequest()->getSession()->set('_locale', 'nl');

现在可以设置语言环境、翻译和可译文

希望这也对其他人有所帮助..

【讨论】:

    猜你喜欢
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多