Installation
composer require richan-fongdasen/laravel-i18n
Publish the config file:
php artisan vendor:publish --provider="RichanFongdasen\LaravelI18n\LaravelI18nServiceProvider" --tag="config"
Configure Supported Locales
Update config/laravel-i18n.php:
'locales' => [
'en' => 'English',
'fr' => 'Français',
'es' => 'Español',
],
First Use Case: Localized Routes
Define a localized route in routes/web.php:
Route::i18n([
'en' => 'home',
'fr' => 'accueil',
'es' => 'inicio',
], function () {
return view('welcome');
});
Access via /en/home, /fr/accueil, etc.
Dynamic Route Grouping
Use Route::i18nGroup() for shared prefixes:
Route::i18nGroup(['prefix' => 'admin'], function () {
Route::i18n(['en' => 'dashboard', 'fr' => 'tableau-de-bord'], function () {
// ...
});
});
Locale Fallback
Set a default locale in config/laravel-i18n.php:
'default_locale' => 'en',
'fallback_locale' => 'en',
Localized Model Attributes
Use the Localizable trait:
use RichanFongdasen\LaravelI18n\Eloquent\Localizable;
class Product extends Model
{
use Localizable;
protected $localizable = ['name', 'description'];
}
Access translations:
$product->name('fr'); // Get French name
$product->setName('fr', 'Nouveau nom'); // Set French name
Database Schema
The package auto-creates localizable_<table_name> tables. Migrate with:
php artisan i18n:migrate
Locale Detection Use middleware to auto-detect locale from:
/fr/...)app/Http/Kernel.php:protected $middlewareGroups = [
'web' => [
// ...
\RichanFongdasen\LaravelI18n\Http\Middleware\DetectLocale::class,
],
];
Locale-Specific Responses Dynamically load views/translations:
$locale = app()->getLocale();
return view("pages.{$locale}.home");
Route Caching Conflicts Clear route cache after adding localized routes:
php artisan route:clear
Or disable caching in config/laravel-i18n.php:
'cache_routes' => false,
Locale-Specific Validation Ensure validation rules account for localized fields:
$validator = Validator::make($request->all(), [
"name.{$locale}" => 'required|string',
]);
Database Overhead Localized fields create additional database tables. Monitor query performance with:
php artisan tinker
>>> \DB::enableQueryLog();
>>> $product->name('fr');
>>> \DB::getQueryLog();
Locale Not Switching?
Check middleware order in app/Http/Kernel.php. DetectLocale must run before ShareErrorsFromSession or StartSession.
Missing Translations
Verify the localizable_<table> tables exist and are populated. Use:
php artisan i18n:refresh
Custom Locale Providers
Extend locale detection by implementing RichanFongdasen\LaravelI18n\Contracts\LocaleProvider:
class CookieLocaleProvider implements LocaleProvider
{
public function getLocale(): string
{
return request()->cookie('locale', config('app.locale'));
}
}
Register in config/laravel-i18n.php:
'providers' => [
\RichanFongdasen\LaravelI18n\Providers\UrlLocaleProvider::class,
App\Providers\CookieLocaleProvider::class,
],
Locale-Specific Controllers Use route model binding with localized models:
Route::i18n(['en' => 'product/{product}', 'fr' => 'produit/{product}'], function (Product $product) {
// $product is automatically localized
});
Fallback Logic Override fallback behavior in a service provider:
public function boot()
{
app()->setFallbackLocale(function () {
return request()->ip() === '127.0.0.1' ? 'en' : config('app.fallback_locale');
});
}
How can I help you explore Laravel packages today?