chinleung / laravel-multilingual-routes
A package to register multilingual routes.
Installs: 111 289
Dependents: 2
Suggesters: 0
Security: 0
Stars: 395
Watchers: 10
Forks: 25
Open Issues: 5
Requires
- php: ^8.1
- chinleung/laravel-locales: ^2.0
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^9.5.10|^10.5
- dev-master
- v4.x-dev
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.1
- v4.0.0
- v3.x-dev
- v3.0.1
- v3
- v2.x-dev
- v2.8.2
- v2.8.1
- v2.8.0
- v2.7.5
- v2.7.4
- v2.7.3
- v2.7.2
- v2.7.1
- v2.7.0
- v2.6.0
- v2.5.1
- v2.5.0
- v2.4.1
- v2.4.0
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.1
- v2.0.0
- v1.x-dev
- v1.10.0
- v1.9.0
- v1.8.1
- v1.8.0
- v1.7.1
- v1.7.0
- v1.6.0
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.0
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.2
- v1.0.1
- v1.0.0
This package is auto-updated.
Last update: 2024-10-10 20:47:13 UTC
README
A package to register multilingual routes for your application.
Versioning
Installation
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
. It must be the first item in the web
middleware group.
protected $middlewareGroups = [ 'web' => [ \ChinLeung\MultilingualRoutes\DetectRequestLocale::class, // ... ] ];
Configuration
By default, the application locales are only going to be en
and the default locale is not prefixed. If you want to prefix the default locale, please run the following command to publish the configuration file:
php artisan vendor:publish --provider="ChinLeung\MultilingualRoutes\MultilingualRoutesServiceProvider" --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.
Example
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');
A demo repository has been setup to showcase the basic usage of the package.
Usage
Quick Usage
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:
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
To retrieve the current route in another locale, you can use the current_route(string $locale = null)
helper:
current_route(); // Returns the current request's route current_route('fr'); // Returns the current request's route in French version current_route('fr', route('fallback')); // Returns the fallback route if the current route is not registered in French
Renaming the routes
Route::multilingual('test', 'TestController')->name('foo');
Renaming a route based on the locale
Route::multilingual('test', 'TestController')->names([ 'en' => 'foo', 'fr' => 'bar', ]);
Skipping a locale
Route::multilingual('test', 'TestController')->except(['fr']);
Restricting to a list of locales
Route::multilingual('test', 'TestController')->only(['fr']);
Changing the method of the request
Route::multilingual('test', 'TestController')->method('post');
Registering a view route
// Loads test.blade.php Route::multilingual('test');
Registering a view route with a different key for the route and view
// Loads welcome.blade.php instead of test.blade.php Route::multilingual('test')->view('welcome');
Passing data to the view
Route::multilingual('test')->data(['name' => 'Taylor']); Route::multilingual('test')->view('welcome', ['name' => 'Taylor']);
Redirect localized route
Route::multilingual('support'); Route::multilingual('contact')->redirect('support');
Check localized route
Request::localizedRouteIs('home');
Signing localized routes
URL::signedLocalizedRoute('unsubscribe', ['user' => 1]); URL::temporarySignedLocalizedRoute('unsubscribe', now()->addMinutes(30), ['user' => 1]);
Upgrading from 1.x to 2.x
To update from 1.x to 2.x, you simply have to rename the namespace occurrences in your application from LaravelMultilingualRoutes
to MultilingualRoutes
. The most common use case would be the DetectRequestLocale
middleware.
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email hello@chinleung.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.