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.
-
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]); } } -
Create a Custom Controller for Jetstream:
Create a new file in your application, for example,
app/Http/Controllers/Inertia/CustomOtherBrowserSessionsController.php:<?php namespace App\Http\Controllers\Inertia; use Illuminate\Contracts\Auth\StatefulGuard; use Illuminate\Http\Request; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\DB; use Illuminate\Validation\ValidationException; use Laravel\Fortify\Actions\ConfirmPassword; class CustomOtherBrowserSessionsController extends Controller { public function destroy(Request $request, StatefulGuard $guard) { $confirmed = app(ConfirmPassword::class)( $guard, $request->user(), $request->password ); if (! $confirmed) { throw ValidationException::withMessages([ 'password' => __('custom_messages.incorrect_password'), ]); } $guard->logoutOtherDevices($request->password); $this->deleteOtherSessionRecords($request); return back(303); } protected function deleteOtherSessionRecords(Request $request) { if (config('session.driver') !== 'database') { return; } DB::connection(config('session.connection'))->table(config('session.table', 'sessions')) ->where('user_id', $request->user()->getAuthIdentifier()) ->where('id', '!=', $request->session()->getId()) ->delete(); } }
Step 2: Register Custom Responses and Controllers
In your FortifyServiceProvider and JetstreamServiceProvider, register these custom classes.
-
FortifyServiceProvider:
use App\Http\Responses\CustomFailedPasswordConfirmationResponse; use Laravel\Fortify\Contracts\FailedPasswordConfirmationResponse; public function boot() { $this->app->singleton(FailedPasswordConfirmationResponse::class, CustomFailedPasswordConfirmationResponse::class); } -
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.