Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

scarneros's avatar

Cannot translate some Fortify and Inertia messages

Hello,

I'm trying to translate the following messages that reside inside Fortify and Inertia vendor files, but there's no way to achieve this from the application. Also, it looks like is not possible to publish them. assets.

/vendor/laravel/fortify/src/Http/Responses/FailedPasswordConfirmationResponse.php

<?php

namespace Laravel\Fortify\Http\Responses;

use Illuminate\Validation\ValidationException;
use Laravel\Fortify\Contracts\FailedPasswordConfirmationResponse as FailedPasswordConfirmationResponseContract;

class FailedPasswordConfirmationResponse implements FailedPasswordConfirmationResponseContract
{
    /**
     * Create an HTTP response that represents the object.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function toResponse($request)
    {
        $message = __('The provided password was incorrect.');

        if ($request->wantsJson()) {
            throw ValidationException::withMessages([
                'password' => [$message],
            ]);
        }

        return back()->withErrors(['password' => $message]);
    }
}

/vendor/laravel/jetstream/src/Http/Controllers/Inertia/OtherBrowserSessionsController.php

Any idea on how to achieve that? There are other untranslatable messages in Fortify or Inertia vendor files?

(I tried also with a xx.json file inside lang/xx folder, but not working anyway).

0 likes
2 replies
LaryAI's avatar
Level 58

To translate messages in Laravel Fortify and Jetstream, you can override the default behavior by customizing the response classes and using your own translation files. Here's how you can achieve this:

Step 1: Create Custom Response Classes

First, create custom response classes that will replace the default ones provided by Fortify and Jetstream.

  1. Create a Custom Response for Fortify:

    Create a new file in your application, for example, app/Http/Responses/CustomFailedPasswordConfirmationResponse.php:

    <?php
    
    namespace App\Http\Responses;
    
    use Illuminate\Validation\ValidationException;
    use Laravel\Fortify\Contracts\FailedPasswordConfirmationResponse as FailedPasswordConfirmationResponseContract;
    
    class CustomFailedPasswordConfirmationResponse implements FailedPasswordConfirmationResponseContract
    {
        public function toResponse($request)
        {
            $message = __('custom_messages.incorrect_password');
    
            if ($request->wantsJson()) {
                throw ValidationException::withMessages([
                    'password' => [$message],
                ]);
            }
    
            return back()->withErrors(['password' => $message]);
        }
    }
    
  2. Create a Custom Controller for Jetstream:

    Create a new file in your application, for example, app/Http/Controllers/Inertia/CustomOtherBrowserSessionsController.php:

Step 2: Register Custom Responses and Controllers

In your FortifyServiceProvider and JetstreamServiceProvider, register these custom classes.

  1. FortifyServiceProvider:

    use App\Http\Responses\CustomFailedPasswordConfirmationResponse;
    use Laravel\Fortify\Contracts\FailedPasswordConfirmationResponse;
    
    public function boot()
    {
        $this->app->singleton(FailedPasswordConfirmationResponse::class, CustomFailedPasswordConfirmationResponse::class);
    }
    
  2. JetstreamServiceProvider:

    Replace the default controller with your custom controller in the routes:

    use App\Http\Controllers\Inertia\CustomOtherBrowserSessionsController;
    
    Route::delete('/user/other-browser-sessions', [CustomOtherBrowserSessionsController::class, 'destroy'])
        ->name('other-browser-sessions.destroy');
    

Step 3: Add Custom Translation Messages

Create a custom translation file, for example, resources/lang/en/custom_messages.php:

<?php

return [
    'incorrect_password' => 'The password you entered is incorrect.',
];

Conclusion

By following these steps, you can customize and translate the messages in Fortify and Jetstream without modifying the vendor files. This approach ensures that your translations are maintained even after package updates.

scarneros's avatar

Sure the AI answer works, but I'm trying to find the recommended (or less destructive) way. Overwriting Fortify and Inertia classes does not sound right for some reason. I don't understand why these translations are not providing a key like others.

Please or to participate in this conversation.