PHP code example of dartmoon / laravel-localized-routes
1. Go to this page and download the library: Download dartmoon/laravel-localized-routes library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dartmoon / laravel-localized-routes example snippets
Route::localize(function () {
// Put here all the routes you want to localize
});
/**
* Return enabled locales
*/
return [
'available' => [
// 'locale' => 'Name of the locale'
'en' => 'EN',
'it' => 'IT',
],
'default' => 'en', // Default locale to be used
];
/**
* Return the configuration for the localized routes
*/
return [
'prefix_default' => false, // If true the default locale will be prefixed to the routes
];
route('home'); // If you have the "en" locale loaded then '/home', if you have the "it" locale loaded than it will be '/home-translated'
route('it.home'); // Will return '/home-translated'. This route will not be defined if the current locale is "it"!
route('en.home'); // Will return '/home'. This route will not be defined if the current locale is "en"!
namespace App\LocaleProviders;
use Dartmoon\LaravelLocalizedRoutes\App\LocaleProviders\Contracts\LocaleProviderContract;
class MyCustomLocaleProvider implements LocaleProviderContract
{
public function getDefaultLocale(): string
{
return 'en';
}
public function getAvailableLocales(): array
{
return [
'en' => 'EN',
'it' => 'IT',
];
}
public function getLocaleName(string $locale, string $default = null): string
{
return $this->getAvailableLocales()[$locale] ?? $default;
}
}
namespace App\Providers;
use App\LocaleProviders\MyCustomLocaleProvider;
use Dartmoon\LaravelLocalizedRoutes\App\LocaleProviders\Contracts\LocaleProviderContract;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(
LocaleProviderContract::class,
MyCustomLocaleProvider::class
);
}
}