我正在使用 symfony2 开发一个应用程序。我面临本地化问题。我想在学说生命周期的 postLoad 事件中设置,但可以找到一种方法来做到这一点。我正在使用路线方法来设置我的本地例如:
http://example.com/en/content
这是我的听众:
namespace MyApiBundle\Listener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\ORM\Event\LifecycleEventArgs;
class LocaleListener
{
private $local;
public function __construct($local) {
$this->local = $local;
}
public function postLoad(LifecycleEventArgs $args)
{
$local= 'en'; // I need to get the local from here
$entity = $args->getEntity();
if(method_exists($entity, 'setLocale')) {
$entity->setLocale($local);
}
}
}
从这里有什么快速的方法可以到达本地吗?无法使用 new Request(),因为它总是返回 en
我还有 3 种其他语言。感谢您的帮助
最佳答案
是的,可以。可以注入(inject)@request_stack
service进入您的监听器,从中获取请求并读取区域设置。
但是,有一个 Doctrine 扩展可能可以满足您的需求:Translatable
关于php - Symfony2 getLocal() 在加载后生命周期事件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34662166/