如何在 SonataAdmin 中获取当前语言环境

How to get the current locale in SonataAdmin

我想知道如何在 SonataAdmin Admin class 中获取当前语言环境。 我知道我可以将 requestStack 作为管理服务的参数传递,但我想知道 sonata admin 是否提供了一种直接访问当前语言环境的方法。

您已经可以访问 request (\Symfony\Component\HttpFoundation\Request)。

https://github.com/sonata-project/SonataAdminBundle/blob/3.x/Admin/AbstractAdmin.php#L2225

所以 $this->request->getLocale() 就可以了。