{
"show": "Toutes les données ont été chargées."
}
Via Model
use LaravelLang\Routes\Middlewares\LocalizationByModel;
app('router')
->middleware(LocalizationByModel::class)
->get('foo/{bar}', function (string $bar) {
return response()->json([
$bar => __('Foo'),
]);
});
This method allows you to set the locale for any authorized client. To do this, make sure that the model contains the "locale" attribute.
If the user table does not have this column, add it if necessary, or create a dynamic method in the model class.
For example:
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Foundation\Auth\User as Authenticatable;
use LaravelLang\LocaleList\Locale;
class User extends Authenticatable
{
protected function locale(): Attribute
{
return Attribute::make(
get: fn () => Locale::French
);
}
}
Now, when the user is authorized via middleware or manually, for example, using the Auth::login($user) method, the application will return information in the language the user's choice.
In addition, if the user is authorized with a particular guard, you can specify it with a colon when calling the middleware.
For example:
use LaravelLang\Routes\Middlewares\LocalizationByModel;
app('router')
->middleware(LocalizationByModel::class . ':guard_name')
->get('foo/{bar}', function (string $bar) {
return response()->json([
$bar => __('Foo'),
]);
});
To get a user instance, the middleware uses the following code:
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
class LocalizationByModel extends Middleware
{
public function __construct(
protected ?string $guard = null,
) {
}
protected function user(Request $request): ?Model
{
return $request->user($this->guard);
}
This method allows you to easily manage group prefixes using localized routes.
In addition to the main middleware for this group, LocalizationByParameterPrefix, the following are also involved part: LocalizationByCookie, LocalizationByHeader and LocalizationBySession.
You can use one of three interaction methods:
use Illuminate\Support\Facades\Route;
use LaravelLang\Routes\Facades\LocalizationRoute;
app('router')->localizedGroup(fn () => ...)
Route::localizedGroup(fn () => ...)
LocalizationRoute::group(fn () => ...)
The final application will register two types of routes - with and without the {locale} prefix. For example:
URL
Pattern
Name
https://example.com/fr/foo/show
{locale}/foo/{bar}
localized.show
https://example.com/foo/show
foo/{bar}
show
The name prefix can be changed in the routes section of the config/localization.php file.
When a localization parameter is passed to install it in the application, the event is invoked:
use LaravelLang\Routes\Events\LocaleHasBeenSetEvent;
LocaleHasBeenSetEvent::dispatch($locale);
The $locale property of the event is of the LaravelLang\Locales\Data\LocaleData type.
If necessary, you can listen to this event. For example:
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use LaravelLang\Routes\Events\LocaleHasBeenSetEvent;
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
Event::listen(static function (LocaleHasBeenSetEvent $event) {
Log::info('Locale set to: ' . $event->locale->code);
});
}
}