The millionvisions/laravel-i18n package provides a simple way to manage translations and localization in Laravel
applications. It offers features such as middleware for locale handling, Blade directives for generating alternate URLs,
and easy generation of translation files.
- Install the package via composer:
composer require millionvisions/laravel-i18n- Publish the configuration file to
config/i18n.php:
php artisan vendor:publish --provider="MillionVisions\I18n\I18nServiceProvider"- Add the I18nServiceProvider to the providers array in
bootstrap/providers.php:
return [
//...
MillionVisions\LaravelI18n\I18nServiceProvider::class,
//...
];To generate translation files based on your application's defined keys, you can use the following artisan commands.
php artisan i18n:create-translation-filesThis command scans your application for translation keys and creates corresponding PHP files in the configured language directory, ensuring that your translations are structured and ready for use.
php artisan i18n:seedThis command acts as an alias for the previous command and can be used interchangeably to achieve the same result.
The package includes middleware that helps manage locale preferences in your application. It ensures that routes are accessible based on the provided locale parameter.
Route::group(
['prefix' => '{locale}', 'middleware' => 'locale'],
function () {
// Define your localized routes here
}
);In this example, the prefix option specifies that the locale will be part of the URL. For instance, accessing /en or /de will route to the appropriate controller actions. The middleware ensures that the application checks the validity of the locale before proceeding.
Route::group(
['prefix' => '{locale}', 'middleware' => 'locale:en'],
function() {
// Define your localized routes here
}
);In this case, the locale:en middleware restricts access to this route group, allowing only English (en) as a valid locale. Other locales would result in an error or redirection based on your middleware's logic.
Route::localized()->group(function () {
// Define your localized routes here
});The localized macro provides a convenient way to define a group of routes that automatically handle locale prefixes and middleware. It streamlines the process of creating localized routes without repetitive code.
The package introduces custom Blade directives to facilitate the generation of alternate URLs for your application.
@alternate('de')This directive generates an HTML link tag that points to the equivalent page in the specified locale (in this case, German). It includes the appropriate hreflang attribute to help search engines understand language relationships.
@alternates()This directive automatically generates links for all defined locales, making it easier to manage multilingual SEO by ensuring that all language versions of a page are properly linked.
The MIT License (MIT). Please see License File for more information.