Laravel Localization(trans helper) tutorial example

Laravel Localization(trans helper) tutorial example

In this post we will give you information about Laravel Localization(trans helper) tutorial example. Hear we will give you detail about Laravel Localization(trans helper) tutorial exampleAnd how to use it also give you demo for it if it is necessary.

First Laravel 5 introduce localization for laravel application. Localization is very pretty intrsting topic added on laravel 5. Localization through we can manage multiple language message. i mean you can manage multiple languages within your application. for example you have one title like “Home Page”, this is static title and now it is in english, but you also manage other language like if this site open in spanish then name will like : “Página de inicio”.

So, laravel 5 provide to multi language messages stoare in file. you can do it easily using “trans” helper. you can do in your laravel application easily because laravel provide file in

See also  Laravel 7/6 Notification Tutorial | Create Notification with Laravel 7/6

resources/lang.

In resources/lang directory have diffrence folder for various languages, you can create new folder for you own language. i am going to give you simple example for localization. so first create resources/lang/en/messages.php and put bellow code in that file.

resources/lang/en/messages.php

return [

'title' => 'Home Page Title - English',

'heading' => 'Home Page Heading - English',

];

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

resources/lang/es/messages.php

return [

'title' => 'Home Page Title - Spanish',

'heading' => 'Home Page Heading - Spanish',

];

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


welcome.blade.php

@extends('layouts.app')


@section('content')

<div >

<div >

<h1 >{{ trans('messages.heading') }}</h1>

<?php App::setLocale('es'); ?>

<h1 >{{ trans('messages.heading') }}</h1>

</div>

</div>

@endsection

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.

config/app.php

[

.....

'locale' => 'en',

......

'fallback_locale' => 'en',

.....

]

So, let’s start to implement on your laravel application, it is really good one…

Hope this code and post will helped you for implement Laravel Localization(trans helper) tutorial example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

3  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US