
Good content takes time and effort to come up with.
Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.
Recently I wanted to support multiple language responses in my Laravel REST API project. So thought that it would be a good go for the Localization feature of Laravel.
Let me walk you through the process of how I achieved it.
You can find the code in my GitHub repository - Laravel Sanctum
composer create-project --prefer-dist laravel/laravel laravel-localization
If you want to name the locales as per standards then change the following settings under.
config/app.php
'locale' => 'en-US',
'fallback_locale' => 'en-US',
locale
- Default locale is set to en-US
fallback_locale
- If the locale in header passed in not found under /resources/lang/ folder then it will fallback to en-US
Laravel gives a fluent way to handle multiple languages in your project. Let's see how we can structure it in our project.
/laravel-localization /resources /lang /en-US auth.php /fr-FR auth.php
All the English auth messages will go in /resources/lang/en-US
, French in /resources/lang/fr-FR
. You can create as many as language translations.
TIP: Make sure that the /resources/lang folder (en-Us, fr-FR) must match config/app.php locale & fallback_locale.
Let's see what will go in our translations messages.
/resources/lang/en-Us/auth.php
<?php
return [
/** Login */
"blocked" => "You have been blocked.",
"email_not_verified" => "Your email is not verified.",
"failed" => "Invalid email or password.",
"success" => "User authenticated successfully.",
"throttle" => "Too many login attempts. Please try again in :seconds seconds.",
/** Logout */
"logout_success" => "User logged out successfully.",
];
/resources/lang/fr-FR
<?php
return [
/** Login */
"blocked" => "Vous avez été bloqué.",
"email_not_verified" => "Votre email n'est pas vérifié.",
"failed" => "Email ou mot de passe invalide.",
"success" => "L'utilisateur s'est authentifié avec succès.",
/** Logout */
"logout_success" => "L'utilisateur s'est déconnecté avec succès.",
];
Following the standard, you will come across the Accept-Language
header. You might also use like X-Language but it is an unorthodox specification.
Every time a REST API request is established then you have to add the header Accept-Language
NOTE: I have added Postman examples at the end of the article.
Let's create a middleware so that we can handle the Accept-Languge
headers before it reaches our application code with a proper fallback
php artisan make:middleware Localization
App/Http/Kernel.php
protected $routeMiddleware = [
//...
'localization' => Localization::class,
];
App / Http / Middleware / Localization.php
public function handle(Request $request, Closure $next)
{
/**
* requests hasHeader is used to check the Accept-Language header from the REST API's
*/
if ($request->hasHeader("Accept-Language")) {
/**
* If Accept-Language header found then set it to the default locale
*/
App::setLocale($request->header("Accept-Language"));
}
return $next($request);
}
NOTE: You can use App::getLocale()
to know which locale you are currently using anywhere in your application.
api.php
Route::middleware("localization")->group(function () {
Route::group(["prefix" => "auth", "as" => "auth."], function () {
Route::post("/authenticate", [LoginController::class, "authenticate"])->name("authenticate");
//... other routes
});
Route::middleware("auth:sanctum")->group(function () {
//... other routes
});
});
You can display any message with the help of the following way.
__("filename.message_name");
filename
-> from which file you want to use
message_name
-> which message key do you want to access
As we have already set the locale in Localization middleware via Accept-Language
. The messages will be displayed appropriately.
if ($user->is_blocked) {
throw new UserIsBlockedException(__("auth.blocked"));
}
if (!$user->is_email_verified) {
throw new UserEmailNotVerifiedException(__("auth.email_not_verified"));
}
if (!Hash::check($request->get("password"), $user->password)) {
throw new Exception(__("auth.failed"));
}
public function logout()
{
request()->user()->tokens()->delete();
return response()->json([
"status" => "success",
"message" => __("auth.logout_success"),
], Response::HTTP_OK);
}
Why And How To Use PHP PDO With CRUD Examples
Securely Connect Server MYSQL DB From Sequel Pro / MYSQL Workbench
Google reCAPTCHA Integration In PHP Laravel Forms
What Is Composer? How Does It Work? Useful Composer Commands And Usage
composer.json v/s composer.lock
Send Email In PHP With PHPMailer
Free Live Chat Integration Using TAWK.IO
Push Files To CPanel / Remote Server using FTP Software FileZilla
Upload File From Frontend Server {GuzzleHttp} To REST API Server In PHP {Laravel}
Free SSL Certificate For CPanel
Client-Side DataTable, Adding Super Powers To HTML Table
Manipulate HTML Using DOMDocument In PHP