Download the PHP package dartmoon/laravel-localized-routes without Composer
On this page you can find all versions of the php package dartmoon/laravel-localized-routes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dartmoon/laravel-localized-routes
More information about dartmoon/laravel-localized-routes
Files in dartmoon/laravel-localized-routes
Package laravel-localized-routes
Short Description Localize laravel routes and views
License MIT
Informations about the package laravel-localized-routes
Laravel Localized Routes
A simple package to localize the routes of Laravel. This pakage adds some macros to the Route
facade to allow the localization. There are also some helpers.
Installation
Usage
-
Group all the routes you want to translate inside a
Route::localize(...)
. This must be put at the topmost group level of your routes. - For each route that you want to localize change the method with the localized one. The localized version has a
Localized
suffix.
E.g. if this is your route file
Then it must become
- You can now translate all your routes using the Laravel translation service. Inside the your lang folder (eg.
/lang/it
) create aroutes.php
file.
Customizing the available languages
First you need to publish the config.
Then you will find two new files (locales.php
and localized-routes.php
) files inside your config
folder.
The file locales.php
contains the list of the available locales. By default it is as follows:
You can now edit the available
with the locale you want to enable.
The file localized-routes.php
contains the configuration of the package. By default it is as follows:
You can now edit the prefix_default
to true
if you want to prefix the default locale to the routes.
Named routes
This package supports named routes out of the box and adds some useful prefixes.
E.g. Let's suppose these are your routes.
And this is your translation
Then you can simply do as follows:
Helpers
-
route_localized($name, $parameters = [], $locale = null, $absolute = true)
it behaves exactly as theroute
helper of Laravel, but it allows you to specify the locale -
url_localized($url, $locale = null)
it allows you to localize an URL -
current_localized($locale = null)
it returns the current URL or route localized -
available_locales()
returns the available locales, without their names -
is_default_locale($locale)
returns true if the specified locale is the default one -
is_current_locale_default
returns true if the current locale is the default one -
locale_name($locale, $default = null)
returns the locale name for the specified locale -
default_locale()
returns the default locale available_alternates()
returns the available alternates for the available locales
Using a different LocaleProvider
By default the package uses the Dartmoon\LaravelLocalizedRoutes\App\LocaleProviders\DefaultLocaleProvider
to get the current locale from the locales.php
config file.
If you want to use a different provider to get the available locales from somewhere else, you can do so by creating a new class that implements the Dartmoon\LaravelLocalizedRoutes\App\LocaleProviders\Contracts\LocaleProviderContract
interface and then binding it in the service container.
Then you can bind it in the service container in the AppServiceProvider
class.
License
This project is licensed under the MIT License - see the LICENSE.md file for details