Laravel Localization – The Laravel Localization is used to support different language to be used in application. You should create a separete directory for each supported language.
Laravel Localization.
Let us understand how to use laravel Localization.
Function:-
There are some followings function available in laravel Localization.
- 1. Introduction.
- 2. Defining Translation Strings.
- 3. Retrieving Translation Strings.
- 4. Overriding Package Language Files.
1. Introduction.
Laravel localization provide a simple way to access string in multiple languages and allowing you to easily support multiple languages within your application. All string are stored in the file within the resource/lang directory
Configuring The Locale
The default language for your application is stored in the config/app.php configuration file. You can modify this value to suit the needs of your application. You can also change the active language at run time using the setLocale method.
Route::get('welcome/{locale}', function ($locale) { App::setLocale($locale); // });
You can also configure a fallback language, the fallback language is also configure in the config/app.php configuration file.
'fallback_locale' => 'en',
Determining The Current Locale
You can use the getLocale and isLocale method on the app facade and also use for check the current locale and given value.
$locale = App::getLocale(); if (App::isLocale('en')) { // }
2. Defining Translation Strings.
Using Short Keys
Translation string are stored in resource/language directory. this directory there should be a subdirectory for each language.
/resources /lang /en messages.php /hi messages.php
All language file simply return an array of strings.
<?php return [ 'welcome' =?> 'Welcome To Tutorialsplane.com' ];
3. Retrieving Translation Strings.
You can retrieve lines from using _ helper function. The _ method accepts the file and key of the translation string as its first argument.
echo __('messages.welcome'); echo __('I love programming.');
If you are using blade templates. Then you may use {{ }} syntax.
{{ __('messages.welcome') }} @lang('messages.welcome')
Replacing Parameters In Translation Strings
If you want, you can define place-holder in your translation string. All place-holder are set with :.
'welcome' => 'Welcome, :name',
To replace the place-holders, you may pass the second argument.
echo __('messages.welcome', ['name' => 'Tutorialsplane']);
If the place-holders name contains all capital letter then a value of place-holders is also print capitalized. If the first letter is capitalized then output is same as it is.
'welcome' => 'Welcome, :NAME', // Welcome, TUTORIALSPLANE 'goodbye' => 'Goodbye, :Name', // Goodbye, Tutorialsplane
Pluralization
Pluralization is a complex problem, as different languages have a variety of complex rules for pluralization.
'apples' => 'There is one apple|There are many apples',
You may even create more complex pluralization rules.
'apples' => '{0} There are none|[1,19] There are some|[20,*] There are many',
You can use trans_choice function to retrieve the line for a given count.
echo trans_choice('messages.apples', 10);
4. Overriding Package Language Files.
Some packages may override with their own language file. Earlier of changing of packages core file to adjust these line. You can override them by placing file in the resources/lang/vender/{package}/{locale} directory.
If you need to override the English translation strings in message.php. You should place a language file at resources/lang/vender/hearthfire/en/message.php. you can only define the translation string which is you want.
Full Example of Localization:-
First we have to create three different folder in resources/lang directory. Like save english file at resources/lang/en/lang.php and put the code like this:-
<?php return [ 'msg' =?> 'Welcome To Tutorialsplane.' ]; ?>
save french file at resources/lang/fe/lang.php directory.
<?php return [ 'msg' =?> 'Bienvenue à Tutorialsplane.' ]; ?>
save german file at resources/lang/ge/lang.php directory.
<?php return [ 'msg' =?> 'Willkommen bei Tutorialsplane.' ]; ?>
Then create a controller called LocalizationController through Artsian command.
php artisan make:controller LocalizationController --plain
Controller Parts:-
Let’s look at a simple example.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; class LocalizationController extends Controller { public function index(Request $request,$locale) { app()-?>setLocale($locale); echo trans('lang.msg'); } } |
Routes Parts:-
Route::get('localization/{locale}','LocalizationController@index');
Path and Output
http://localhost:/path/localization/en
http://localhost:/path/localization/fe
http://localhost:/path/localization/ge