- 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\Middlwares
Create 3 files ( attached below ) - in
app/Http/Kernel.php
add: -
- in
$middlewareGroups
,web
section\App\Http\Middleware\WebLocalization::class
- in
-
- in
$middlewareGroups
,api
section\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.php
to 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-Language
header with language ex:ar
- for web you can change the language just by passing
?lang=ar
at 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>