Hi Everybody,
In this blog we will focus on how to implement multilingual in API response messages in laravel/lumen.
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.
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.
In this blog we will focus on how to implement multilingual in API response messages in laravel/lumen.
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
<?php
return [
‘greeting’ => ‘Hello world. This is using english.’
];
Under lang/id/messages.php
<?php
return [
‘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,
<?php
namespace 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.
In Laravel
protected $routeMiddleware = [
/**
* other middlewares
* ...
*/
‘localization’ => \App\Http\Middleware\localization::class,
];
in Lumne
$app->routeMiddleware([
'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.
<?php
namespace 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);
}
}
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.php
Route::get(‘greeting’, ‘MessageController@index’)
->middleware(‘localization’);
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 middlewareGroupsinside api:
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'api' => [
'throttle:60,1',
'localization'
'bindings',
],
];
Then our localization is works.
Let’s check our greeting API using postman. First we try with English (en)localization then Indonesian (id).
Good job! For Sharing your best ideas... I will refer the people to best learning and training Institute for online courses... click the below link:
ReplyDeleteshort courses in pakistan
react native advanced concepts
sorting algorithms c++
graphic designing institute
mern stack course online
php training institute
e commerce online course in pakistan
Online short courses in Pakistan
Tech Inn Solutions
Good Job! You have Shared your well knowledge... I will refer the people to the best IT Solutions providers click the below link:
ReplyDeleteit solutions website
pakistan web developer
app development in pakistan
software developers in pakistan
digital marketing in pakistan
website maintenance packages
professional it consulting services
Cre8tivebot
Software Development in Pakistan