movemoveapp/laravel-localization
Installation:
composer require movemoveapp/laravel-localization
Publish the config file:
php artisan vendor:publish --provider="Movemoveapp\Localization\LocalizationServiceProvider"
Configure config/localization.php:
'locales' => [
'en' => [
'name' => 'English',
'url' => 'en',
'flag' => 'gb',
'native' => 'English',
],
'es' => [
'name' => 'Español',
'url' => 'es',
'flag' => 'es',
'native' => 'Español',
],
'ru' => [
'name' => 'Русский',
'url' => 'ru',
'flag' => 'ru',
'native' => 'Русский',
],
],
First Use Case:
use Movemoveapp\Localization\Facades\Localization;
$locale = Localization::getCurrentLocale(); // e.g., 'en'
domain_locales in config/localization.php:
'domain_locales' => [
'example.es' => 'es',
'example.ru' => 'ru',
'en.example.com' => 'en',
],
Now, visiting example.es will automatically redirect to the Spanish locale.Accept-Language header or session/cookie.
Localization::detectLocale(); // Auto-detects and sets locale
routes/web.php:
Route::get('/home', 'HomeController@index')->name('home');
The package handles locale-specific URLs (e.g., /en/home, /es/inicio).Localization::setLocale('es'); // Force locale to Spanish
@foreach(Localization::getSupportedLocales() as $locale)
<a href="{{ Localization::getLocalizedURL($locale, route('home')) }}">
{{ $locale['native'] }}
</a>
@endforeach
$url = Localization::getLocalizedURL('es', route('home')); // e.g., /es/inicio
return Localization::redirectLocalized('es', route('home'));
Route::middleware(['locale' => 'es'])->group(function () {
Route::get('/es/privacy', 'PrivacyController@index');
});
app/Http/Kernel.php:
protected $middlewareGroups = [
'web' => [
// ...
\Movemoveapp\Localization\Middleware\DetectLocale::class,
],
];
'domain_locales' => [
'example.es' => 'es',
'example.ru' => 'ru',
'en.example.com' => 'en',
],
example.es will redirect to /es/ (or your configured base URL for the locale).<a href="{{ localized(route('home')) }}">
{{ __('Home') }}
</a>
Add to AppServiceProvider:
Blade::directive('localized', function ($expression) {
return "<?php echo Movemoveapp\Localization\Facades\Localization::getLocalizedURL(Localization::getCurrentLocale(), {$expression}); ?>";
});
Localization::saveLocaleToSession('es');
Localization::detectLocaleFromSession();
config/localization.php:
'fallback_locale' => 'en',
'fallback_locales' => ['en', 'es'],
$this->app->instance('request', $request = Mockery::mock());
$request->shouldReceive('get')->with('locale')->andReturn('es');
example.com and en.example.com), the package prioritizes the most specific match.en.example.com vs. example.com).php artisan route:clear
Or disable caching during development:
'cache_routes' => env('APP_ENV') !== 'local',
'session_key' => 'laravel_localization_locale',
'cookie_key' => 'laravel_localization_locale',
auth may not work as expected with locale-aware routes.localized helper for URLs.route() may not respect the current locale in tests.Localization::setLocale('es');
$url = route('home'); // Now returns `/es/inicio`
dd(Localization::getCurrentLocale());
dd(Localization::detectLocale());
Add to AppServiceProvider:
Localization::setLocaleChangedCallback(function ($locale) {
\Log::info("Locale changed to: {$locale}");
});
Ensure locales and domain_locales are correctly configured. Test with:
php artisan localization:list
Override the default detection logic by extending the DetectLocale middleware:
namespace App\Http\Middleware;
use Closure;
use Movemoveapp\Localization\Middleware\DetectLocale as BaseDetectLocale;
class CustomDetectLocale extends BaseDetectLocale
{
protected function detectLocale()
{
// Custom logic here
return 'es'; // Force Spanish
}
}
Register in app/Http/Kernel.php:
protected $middleware = [
\App\Http\Middleware\CustomDetectLocale::class,
];
Load locales dynamically (e.g., from a database):
Localization::setLocales($dynamicLocales);
Override the redirect behavior:
Localization::setRedirectCallback(function ($locale, $url) {
return redirect()->to($url)->withCookie(cookie('locale', $locale, 30));
});
Use Blade's @lang directive or create locale-specific view paths:
// In config/filesystems.php
'views' => [
'en' => resource_path('views/en'),
'es' => resource_path('views/es'),
],
Then override the view resolver in AppServiceProvider:
$this->app['view']->addNamespace('en', resource
How can I help you explore Laravel packages today?