If you are building a multilingual website and you need URL management then this is the package for you. It integrates into existing Laravel functionality to support translated URLs and custom locales.
It supports route caching out of the box.
Before, I was using mcamara/laravel-localization
to handle this, but I really really needed route caching to work. So, I created this package and simplified the whole setup and integration process.
Whatever mcamara/laravel-localization package can do, Loki can do too, but better.
Features
Simple installation
Easy configuration
Custom locales
Hide default locale
Translated routes
Language selector
Route caching
Native Laravel helper functions (route and url)
Support for non localized routes
Slug translation
Installation
From the command line:
composer require laravelista/loki
Then, add the Loki trait to your RouteServiceProvider class:
use Laravelista\Loki\Loki;
class RouteServiceProvider extends ServiceProvider
{
use Loki;
}
If you don't intend to use translated URLs (by default it is set to false) then publish the route file with the command bellow. Otherwise be sure to publish the config file, set useTranslatedUrls to true and create appropriate route files {locale}.web.php.
Publish the localized routes file loki.web.php to your /routes directory:
Now, add $this->mapLocalizedWebRoutes(); to the map method in the RouteServiceProvider.
Installation complete!
Put your localized routes in /routes/loki.web.php file. If you are using translated URLs create a route file for each locale {locale}.web.php (eg. en.web.php). Your non localized routes can remain in the /routes/web.php file. To access non localized routes use URL::getNonLocalizedRoute or URL::getNonLocalizedUrl. See the helpers chapter bellow to find out more.
That's it! View the configuration chapter bellow to configure your preferences.
If you have this enabled, be sure to use route names otherwise it will not work correctly.
Resource routes
If you are using Route::resource and have set useTranslatedUrls to true you will have to set the names for the resource manually or stop using resource mapper and manually map resource routes.
Syntax for the show method, but you can apply the same to others.
The default Laravel helper functions route and url have been changed to support URL localization. So you can use those as you normally would. This enables you to easily swap the mcamara/laravel-localization package with this one. Also, you can install this package, configure your supported locales and your route and URL links will work without the need to change anything.
The bellow helper methods are for edge cases where you want to retrieve the URL for specific locale or just get the current URL in specific locale.
This method will set the parameters for given locale, so that when method getLocalizedRoute
is called it will use these parameters instead of the already present parameters.
Useful for translating slugs. Remember to include all parameters.
Language switcher
Use this blade template snippet to enable users to change the language:
Add this code to your view file inside @php {your code here} @endphp block or inside a controller method where you want to translate slugs:
foreach (config('loki.supportedLocales') as $locale) {
URL::overrideParameters($locale, $model->{'slug_'.$locale});
}
Replace $model->{'slug_'.$locale} with whatever logic you use to get the translated slug for the model from database.
This method will override the route parameters for given locale, so that the language switcher will return correct URLs with translated slugs.
Sponsors & Backers
I would like to extend my thanks to the following sponsors & backers for funding my open-source journey. If you are interested in becoming a sponsor or backer, please visit the Backers page.
Contributing
Thank you for considering contributing to Loki! The contribution guide can be found Here.
Code of Conduct
In order to ensure that the open-source community is welcoming to all, please review and abide by the Code of Conduct.
License
Loki is open-source software licensed under the MIT license.
请发表评论