2017-03-02 11 views
-1

我使用symfony 3.2,我想用ajax表單登錄。一切工作正常,除非消息沒有翻譯。使用ajax表單翻譯郵件登錄

這是AjaxAuthenticationFailureHandler類

class AjaxAuthenticationFailureHandler extends DefaultAuthenticationFailureHandler 
{ 
/** 
* @var mixed 
*/ 
private $translator; 

/** 
* @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel 
* @param \Symfony\Component\Security\Http\HttpUtils  $httpUtils 
* @param array            $options 
* @param \Psr\Log\LoggerInterface       $logger 
* @param mixed            $translator 
*/ 
public function __construct(HttpKernelInterface $httpKernel, HttpUtils $httpUtils, array $options, LoggerInterface $logger = null, $translator = null) 
{ 
    parent::__construct($httpKernel, $httpUtils, $options, $logger); 

    $this->translator = $translator; 
} 

/** 
* {@inheritDoc} 
*/ 
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) 
{ 
    if ($request->isXmlHttpRequest()) { 
     return new Response(json_encode(array(
      'has_error' => true, 
      'error'  => $this->translator->trans($exception->getMessage()) 
     ))); 
    } 

    return parent::onAuthenticationFailure($request, $exception); 
} 

回答

1

試着改變你的翻譯線這樣的:

$this->translator->trans($exception->getMessage(), array(), 'messages') 

然後在資源/翻譯文件夾中創建翻譯文件爲每種語言即messages.en.yml ,messages.fr.yml等...

你的$ exception-> getMessage()應該嘗試並返回翻譯鍵來簡化事物,例如它應該返回「error.name.missing」,然後在你的yml翻譯文件中:

error.name.missing: 'The name cannot be missing'