Installation
composer require baconmanager/language-bundle
Add to config/bundles.php (Laravel 5.4+):
Bacon\Bundle\LanguageBundle\BaconLanguageBundle::class,
Route Configuration
Add to routes/web.php (or equivalent):
Route::group([
'prefix' => '{_locale}',
'middleware' => ['locale']
], function () {
// Your routes here
});
Middleware Setup
Create app/Http/Middleware/LocaleMiddleware.php:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\App;
class LocaleMiddleware
{
public function handle($request, Closure $next)
{
$locale = $request->segment(1);
if (in_array($locale, ['en', 'pt', 'es'])) { // Add supported locales
App::setLocale($locale);
}
return $next($request);
}
}
Register in app/Http/Kernel.php:
protected $middlewareGroups = [
'web' => [
// ...
\App\Http\Middleware\LocaleMiddleware::class,
],
];
First Use Case Render the language switcher in a Blade template:
@languageswitcher(['en', 'pt', 'es']) <!-- Replace with actual template tag if available -->
Locale Routing
{_locale} in route definitions (e.g., /{_locale}/admin/users).Translation Integration
resources/lang/{locale}/messages.php (Laravel convention).__() or @lang() in Blade:
<h1>@lang('messages.welcome')</h1>
Language Switcher UI
resources/views/components/languageswitcher.blade.php):
@foreach(['en', 'pt', 'es'] as $locale)
<a href="{{ url()->current() }}" hreflang="{{ $locale }}">
{{ Str::upper($locale) }}
</a>
@endforeach
@component('components.languageswitcher') @endcomponent
Admin CRUD Integration
// routes/web.php
Route::prefix('{_locale}/admin/languages')->group(function () {
Route::resource('languages', \Bacon\LanguageController::class);
});
Dynamic Locale Detection Use request headers or user preferences:
$locale = request()->header('Accept-Language') ?? session('locale') ?? 'en';
App::setLocale($locale);
Fallback Locales
Configure in app/Providers/AppServiceProvider.php:
public function boot()
{
\Illuminate\Support\Facades\App::setFallbackLocale('en');
}
API Localization Return locale-specific responses:
return response()->json($data, 200, [], null, ['locale' => app()->getLocale()]);
Route Prefix Conflicts
{_locale} doesn’t clash with existing route parameters.php artisan route:list to verify routes.Middleware Order
LocaleMiddleware before other middleware that relies on locale (e.g., CORS, auth).Translation Caching
php artisan view:clear
php artisan config:clear
Bundle-Specific Quirks
AppKernel.php). Adapt to Laravel’s config/bundles.php.Language model extends Laravel’s Model and not Symfony’s Entity.Check Locale Log the current locale in middleware:
\Log::debug('Current locale:', ['locale' => app()->getLocale()]);
Route Debugging
Use dd(Route::current()->getParameters()) to inspect captured {_locale}.
Translation Debugging
Verify translations exist in resources/lang/{locale}/messages.php:
dd(__('messages.key', [], app()->getLocale()));
Custom Locale Provider Override the default locale logic by binding a service:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->bind(\Bacon\LocaleProvider::class, function () {
return new \App\Services\CustomLocaleProvider();
});
}
Extend Language Switcher Create a custom view component to style the switcher:
<!-- resources/views/custom/languageswitcher.blade.php -->
<select onchange="window.location.href=this.value">
@foreach(config('app.locales') as $locale)
<option value="{{ url()->current() }}" {{ app()->getLocale() === $locale ? 'selected' : '' }}>
{{ $locale }}
</option>
@endforeach
</select>
Add New Locales
Update config/app.php:
'locales' => ['en', 'pt', 'es', 'fr'],
And create corresponding translation files.
Avoid Over-Fetching Lazy-load translations for non-critical paths:
if (app()->getLocale() === 'en') {
// Load English translations only when needed
}
Cache Translations Use Laravel’s translation caching:
php artisan translate:cache
How can I help you explore Laravel packages today?