Laravel localization (trans helper) tutorial example

0
108
Laravel-localization-tutorial.pung



The first larval 5 larvae present localization for application. Localization is a very beautiful contradictory theme added on the larva. Localization We can manage multiple language messages. I mean you can manage multiple languages ​​in your application. For example you have a title like “Home Page”, it’s a static title and now it’s in English, but you also manage other languages ​​like if this site opens in Spanish you will like the name: “Pagina De Inikio “.

So, larvae provide multi-language messages in 5 files. You can easily do this using the “trans” assistant. You can easily do this in your larva application because the larvae provide the file inside

Resources / Lang.

In Resources / Lang The directory has different folders for different languages, you can create a new folder for your language. I am going to give you a simple example for localization. So make first Resource / lang / en / message. FP And put bellow code in that file.

Resource / lang / en / message. FP

return [

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

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

];

Okay, now we created the messages.php file for english langauge, this file is used to retrieve the English language message. Now we can add our own language messages to create new folders. So I am going to create an “es” folder for the Spanish language message. So, let’s create a new “es” folder Resources / Lang. Now make first Resource / lang / s / message. FP And put bellow code in that file.

Resource / lang / s / message

return [

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

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

];

Okay, now we’re ready to look at the Trans Helper, the route or controller file where you want it. Now I am going to give you an example in the view file.

welcome.blade.php

@extends('layouts.app')


@section('content')

<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>

</div>

</div>

@endsection

In the example above you can use “App :: setLocale”. We can define this function in the run time language. So in the output you can see the message first English and then Spanish.

You can also define the default language “Spanish” by app.php. Open if you want to change config / app.php And name locale = language folder. You can also set the “fallback_locale” language message. fallback_locale means that if you do not have a message on the current set language then it will be taken from this language by default. You can see the bellow configuration.

config / app.php

[

.....

'locale' => 'en',

......

'fallback_locale' => 'en',

.....

]

So, let’s start applying to our larval application, it’s really cool …

.

LEAVE A REPLY

Please enter your comment!
Please enter your name here