Package Data | |
---|---|
Maintainer Username: | sheepy85 |
Maintainer Contact: | sheepy85@gmail.com (sheepy85) |
Package Create Date: | 2014-10-17 |
Package Last Update: | 2014-10-30 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-22 15:03:58 |
Package Statistics | |
---|---|
Total Downloads: | 37 |
Monthly Downloads: | 1 |
Daily Downloads: | 0 |
Total Stars: | 2 |
Total Watchers: | 1 |
Total Forks: | 1 |
Total Open Issues: | 0 |
Simple Localization Middleware for Laravel 5 to make dynamic prefixed route names to the same controller actions.
Facade Alias : Locale
Step 1: Add to stack
// App\Http\Kernel.php
protected $stack = [
...
'Sheepy85\L5Localization\Middleware\Localization',
];
Step 2: Use routes.php file
// App\Providers\RouteServiceProvider.php
public function map( Router $router ) {
require app_path( 'Http/routes.php' ); // uncomment
}
Step 3: Add the ServiceProvider
// config/app.php
'providers' => [
...
'Sheepy85\L5Localization\L5LocalizationServiceProvider' ,
] ,
Step 4: Use file generator
> php artisan locale:select en es fr
to get the full list of Localization short codes use -l or --list
Example files in project folder.
Locale::locales()
looking for config/locale.php, set optional parameter to true
for raw url generation
Locale::router()
return Laravel Router instance and only accept [get, post, put, delete, patch]
methods
1 arg: If your routes lang file don't contain the route name, use as raw Uri 2 arg: Route name 3 arg: Action 4 arg: Localization short code
// routes.php
foreach ( Locale::codes( true ) as $code ) {
// or
//foreach ( Locale::locales( true ) as $code => $locale ) {
Route::group( [
'namespace' => '\App\Http\Controllers' ,
'prefix' => $code ,
] , function() use( $code ) {
Locale::router()->get( '/' , 'home' , 'HomeController@index' , $code );
Locale::router()->get( 'routes.news' , 'news' , 'HomeController@news' , $code );
Locale::router()->post( 'login' , 'login' , 'AuthController@login' , $code );
} );
}
Result:
GET|HEAD / | home | App\Http\Controllers\HomeController@index
GET|HEAD news | news | App\Http\Controllers\HomeController@news
POST login | login | App\Http\Controllers\AuthController@login
GET|HEAD en | en.home | App\Http\Controllers\HomeController@index
GET|HEAD en/news | en.news | App\Http\Controllers\HomeController@news
POST en/login | en.login | App\Http\Controllers\AuthController@login
GET|HEAD hu | hu.home | App\Http\Controllers\HomeController@index
GET|HEAD hu/hirek | hu.news | App\Http\Controllers\HomeController@news
POST hu/belepes | hu.login | App\Http\Controllers\AuthController@login
with helper to the session current locale:
{{ lroute('news') }}
or laravel's helper, notice need to comment out the original function in Illuminate\Foundation\helpers.php
file
{{ route('news') }}
has()
Determine localization statusget()
Get current localizationset( $locale )
Set current localizationflush()
Unset Session variableexist( $locale )
Determine localization adjustablenotExist( $locale )
Determine localization not adjustablefallback()
Set fallback localizationlocale()
Get current localizationname()
Get current localization english namenative()
Get current localization native namescript()
Get current localization code scriptdirection()
Get current localization read directionlocales( $withNull = false )
Get available localizations, optional empty first rowcodes( $withNull = false )
Get available localization short codes, optional empty first row