Skip to content

Commit ea470ef

Browse files
add lang flag and update middleware with livewire class
1 parent 8cdbbaa commit ea470ef

File tree

10 files changed

+151
-80
lines changed

10 files changed

+151
-80
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ APP_ENV=local
33
APP_KEY=
44
APP_DEBUG=true
55
APP_URL=http://laravel.cm.test
6+
APP_LOCALE=fr
67
FILAMENT_PATH=cp
78

89
LOG_CHANNEL=stack

app/Http/Middleware/LocaleLangMiddleware.php

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 LocaleMiddleware
15+
{
16+
/**
17+
* @throws ContainerExceptionInterface
18+
* @throws NotFoundExceptionInterface
19+
*/
20+
public function handle(Request $request, Closure $next): Response
21+
{
22+
$user = Auth::user();
23+
$lang = config('lcm.lang.app_local');
24+
$supportLang = config('lcm.lang.support_local');
25+
if (! Auth::check()) {
26+
if (! is_null($request->server('HTTP_ACCEPT_LANGUAGE'))) {
27+
$navigatorLang = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2);
28+
if (in_array($navigatorLang, $supportLang)) {
29+
$lang = $navigatorLang;
30+
}
31+
}
32+
}
33+
if (! is_null($user)) {
34+
if (isset($user->settings['default_lang']) && $user->settings['default_lang'] != $lang) {
35+
$lang = $user->settings['default_lang'];
36+
}
37+
}
38+
app()->setLocale($lang);
39+
40+
return $next($request);
41+
}
42+
}

app/Livewire/Components/Locale.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Livewire\Components;
6+
7+
use Illuminate\Contracts\View\View;
8+
use Illuminate\Routing\Redirector;
9+
use Illuminate\Support\Facades\Auth;
10+
use Livewire\Component;
11+
use Symfony\Component\HttpFoundation\RedirectResponse;
12+
13+
final class Locale extends Component
14+
{
15+
public string $selectedLang;
16+
17+
public function mount(): void
18+
{
19+
$this->selectedLang = app()->getLocale();
20+
}
21+
22+
public function changeLang(string $lang): RedirectResponse|Redirector
23+
{
24+
$user = Auth::user();
25+
if ($user) {
26+
$settings = $user->settings;
27+
$settings['default_lang'] = $lang;
28+
$user->settings = $settings;
29+
$user->save();
30+
}
31+
app()->setLocale($lang);
32+
return redirect()->to(url()->current());
33+
}
34+
35+
public function render(): View
36+
{
37+
return view('livewire.components.locale');
38+
}
39+
}

bootstrap/app.php

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

33
declare(strict_types=1);
44

5-
use App\Http\Middleware\LocaleLangMiddleware;
5+
use App\Http\Middleware\LocaleMiddleware;
66
use Illuminate\Foundation\Application;
77
use Illuminate\Foundation\Configuration\Exceptions;
88
use Illuminate\Foundation\Configuration\Middleware;
@@ -18,8 +18,10 @@
1818
'role' => \Spatie\Permission\Middleware\RoleMiddleware::class,
1919
'checkIfBanned' => \App\Http\Middleware\CheckIfBanned::class,
2020
]);
21-
$middleware->append(\Illuminate\Session\Middleware\StartSession::class);
22-
$middleware->append(LocaleLangMiddleware::class);
21+
$middleware->appendToGroup('web', [
22+
LocaleMiddleware::class,
23+
\Illuminate\Session\Middleware\StartSession::class,
24+
]);
2325
})
2426
->withExceptions(function (Exceptions $exceptions): void {
2527
//

config/lcm.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
'web_hook' => env('SLACK_WEBHOOK_URL', ''),
2727
],
2828

29+
'lang' => [
30+
'app_local' => env('APP_LOCALE', 'fr'),
31+
'support_local' => ['fr', 'en'],
32+
],
33+
2934
'spa_url' => env('FRONTEND_APP_URL', 'http://localhost:4200'),
3035

3136
'notch-pay-public-token' => env('NOTCHPAY_PUBLIC_KEY', null),
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
private array $tables = ['articles', 'discussions', 'threads'];
12+
13+
public function up(): void
14+
{
15+
Schema::table('articles', function (Blueprint $table): void {
16+
$table->string('locale')->default('fr')->after('slug');
17+
});
18+
19+
Schema::table('discussions', function (Blueprint $table): void {
20+
$table->string('locale')->default('fr')->after('body');
21+
});
22+
23+
Schema::table('threads', function (Blueprint $table): void {
24+
$table->string('locale')->default('fr')->after('body');
25+
});
26+
}
27+
28+
public function down(): void
29+
{
30+
foreach ($this->tables as $tab) {
31+
Schema::table($tab, function (Blueprint $table): void {
32+
$table->dropColumn('locale');
33+
});
34+
}
35+
}
36+
};

database/migrations/2024_12_03_004238_add_locale_to_articles_discussions_threads_tables.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

resources/views/components/layouts/footer.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class="ml-2 size-6 rounded-full"
9999
<x-icon.youtube class="size-6" aria-hidden="true" />
100100
</x-link>
101101
</div>
102+
<livewire:components.locale></livewire:components.locale>
102103
</div>
103104
</x-container>
104105
</div>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<div class="flex space-x-2 font-heading cursor-pointer font-bold text-primary-700" >
2+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="flag-icons-cm" viewBox="0 0 640 480" class="w-6 h-6">
3+
<path fill="#007a5e" d="M0 0h213.3v480H0z"/>
4+
<path fill="#ce1126" d="M213.3 0h213.4v480H213.3z"/>
5+
<path fill="#fcd116" d="M426.7 0H640v480H426.7z"/>
6+
<g fill="#fcd116" transform="translate(320 240)scale(7.1111)">
7+
<g id="cm-b">
8+
<path id="cm-a" d="M0-8-2.5-.4 1.3.9z"/>
9+
<use xlink:href="#cm-a" width="100%" height="100%" transform="scale(-1 1)"/>
10+
</g>
11+
<use xlink:href="#cm-b" width="100%" height="100%" transform="rotate(72)"/>
12+
<use xlink:href="#cm-b" width="100%" height="100%" transform="rotate(144)"/>
13+
<use xlink:href="#cm-b" width="100%" height="100%" transform="rotate(-144)"/>
14+
<use xlink:href="#cm-b" width="100%" height="100%" transform="rotate(-72)"/>
15+
</g>
16+
</svg>
17+
@if($selectedLang == 'fr')
18+
<button wire:click="changeLang('en')">EN</button>
19+
@else
20+
<button wire:click="changeLang('fr')">FR</button>
21+
@endif
22+
</div>

0 commit comments

Comments
 (0)