Laravel localization
Very easy and light package for loclization. Routing based on available locales and easy to get rid of default language prefix from route, (*1)
Installation
Composer
Add Laravel Localization to your composer.json file, (*2)
"rohit/laravel-localization": "^1.0"
Run composer install
to get the latest version of package, (*3)
Or you can directly run the composer require
command, (*4)
composer require rohit/laravel-localization
Configuration
After the package install is completed you need to configure config/app.php
and add Providers
and Aliases
, (*5)
'providers` => [
.......
.......
Rohit\LaravelLocalization\LaravelLocalizationServiceProvider::class
]
```php
'aliases' => [
......
......
'Localization' => Rohit\LaravelLocalization\Facades\LaravelLocalization::class
], (*6)
### Vendor Publish
After the above steps, you need to publish vendor for this packge. It will create `laravel-localization.php` file under `config` folder. This folder contains the configuration for your locales.
php artisan vendor:publish --provider="Rohit\LaravelLocalization\LaravelLocalizationServiceProvider"
The file `laravel-localization.php` will contain the following structure
```php
return [
// Add any language you want to support and comes as prefix in the url
'all_locales' => [
'en',
'th'
],
'default_locale' => 'th', // Default locale will not be shown in the url
];
Here you can add as many locales available in your project and set the default_locale
to the value for which you want to exclude the prefix, (*7)
NOTE: You can keep this field blank if you want all prefix in th url, (*8)
For Example:, (*9)
If your project has en
and th
as the available locales and if you set th
as your default locale. Then the url will look like,, (*10)
For English: http://your_domain.com/en/your_page
, (*11)
For Thai http://your_domain.com/your_page
, (*12)
It will skip default locale th
from the url, (*13)
Middleware
After this, you need to update the app\Http\Kernel.php
file and add the following line under routeMiddleware
, (*14)
protected $routeMiddleware = [
........
........
'localization' => \Rohit\LaravelLocalization\Middleware\LanguageHandler::class,
]
Routing
Finally you can manage all the routes with this configuration and middleware for smooth operation, (*15)
Update app\Http\routes.php
file and add all your routes under this group, (*16)
Route::group([
'prefix' => Localization::setLocale(),
'middleware' => ['localization']
], function() {
// All your routes here
});
Helpers
There is a helper function to get the url for language switcher on the current page. You simply need to call a function with the locale
, (*17)
Localization::getLocaleUrl('en')