Skip to content

Commit e49ffe3

Browse files
change redirection to reload event ,deleted startsession middleware in app , ...etc
1 parent 3ae3b4a commit e49ffe3

File tree

7 files changed

+34
-34
lines changed

7 files changed

+34
-34
lines changed

app/Http/Middleware/LocaleMiddleware.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,27 @@ final class LocaleMiddleware
2020
public function handle(Request $request, Closure $next): Response
2121
{
2222
$user = Auth::user();
23-
$lang = config('lcm.lang.app_local');
24-
$supportLang = config('lcm.lang.support_local');
23+
$lang = config('lcm.app_locale');
24+
$supportLang = config('lcm.supported_locales');
25+
2526
if (! Auth::check()) {
27+
2628
if (! is_null($request->server('HTTP_ACCEPT_LANGUAGE'))) {
2729
$navigatorLang = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2);
30+
2831
if (in_array($navigatorLang, $supportLang)) {
2932
$lang = $navigatorLang;
3033
}
3134
}
3235
}
36+
3337
if (! is_null($user)) {
34-
if (isset($user->settings['default_lang']) && $user->settings['default_lang'] != $lang) {
35-
$lang = $user->settings['default_lang'];
38+
39+
if (isset($user->settings['locale']) && $user->settings['locale'] != $lang) {
40+
$lang = $user->settings['locale'];
3641
}
3742
}
43+
3844
app()->setLocale($lang);
3945

4046
return $next($request);

app/Livewire/Components/Locale.php renamed to app/Livewire/Components/ChangeLocale.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
namespace App\Livewire\Components;
66

77
use Illuminate\Contracts\View\View;
8-
use Illuminate\Routing\Redirector;
98
use Illuminate\Support\Facades\Auth;
109
use Livewire\Component;
11-
use Symfony\Component\HttpFoundation\RedirectResponse;
1210

13-
final class Locale extends Component
11+
final class ChangeLocale extends Component
1412
{
1513
public string $selectedLang;
1614

@@ -19,22 +17,23 @@ public function mount(): void
1917
$this->selectedLang = app()->getLocale();
2018
}
2119

22-
public function changeLang(string $lang): RedirectResponse|Redirector
20+
public function changeLang(string $lang): void
2321
{
2422
$user = Auth::user();
23+
2524
if ($user) {
2625
$settings = $user->settings;
27-
$settings['default_lang'] = $lang;
26+
$settings['locale'] = $lang;
2827
$user->settings = $settings;
2928
$user->save();
3029
}
31-
app()->setLocale($lang);
3230

33-
return redirect()->to(url()->current());
31+
app()->setLocale($lang);
32+
$this->dispatch('localeChanged');
3433
}
3534

3635
public function render(): View
3736
{
38-
return view('livewire.components.locale');
37+
return view('livewire.components.change-locale');
3938
}
4039
}

bootstrap/app.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
'role' => \Spatie\Permission\Middleware\RoleMiddleware::class,
1919
'checkIfBanned' => \App\Http\Middleware\CheckIfBanned::class,
2020
]);
21-
$middleware->appendToGroup('web', [
21+
$middleware->web(append: [
2222
LocaleMiddleware::class,
23-
\Illuminate\Session\Middleware\StartSession::class,
2423
]);
2524
})
2625
->withExceptions(function (Exceptions $exceptions): void {

config/lcm.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@
2626
'web_hook' => env('SLACK_WEBHOOK_URL', ''),
2727
],
2828

29-
'lang' => [
30-
'app_local' => env('APP_LOCALE', 'fr'),
31-
'support_local' => ['fr', 'en'],
32-
],
29+
'app_locale' => env('APP_LOCALE', 'fr'),
30+
31+
'supported_locales' => ['fr', 'en'],
3332

3433
'spa_url' => env('FRONTEND_APP_URL', 'http://localhost:4200'),
3534

database/migrations/2024_12_03_004238_add_locale_column_to_tables.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,19 @@
88

99
return new class extends Migration
1010
{
11-
private array $tables = ['articles', 'discussions', 'threads'];
12-
1311
public function up(): void
1412
{
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-
});
13+
foreach (['articles', 'discussions', 'threads'] as $key => $value) {
14+
$afterColumn = ($key == 0) ? 'slug' : 'body';
15+
Schema::table($value, function (Blueprint $table) use ($afterColumn): void {
16+
$table->string('locale')->default('fr')->after($afterColumn);
17+
});
18+
}
2619
}
2720

2821
public function down(): void
2922
{
30-
foreach ($this->tables as $tab) {
23+
foreach (['articles', 'discussions', 'threads'] as $tab) {
3124
Schema::table($tab, function (Blueprint $table): void {
3225
$table->dropColumn('locale');
3326
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +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>
102+
<livewire:components.change-locale></livewire:components.change-locale>
103103
</div>
104104
</x-container>
105105
</div>

resources/views/livewire/components/locale.blade.php renamed to resources/views/livewire/components/change-locale.blade.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
<div class="flex space-x-2 font-heading cursor-pointer font-bold text-primary-700" >
1+
<div class="flex space-x-2 font-heading cursor-pointer font-bold text-primary-700" x-data="{ init() {
2+
window.addEventListener('localeChanged', () => {
3+
window.location.reload();
4+
});
5+
} }" x-init="init()" >
26
<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">
37
<path fill="#007a5e" d="M0 0h213.3v480H0z"/>
48
<path fill="#ce1126" d="M213.3 0h213.4v480H213.3z"/>

0 commit comments

Comments
 (0)