Home Laravel Laravel Localization(trans helper) tutorial

Laravel Localization(trans helper) tutorial

by Shahriar Sagor

Today now in this post i want to show you about laravel localization(trans helper) . We know At First Laravel 5 introduce localization for laravel application. Localization is very pretty interesting topic include on laravel. By using Localization we can manage our multiple language message. i mean if we want manage multiple languages within our application. For example we have one title like “Home Page”, and this is static title and now it is in english, but you also manage other language as like if this site open in bangla then name will like : “হোম”.

So, laravel provide to multi language messages store in file. we can do this easily by using “trans” helper. we can do in our laravel application easily because laravel provide file in location


So In resources/lang directory we have difference folder for various languages, we can create a new folder for our own language. Now i am going to give you a simple example for localization.

so at first need to create resources/lang/en/messages.php and need to put bellow code in that file.


return [
    'title' => 'Home Page Title - English',
    'heading' => 'Home Page Heading - English',

Okay good, now we need to create messages.php file for english langauge, now on this file is use for retrieve english language message. okay now we can add over own language messages using the create new folder. so i am going to create “es” folder for bangla language message. so, let’s create the new “es” folder in resources/lang. now at first need to create resources/lang/es/messages.php and now need to put bellow code in that file.


return [
    'title' => 'Home Page Title - Bangla',
    'heading' => 'Home Page Heading - Bangla',

Ok, now we are ready for use trans helper in view, route or controller file we can use every where we want. now i am going to give you example in my view file.



<div class="row">
    <div class="col-md-8">
        <h1 class="text-primary" >{{ trans('messages.heading') }}</h1>
    	<?php App::setLocale('es'); ?>
        <h1 class="text-primary" >{{ trans('messages.heading') }}</h1>

In above example you can i use “App::setLocale” this facade function through we can define run time language. so in output you can see first english and then spanish message.

You can also define default language “spanish” by change app.php. If you want to change then open config/app.php and give locale = language folder name. You can also set “fallback_locale” language message. fallback_locale means if you have no message on current set language then it will take by default take from this language. you can see bellow configration.


	'locale' => 'en',
	'fallback_locale' => 'en',

Read Also : How to convert Object into Array in PHP?

Thanks for read. I hope it help you. For more you can follow us on facebook.


You may also like