Download the PHP package cariboufute/locale-route without Composer
On this page you can find all versions of the php package cariboufute/locale-route. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cariboufute/locale-route
More information about cariboufute/locale-route
Files in cariboufute/locale-route
Package locale-route
Short Description A testable route package with localization for Laravel 5 to 10
License MIT
Homepage https://github.com/cariboufute/locale-route
Informations about the package locale-route
LocaleRoute, for localized testable routes in Laravel
LocaleRoute is a package to make testable localized routes with Laravel 5 to 10. It comes from the need to have localized routes that are fully testable.
LocaleRoute has a syntax close to the original Laravel routing methods, so the installation and learning curve should be quite easy.
For an example of LocaleRoute implementation, please check my locale-route-example repo.
Change log
Please see changelog for more information what has changed recently and what will be added soon.
Requirements
Please choose your version of LocaleRoute according to your version of Laravel.
Laravel | LocaleRoute |
---|---|
8.0 - 10.X | 3.* |
5.5 - 7.X | 2.* |
5.1 - 5.4 | 1.* |
Install
First install the package through Composer by typing this line in the terminal at the root of your Laravel application.
For Laravel 5.4 and earlier
Since Laravel 5.5, Package Discovery installs service provider and alias automatically. But if you have Laravel 5.4 and earlier, add the service provider and the alias in .
Middleware
In your file, add the middleware in the web middleware group. This will read the locale from the session variable, saved by each localized route and will keep the locale for redirections, even after using unlocalized routes to access models CRUD routes, for instance.
Config file
Finally install the config file of the package by typing this line in the terminal at the root of your Laravel application.
To bypass the package selection prompt, type this line instead.
Then you should have a installed.
Configuration
Check your file. Here is the default file.
Locales
Add all the locale codes needed for your website in .
For instance, if you want English, French, Spanish and German in your site...
Add locale automatically to URLs
This option is by default set to true. It prepends all URLs build by locale-route with a , according to Google Multi-regional and multilingual sites guidelines.
If for any reason, you don't want this prefix to be added automatically, just put this option to false, like this.
Usage
Adding routes
Adding localized routes is now really easy. Just go to your file (or in older versions of Laravel) and add declarations almost like you would declare Laravel methods.
For the first line, it is the equivalent of declaring this in pure Laravel, while having the app locale set to the right locale.
You can also give a string as locale URL if it is the same for all locales
Now, trailing methods are supported with LocaleRoute, as with the original Laravel Route facade.
So the syntax can be resumed to this.
Using translator files for URLs
You can also use the Laravel translator to put all your locale URLs in files. If there is no locale URL array, will automatically check for the translated files to find URLs. All you need to do is to remove the locale URL array in and declare them as in your translated route files, like this.
Note about localized and unlocalized routes using same base URL
If you declare localized and unlocalized routes using the same base URL, please declare your LocaleRoute method before the Route method. If you don't, the normal route will be discarded by the locale route attribution process.
For instance, if you declare a normal route with to redirect to the fallback locale (for instance, ) before the localized routes (for instance, and ), the localized routes with replace the first route before being added the locale. Declaring locale routes before the normal unlocalized route will cause no problems.
Middleware
If you want to use middleware for your LocaleRoute, add them in the url array (3rd parameter) in the key.
Grouping
You can use the methods inside normal methods.
Resource
To add a localized RESTful resource, just use with the same syntax as . This will give localized routes for all GET/HEAD routes and will keep the POST/PUT/PATCH/DELETE routes unlocalized.
If you want to translate the create and edit words in resources routes URL, add route-labels.php lang files in the resources/lang folder with translation for create and edit.
API resource
You can also use for the localized version of , which corresponds to without the and pages. The translated route labels will be the same as for .
Overriding options
You can override the and config options simply by declaring them in the url array.
Fetching URLs
gives three helper functions to help you get your URLs quickly. They are close to the Laravel helper function.
locale_route
This is the basic helper function. It calls the URL according to the locale, route name and parameters. When put to null, locale and route are set to the current values.
For the last three situations, there are clearer helper functions.
other_route
Calls another route URL in the same locale. The syntax is the same as Laravel .
other_locale
Calls the same route URL in another locale. For the syntax, we just replace the route name by the locale. Perfect for language selectors.
Contributing
Please see conduct for details.
Credits
- Frédéric Chiasson
- All Contributors
License
The MIT License (MIT). Please see License File for more information.