Skip to content

Commit 8cdbbaa

Browse files
Add local columns in tables and add middleware to check and set app language
1 parent 081ac19 commit 8cdbbaa

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Middleware;
6+
7+
use Closure;
8+
use Illuminate\Http\Request;
9+
use Illuminate\Support\Facades\Auth;
10+
use Psr\Container\ContainerExceptionInterface;
11+
use Psr\Container\NotFoundExceptionInterface;
12+
use Symfony\Component\HttpFoundation\Response;
13+
14+
final class LocaleLangMiddleware
15+
{
16+
/**
17+
* @throws ContainerExceptionInterface
18+
* @throws NotFoundExceptionInterface
19+
*/
20+
public function handle(Request $request, Closure $next): Response
21+
{
22+
if (! Auth::check() || ! session()->has('locale')) {
23+
$navigatorLang = $request->server('HTTP_ACCEPT_LANGUAGE');
24+
$navigatorLang = (is_string($navigatorLang) && strlen($navigatorLang) >= 2) ? $navigatorLang : '';
25+
$navigatorLang = substr($navigatorLang, 0, 2);
26+
$lang = in_array($navigatorLang, ['fr', 'en']) ? $navigatorLang : 'fr';
27+
$request->session()->put('locale', $lang);
28+
}
29+
app()->setLocale(session()->get('locale'));
30+
31+
return $next($request);
32+
}
33+
}

bootstrap/app.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use App\Http\Middleware\LocaleLangMiddleware;
56
use Illuminate\Foundation\Application;
67
use Illuminate\Foundation\Configuration\Exceptions;
78
use Illuminate\Foundation\Configuration\Middleware;
@@ -17,6 +18,8 @@
1718
'role' => \Spatie\Permission\Middleware\RoleMiddleware::class,
1819
'checkIfBanned' => \App\Http\Middleware\CheckIfBanned::class,
1920
]);
21+
$middleware->append(\Illuminate\Session\Middleware\StartSession::class);
22+
$middleware->append(LocaleLangMiddleware::class);
2023
})
2124
->withExceptions(function (Exceptions $exceptions): void {
2225
//
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Facades\Schema;
8+
9+
return new class extends Migration
10+
{
11+
public function up(): void
12+
{
13+
Schema::table('articles_discussions_threads_tables', static function (Blueprint $table): void {
14+
Schema::table('articles', function (Blueprint $table): void {
15+
$table->string('locale')->default('fr')->after('slug');
16+
});
17+
18+
Schema::table('discussions', function (Blueprint $table): void {
19+
$table->string('locale')->default('fr')->after('body');
20+
});
21+
22+
Schema::table('threads', function (Blueprint $table): void {
23+
$table->string('locale')->default('fr')->after('body');
24+
});
25+
});
26+
}
27+
28+
public function down(): void
29+
{
30+
Schema::table('articles_discussions_threads_tables', function (Blueprint $table): void {
31+
Schema::table('articles', function (Blueprint $table): void {
32+
$table->dropColumn('locale');
33+
});
34+
35+
Schema::table('discussions', function (Blueprint $table): void {
36+
$table->dropColumn('locale');
37+
});
38+
39+
Schema::table('threads', function (Blueprint $table): void {
40+
$table->dropColumn('locale');
41+
});
42+
});
43+
}
44+
};

0 commit comments

Comments
 (0)