Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9224333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:17:37+00:00 2026-06-18T04:17:37+00:00

I would like to translate in my ajax login error messages. I am using

  • 0

I would like to translate in my ajax login error messages.
I am using FOSUserBundle, and I would take advantage of the translation files I properly overwritten folder Resources/translations.

My 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 {
        //...
    }
}

}

But return this error:

Fatal error: Call to a member function trans() on a non-object in /var/www/MyBusiness/src/My/UserBundle/Handler/AuthenticationHandler.php

How do I translate the error messages?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:17:38+00:00Added an answer on June 18, 2026 at 4:17 am

    $request->get(string $key, mixed $default = null, type $deep = false) is for getting parameters from GET, PATH, POST, COOKIE. You are trying to get a “translator” parameter, which probably doesn’t exist. So null is returned and you call trans() on a non-object.

    Simply inject the “translator” service in your AuthenticationHandler (in the constructor, probably). Or get it from the service container, as it’s available in your class:

    $translator = $this->service_container->get('translator');
    
    // ...
    $result = array(
        'success' => false,
        'message' => $translator->trans($error, array(), 'FOSUserBundle')
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use FOSUserBundle login form and i would like translate errors messages. These messages
I would like to automatically pre-translate a bunch of text files that I have
I am making an app that would translate websocket messages to AJAX requests to
I would like to translate or 301-redirect urls such as: www.domain.com/example.html to www.domain.com/example Here
I've got some code which I would like to translate into Opengl ES. I'm
Would like someone to take a look at my script and tell me where
I would like to translate my minimalistic server from Coffeescript to Typescript. Here's the
I would like to translate a few hundred words for an application I'm writing.
I would like to translate the following Unix 1 Liner to PowerShell. Synopsis of
I have the string aa{{{a {{ {aaa{ that I would like to translate to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.