- First install those two packages as Dev Depency.
"require-dev": {
...
"laravel-lang/lang": "^11.0",
"laravel-lang/publisher": "^14.1"
},- Optional in update scripts (
post-update-cmd) in composer.json add update languages cmd to keep them updated
"scripts": {
"post-update-cmd": [
...
"@php artisan lang:update"
],
}- now run
php artisan lang:add {LANGUAGES}to add the lang files you need ex:php artisan lang:add ar en fr
and now, we got all lang files we need in lang\* folder for L9 or resources\lang\* for L<9.
- in
app\Http\MiddlwaresCreate 3 files ( attached below ) - in
app/Http/Kernel.phpadd: -
- in
$middlewareGroups,websection\App\Http\Middleware\WebLocalization::class
- in
-
- in
$middlewareGroups,apisection\App\Http\Middleware\ApiLocalization::class
- in
/**
* The application's route middleware groups.
*
* @var array<string, array<int, class-string|string>>
*/
protected $middlewareGroups = [
'web' => [
//...
\App\Http\Middleware\WebLocalization::class,
],
'api' => [
//...
\App\Http\Middleware\ApiLocalization::class,
],
];- in
config/app.phpto support ar,en add and so on:
'available_locales' => [
'en' => [
'dir' => 'ltr',
'name' => 'English',
'native' => 'English',
'script' => 'Latn',
'flag_code' => 'us',
],
'ar' => [
'dir' => 'rtl',
'name' => 'Arabic',
'native' => 'العربية',
'script' => 'Arab',
'flag_code' => 'sa',
],
],- for api they must send you
Accept-Languageheader with language ex:ar - for web you can change the language just by passing
?lang=arat the end of url
<!DOCTYPE html>
@php
$lang = app()->getLocale();
$dir = config('app.available_locales')[app()->getLocale()]['dir'] ?? 'ltr';
@endphp
<html lang="{{ $lang }}" dir="{{ $dir }}">
<!-- ... -->
</html>