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.
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.
‘greeting’ => ‘Hello world. This is using english.’
‘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,
* 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
// continue 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,
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.
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.
Let’s check our greeting API using postman. First we try with English (en) localization then Indonesian (id).
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' => [
Then our localization is works.
I’ve wrapped this tutorial to github:
laravel-multilanguage-api - Sample laravel multilanguage API responsegithub.com
You may clone, run, or modify this project as you need.
Happy coding, Folks!