我想翻译我的 ajax 登录错误消息。我正在使用 FOSUserBundle,我会利用我正确覆盖文件夹 Resources/translations 的翻译文件。
我的 AthenticationHandler.php:
class AuthenticationHandler
implements AuthenticationSuccessHandlerInterface,
AuthenticationFailureHandlerInterface
{
protected $router;
protected $security;
protected $userManager;
protected $service_container;
public function __construct(RouterInterface $router,SecurityContext $security, $userManager, $service_container)
{
$this->router = $router;
$this->security = $security;
$this->userManager = $userManager;
$this->service_container = $service_container;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token) {
if ($request->isXmlHttpRequest()) {
//...
}
return new RedirectResponse($this->router->generate('anag_new'));
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) {
if ($request->isXmlHttpRequest()) {
$error = $exception->getMessage();
$result = array('success' => false, 'message' => $request->get('translator')->trans($error, array(), 'FOSUserBundle'));
$response = new Response(json_encode($result));
$response->headers->set('Content-Type', 'application/json');
return $response;
} else {
//...
}
}
}
但返回此错误:
Fatal error: Call to a member function trans() on a non-object in /var/www/MyBusiness/src/My/UserBundle/Handler/AuthenticationHandler.php
如何翻译错误消息?