Package Data | |
---|---|
Maintainer Username: | ChinLeung |
Maintainer Contact: | hello@chinleung.com (Chin Leung) |
Package Create Date: | 2019-07-29 |
Package Last Update: | 2024-11-19 |
Home Page: | https://github.com/chinleung/laravel-multilingual-routes-demo |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-19 03:07:42 |
Package Statistics | |
---|---|
Total Downloads: | 113,054 |
Monthly Downloads: | 4,349 |
Daily Downloads: | 163 |
Total Stars: | 400 |
Total Watchers: | 10 |
Total Forks: | 25 |
Total Open Issues: | 4 |
A package to register multilingual routes for your application.
You can install the package via composer:
composer require chinleung/laravel-multilingual-routes
To detect and change the locale of the application based on the request automatically, you can add the middleware to your app/Http/Kernel
:
protected $middlewareGroups = [
'web' => [
\ChinLeung\LaravelMultilingualRoutes\DetectRequestLocale::class,
// ...
]
];
By default, the application locales are going to be en
and the app.fallback_locale
is not prefixed. If you want to prefix the fallback locale, please run the following command to publish the configuration file:
php artisan vendor:publish --provider="ChinLeung\LaravelMultilingualRoutes\LaravelMultilingualRoutesServiceProvider" --tag="config"
If your application supports different locales, you can either set a app.locales
configuration or follow the configuration instructions from chinleung/laravel-locales.
Instead of doing this:
Route::get('/', 'ShowHomeController')->name('en.home');
Route::get('/fr', 'ShowHomeController')->name('fr.home');
You can accomplish the same result with:
Route::multilingual('/', 'ShowHomeController')->name('home');
Once you have configured the locales, you can start adding routes like the following example in your routes/web.php
:
Route::multilingual('test', 'TestController');
This will generate the following:
| Method | URI | Name | Action | |----------|---------|---------|-------------------------------------| | GET|HEAD | test | en.test | App\Http\Controllers\TestController | | GET|HEAD | fr/teste | fr.test | App\Http\Controllers\TestController |
Note the URI
column is generated from a translation file located at resources/lang/{locale}/routes.php
which contains the key of the route like the following:
<?php
// resources/lang/fr/routes.php
return [
'test' => 'teste'
];
To retrieve a route, you can use the localized_route(string $name, array $parameters, string $locale = null, bool $absolute = true)
instead of the route
helper:
localized_route('test'); // Returns the url of the current application locale
localized_route('test', [], 'fr'); // Returns https://app.test/fr/teste
localized_route('test', [], 'en'); // Returns https://app.test/test
Route::multilingual('test', 'TestController')->name('foo');
| Method | URI | Name | Action | |----------|---------|--------|-------------------------------------| | GET|HEAD | test | en.foo | App\Http\Controllers\TestController | | GET|HEAD | fr/teste | fr.foo | App\Http\Controllers\TestController |
Route::multilingual('test', 'TestController')->names([
'en' => 'foo',
'fr' => 'bar',
]);
| Method | URI | Name | Action | |----------|---------|--------|-------------------------------------| | GET|HEAD | test | en.foo | App\Http\Controllers\TestController | | GET|HEAD | fr/teste | fr.bar | App\Http\Controllers\TestController |
Route::multilingual('test', 'TestController')->except(['fr']);
| Method | URI | Name | Action | |----------|---------|---------|-------------------------------------| | GET|HEAD | test | en.test | App\Http\Controllers\TestController |
Route::multilingual('test', 'TestController')->only(['fr']);
| Method | URI | Name | Action | |----------|---------|---------|-------------------------------------| | GET|HEAD | fr/teste | fr.test | App\Http\Controllers\TestController |
Route::multilingual('test', 'TestController')->method('post');
| Method | URI | Name | Action | |--------|---------|---------|-------------------------------------| | POST | test | en.test | App\Http\Controllers\TestController | | POST | fr/teste | fr.test | App\Http\Controllers\TestController |
// Loads test.blade.php
Route::multilingual('test');
| Method | URI | Name | Action | |----------|---------|---------|-------------------------------------| | GET|HEAD | test | en.test | Illuminate\Routing\ViewController | | GET|HEAD | fr/teste | fr.test | Illuminate\Routing\ViewController |
// Loads welcome.blade.php instead of test.blade.php
Route::multilingual('test')->view('welcome');
| Method | URI | Name | Action | |----------|---------|---------|-------------------------------------| | GET|HEAD | test | en.test | Illuminate\Routing\ViewController | | GET|HEAD | fr/teste | fr.test | Illuminate\Routing\ViewController |
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email hello@chinleung.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the Laravel Package Boilerplate.