sheepy85 / l5-localization
Simple Localization Middleware for Laravel 5
Requires
- php: >=5.4.0
- illuminate/support: 5.0.*
This package is not auto-updated.
Last update: 2024-11-19 02:34:02 UTC
README
Simple Localization Middleware for Laravel 5 to make dynamic prefixed route names to the same controller actions.
Features
- Seo friendly url generation: example.com/news, example.com/en/news, example.com/es/noticias, example.com/fr/nouvelles
- Session stored Localization data.
- Locale prefix Route names
- Lang file stored Route url with model name
- Route Cache
Facade Alias : Locale
Setup
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
Examples
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
Generate route:cache for all of your languages
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
make Url
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') }}
Api Locale::
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