私の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
エラーメッセージを翻訳するにはどうすればよいですか?