Multi Language API Response Messages In Laravel
When providing API Service, sometimes we need to set multiple language for handling response. For example, for servicing mobile apps that has multi language support, user needs to get proper active language message in their app. We do not want user who already set Indonesian as current app language but get an English language in response message.
How to Accomplish Dynamic Language?
Laravel provides multi language support. Simplest thing is define language using request header.
we can use X-localization header key to determine what language is used. Or in other approach we use X-localization attribute as URL query string. But in this case, I’ll recommend to use it as header request.
Language Setup
Lets take a look in lang folder under resources directory then create folder for our language and create file messages.php to assigned our messages. We may may create many language file depends on our available contexts.
In messages.php file, we simply returned array that contains our messages.
Under lang/en/messages.php
<?phpreturn [
‘greeting’ => ‘Hello world. This is using english.’];
Under lang/id/messages.php
<?phpreturn [
‘greeting’ => ‘Hello world. Ini menggunakan Bahasa Indonesia.’];
We’re done to setup our language here, so next we will create our middleware.
Create Language Middleware
Simply use artisan command:
php artisan make:middleware localization
Then we create our middleware,
<?phpnamespace App\Http\Middleware;use Closure;class localization
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// Check header request and determine localizaton
$local = ($request->hasHeader(‘X-localization’)) ? $request->header(‘X-localization’) : ‘en’; // set laravel localization
app()->setLocale($local); // continue request
return $next($request);
}
}
In this middleware, we are checking incoming request header and set app locale. We set default localizaton if incoming request has no header for localization, then we continue incoming request.
Created middleware must be registered to app/Http/Kernel.php inside variable routeMiddleware.
protected $routeMiddleware = [ /**
* other middlewares
* ...
*/ ‘localization’ => \App\Http\Middleware\localization::class,
];
Calling Middleware
We have registered localization middleware, then we should be able to call it in our route. Let’s create a simple controller to display response from our greeting message before.
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;class MessageController extends Controller
{
/**
* Show greetings
*
* @param Request $request [description]
* @return [type] [description]
*/
public function index(Request $request)
{
$data = [
‘message’ => trans(‘messages.greeting’)
]; return response()->json($data, 200);
}
}
Nice…
Our controller is handling request and simply returning json data with translation greeting message.
Then we create our route and use localization middleware.
// Routes/api.phpRoute::get(‘greeting’, ‘MessageController@index’)
->middleware(‘localization’);
Let’s check our greeting API using postman. First we try with English (en) localization then Indonesian (id).
Nicely working.
Other Tips
Calling localization middleware in each route will become inefficient especially when our route is hundreds or more large. We can simply register middleware in group so we don not need to explicitly register in each route.
Open app/Http/Kernel.php again then edit array middlewareGroups inside api:
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [ 'api' => [
'throttle:60,1',
'localization'
'bindings',
],
];
Then our localization is works.
Repository
I’ve wrapped this tutorial to github:
You may clone, run, or modify this project as you need.
Happy coding, Folks!