Localization is a key part of modern web apps, especially if your users are from different parts of the world and speak different languages. When you make APIs with Laravel, you often need to send back responses in more than one language, depending on what the user wants. Laravel has a great localization system that makes it simple to translate the content of your app.
Using middleware is one of the best ways to add REST API localization to Laravel. Middleware lets you stop incoming API requests, check for language preferences, and set the application language on the fly before the request gets to the controller.
In this guide, we'll learn how to use middleware to make Laravel API localization work so that API responses can be translated into many different languages.
Why API Localization is Important
API localization makes it easier for apps to work with people from different countries. When your API sends back responses in the language the user wants, it makes things easier to use and improves the overall experience.
For instance, an app that serves people in the US, France, and Pakistan might need to respond in English, French, and Urdu. You can make sure that all messages, validation errors, and responses are automatically translated by using localization at the API level.
Laravel makes this easy with its built-in language files and tools for localization.
Step 1: Creating Language Middleware
The first thing you need to do to make Laravel APIs work with different languages is to make a custom middleware that will figure out what language the API request is in.
We will make a middleware called ApiLocalization in this example.
In your Laravel project, run this Artisan command:
php artisan make:middleware for ApiLocalization
When you run the command, Laravel will make a new middleware file in this folder:
app/Http/Middleware/ApiLocalization.php
This middleware will read the language preference from the request header and set the application's locale to match.
Step 2: Update the ApiLocalization Middleware
Next, open the new middleware file and change the logic so that it looks for a request header called X-localization.
This header lets API clients say what language they want the response to be in.
For example:
public function handle($request, Closure $next)
{
$local = ($request->hasHeader('X-localization'))
? $request->header('X-localization')
: 'en';
app()->setLocale($local);
return $next($request);
}
How This Code Functions
The middleware does three simple things:
- It checks to see if the X-localization header is present in the API request.
- It gets the language code from the request if the header is there.
- If there is no header, it will automatically be set to English (en).
The middleware sets the application locale after figuring out the language.
setLocale($local);
This makes sure that Laravel uses the language you chose when sending back translated messages.
Step 3: Sign up for the Middleware
After making and setting up the middleware, the next step is to register it so that Laravel can use it to make API calls.
Please open the following file:
app/Http/Kernel.php
Put the new middleware in the $routeMiddleware array:
'api.localization' => \App\Http\Middleware\ApiLocalization::class,
You can now use the middleware in routes.
Step 4: Use Middleware on API Routes
After registering the middleware, you need to apply it to your API routes.
Open the routes/api.php file and wrap your routes with the localization middleware.
Example:
Route::middleware(['api.localization']) ->group(function () {
Route::get('/message', function () {
return response()->json([
'message' => __('messages.welcome')
]);
});
});
The ApiLocalization middleware will now handle every request to this route. It will set the language before the response is made.
Step 5: Make Language Files
Laravel stores translations in language files. You can find these files in the resources/lang folder.
Make language folders like this:
resources/lang/en/messages.php
resources/lang/fr/messages.php
resources/lang/ur/messages.php
Example English translation:
return [
'welcome' => ' Welcome to our API'
];
Example French translation:
return [
'welcome' => 'Bienvenue dans notre API'
];
Example Urdu translation:
return [
'welcome' => 'ہماری API میں خوش آمدید'
];
Now Laravel will automatically return the translated message based on the selected locale.

Using middleware to add REST API localization to Laravel is a simple and effective way to support multiple languages. You can easily translate all API responses by making your own middleware, looking for a language header, and changing the application's locale on the fly.
Laravel's powerful localization system and middleware make it possible for developers to create APIs that can be used by people from all over the world, no matter what language they speak or where they live.
No comments:
Post a Comment