benjamincrozat / laravel-polyglot
Switch between multiple languages on your Laravel application, the easy way.
Requires
- php: ^7.4
- illuminate/contracts: ^7.0
- illuminate/support: ^7.0
Requires (Dev)
- orchestra/testbench: ^5.0
- phpunit/phpunit: ^8.5
README
Laravel Polyglot
Switch between multiple languages on your Laravel application, the easy way.
Notes
- The tests suite can be found here
Requirements
- PHP 7.1+
- Laravel 5.8+ (it should run fine on previous versions too)
Installation
composer require benjamincrozat/laravel-polyglot
Publish the config file and edit it to your convenience (see the available drivers):
php artisan vendor:publish --provider="BC\Laravel\Polyglot\PolyglotServiceProvider" --tag=polyglot-config
Once it's done, register Laravel Polyglot's service provider before RouteServiceProvider
in config/app.php
:
'providers' => [ ... BC\Laravel\Polyglot\PolyglotServiceProvider::class, App\Providers\RouteServiceProvider::class, ... ]
Then, go to app/Providers/RouteServiceProvider.php
and add the prefix()
method in case you are using the directories
driver. If you're not, the prefix won't have any effect.
protected function mapWebRoutes() { Route::prefix(\Polyglot::prefix()) ->middleware('web') ->namespace($this->namespace) ->group(base_path('routes/web.php')); }
Show a language switcher
Laravel Polyglot provides a basic language switcher that you can include in your Blade templates.
@include('polyglot::switcher')
Search Engines Optimizations
https://support.google.com/webmasters/answer/189077
<head> @include('polyglot::links') </head>
Customize the views
php artisan vendor:publish --provider="BC\Laravel\Polyglot\PolyglotServiceProvider" --tag=polyglot-views
Drivers
query_string
- The user is automatically redirected to his / her language (this behavior can be disabled in the config);
- If the user choose another language, his / her preference is saved in a cookie.
https://foo.com?language={language}
directories
domains
https://{language}.foo.com or https://foo.com and https://bar.com