Laravel package for wrapping routes in multiple locale prefixes.
Via Composer
composer require getsupercode/localizater
To detect and change the locale of the application based on the request automatically, you can add this middleware to your app/Http/Kernel
:
protected $middlewareGroups = [
'web' => [
\Getsupercode\Localizater\LocalizaterMiddleware::class,
// ...
]
];
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="Getsupercode\Localizater\LocalizaterServiceProvider" --tag="config"
After installing the package, Adding the middleware and publishing the configuration file. You need to edit the configuration file config/localizater.php
in order to add more locales.
The default locale is app.locale
located at config/app.php
file.
Add supported locales. It's recommended to write the locale value with its native language.
'locales' => [
'en' => 'English',
'fr' => 'Français',
'ar' => 'العربية',
]
Add RTL direction locales.
'rtl_locales' => ['ar']
If this option is set to true, Default locale URL will be prefixed.
true:
www.example.com/en
www.example.com/fr
false:
www.example.com
www.example.com/fr
If this option is set to true, Default locale route name will be prefixed.
true:
Method | URI | URI | Name |
---|---|---|---|
GET | HEAD | /page | en.page |
GET | HEAD | /fr/page | fr.page |
false:
Method | URI | URI | Name |
---|---|---|---|
GET | HEAD | /page | page |
GET | HEAD | /fr/page | fr.page |
The package will not override the route features you already know. It's just a wrapper function that will create multiple locale routes for you.
// routes/web.php
<?php
use Getsupercode\Localizater\Facades\Localizater;
use Illuminate\Support\Facades\Route;
Localizater::group(function () {
Route::view('/', 'welcome')->name('welcome');
Route::get('/user', 'UserController@index');
});
// Put other (Non read) route actions outside the `Localizater::group` as you don't need to have multiple locales for those actions.
Route::post('/user', 'UserController@store');
The above example will give us:
Method | URI | Name |
---|---|---|
GET|HEAD | / | welcome |
GET|HEAD | /fr | fr.welcome |
GET|HEAD | /user | |
GET|HEAD | /fr/user | fr. |
POST | /user |
If you add a name to a route it will be prepended by the locale key locale.name
. for example: (fr.welcome
)
All locales without a name will have the same prefix name like fr.
. And this is normal as you don't need its names.
You can add attributes to the localizer group function as you do with the route group function.
Localizater::group(['middleware' => 'auth'], function () {
Route::view('/home', 'home')->name('home');
});
Or:
Localizater::group(function () {
Route::group(['middleware' => 'auth'], function () {
Route::view('/home', 'home')->name('home');
});
});
Or as translated routes:
Localizater::group(function () {
Route::group(['middleware' => 'auth'], function () {
Route::get(__('home'), function() {
return view('home');
})->name('home');
});
});
You can get the current route URL in a translated locale key:
You can pass the same parameters as the locale_route()
except locale as it is found based on app()->getLocale()
.
// Current route URL: example.com/fr
localized_route('home');
// Output: example.com/fr/domicile
You can get the current route URL in different locale key:
// Current route URL: example.com
locale_route(null, 'fr');
// Output: example.com/fr
Or a named route:
// Route URL: example.com/fr/home
locale_route('home', 'en');
// Output: example.com/home
You can pass the same parameters as the route()
function after the locale parameter.
// locale_route($route, $locale, $parameters, $absolute);
// Current route
locale_route(null, 'fr', ['status' => 'active'], true);
// Named route
locale_route('home', 'fr', ['status' => 'active'], true);
You can get the value of the locale key in localizater.locales
configuration for the current locale or a specified locale:
locale_name();
// Output: English
locale_name('fr');
// Output: Français
locale_name('ar');
// Output: العربية
You can get HTML dir
attribute based on current locale. The package will search for RTL locales in localizater.rtl_locales
config. If the current locale is listed there, The output will be rtl
or ltr
if it's not listed.
// Current locale is: ar
locale_dir();
// Output: rtl
// Current locale is: en
locale_dir();
// Output: ltr
<html dir="{{ locale_dir() }}"></html>
You can also get dir
attribute for a specified locale:
locale_dir('ar');
// Output: rtl
Please see the changelog for more information on what has changed recently.
composer test
Please see contributing.md for details.
If you discover any security related issues, please email elkebir.med@gmail.com instead of using the issue tracker.
MIT. Please see the license file for more information.