Developer Snippet Diary

localization, multilanguage

Make site to multiple languages

1. make resources/lang/{langnamefolder} ie en

make any file here and create an array ie lang/en/home.php

<?php
	return[
		"welcome" =>" welcom to laravel",
		"sidebar" =>"sidebar of laravel",
	]
?>

now make an folder lang/ur/home.php

<?php
	return[
		"welcome" =>"??????? ??? ??? ??????",
		"sidebar" =>"??????? ????????",
	]
?>

2. Make locale.blade.php

<h1> {{__('home.sidebar')}} </h1>
<h3>{{__('home.welcome')}}</h3>

__() function used for translation home is file name and sidebar is array key

3. Make route

use Illuminate\Support\Facades\App;
Route::get('locale/{lang}',function($locale){
	App::setLocale($locale); //language is passed in url
	return view('locale');
});

Facade is a class that provides access to an object from the container. we use App face and then set locale using App facade. Here facade is used to set language

Now run url 

http://127.0.0.1:8000/locale/ur

http://127.0.0.1:8000/locale/en

Posted by: R GONDAL
Email: rizikmw@gmail.com