Skip to content

Commit 691e489

Browse files
committed
feat: update local middleware and livewire component
1 parent 173bc19 commit 691e489

File tree

11 files changed

+73
-79
lines changed

11 files changed

+73
-79
lines changed

app/Http/Middleware/LocaleMiddleware.php

+13-23
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,31 @@
77
use Closure;
88
use Illuminate\Http\Request;
99
use Illuminate\Support\Facades\Auth;
10-
use Psr\Container\ContainerExceptionInterface;
11-
use Psr\Container\NotFoundExceptionInterface;
1210
use Symfony\Component\HttpFoundation\Response;
1311

1412
final class LocaleMiddleware
1513
{
16-
/**
17-
* @throws ContainerExceptionInterface
18-
* @throws NotFoundExceptionInterface
19-
*/
2014
public function handle(Request $request, Closure $next): Response
2115
{
22-
$user = Auth::user();
23-
$lang = config('lcm.app_locale');
24-
$supportLang = config('lcm.supported_locales');
16+
$browserLocale = explode('_', (string) $request->getPreferredLanguage())[0];
17+
$currentLocale = app()->getLocale();
18+
$activeLocale = session()->get('locale');
19+
$supportedLocales = config('lcm.supported_locales');
2520

26-
if (! Auth::check()) {
21+
if (Auth::check()) {
22+
$userLocale = Auth::user()?->setting('locale', $currentLocale);
2723

28-
if (! is_null($request->server('HTTP_ACCEPT_LANGUAGE'))) {
29-
$navigatorLang = substr((string) $request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2); // @phpstan-ignore-line
30-
31-
if (in_array($navigatorLang, $supportLang)) {
32-
$lang = $navigatorLang;
33-
}
24+
if ($userLocale && $userLocale !== $currentLocale) {
25+
app()->setLocale($userLocale);
3426
}
35-
}
36-
37-
if (! is_null($user)) {
38-
39-
if (isset($user->settings['locale']) && $user->settings['locale'] != $lang) {
40-
$lang = $user->settings['locale'];
27+
} else {
28+
if (! $activeLocale && in_array($browserLocale, $supportedLocales)) {
29+
app()->setLocale($browserLocale);
30+
} else {
31+
app()->setLocale($activeLocale);
4132
}
4233
}
4334

44-
app()->setLocale($lang);
4535

4636
return $next($request);
4737
}

app/Livewire/Components/ChangeLocale.php

+21-11
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,40 @@
66

77
use Illuminate\Contracts\View\View;
88
use Illuminate\Support\Facades\Auth;
9+
use Illuminate\Support\Pluralizer;
10+
use Livewire\Attributes\Computed;
911
use Livewire\Component;
1012

1113
final class ChangeLocale extends Component
1214
{
13-
public string $selectedLang;
15+
public string $currentLocale;
1416

1517
public function mount(): void
1618
{
17-
$this->selectedLang = app()->getLocale();
19+
$this->currentLocale = app()->getLocale();
1820
}
1921

20-
public function changeLang(string $lang): void
22+
public function changeLocale(): void
2123
{
22-
$user = Auth::user();
24+
$locale = $this->currentLocale === 'fr' ? 'en' : 'fr';
2325

24-
if ($user) {
25-
$settings = $user->settings;
26-
$settings['locale'] = $lang;
27-
$user->settings = $settings;
28-
$user->save();
26+
if (Auth::check()) {
27+
Auth::user()?->settings(['locale' => $locale]);
2928
}
3029

31-
app()->setLocale($lang);
32-
$this->dispatch('localeChanged');
30+
$this->currentLocale = $locale;
31+
app()->setLocale($locale);
32+
session()->put('locale', $locale);
33+
34+
Pluralizer::useLanguage($this->currentLocale === 'fr' ? 'french' : 'english');
35+
36+
$this->redirectRoute('home', navigate: true);
37+
}
38+
39+
#[Computed]
40+
public function locale(): string
41+
{
42+
return $this->currentLocale === 'fr' ? 'English' : 'Français';
3343
}
3444

3545
public function render(): View

app/Models/User.php

+2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@
4646
* @property string | null $bio
4747
* @property string | null $website
4848
* @property string | null $banned_reason
49+
* @property array $settings
4950
* @property Carbon | null $email_verified_at
5051
* @property Carbon | null $last_login_at
5152
* @property Carbon | null $banned_at
5253
* @property Collection | Activity[] $activities
54+
* @property-read Collection | SocialAccount[] $providers
5355
*/
5456
final class User extends Authenticatable implements FilamentUser, HasAvatar, HasMedia, HasName, MustVerifyEmail
5557
{

app/Traits/HasSettings.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
trait HasSettings
88
{
9-
public function setting(string $name, string $default): string
9+
public function setting(string $name, string $default): mixed
1010
{
1111
if ($this->settings && array_key_exists($name, $this->settings)) {
1212
return $this->settings[$name];

config/lcm.php

-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
'web_hook' => env('SLACK_WEBHOOK_URL', ''),
2727
],
2828

29-
'app_locale' => env('APP_LOCALE', 'fr'),
30-
3129
'supported_locales' => ['fr', 'en'],
3230

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

lang/en/pages/article.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
return [
66

77
'title' => 'Laravel Cameroon Blog',
8-
'blog' => 'Le Blog de Laravel Cameroun',
8+
'blog' => 'The Laravel Cameroon Blog',
99
'blog_summary' => 'All the latest articles, tips and tutorials published just for you.',
1010
'about_author' => 'About author',
1111
'next_article' => 'Next article',

lang/en/pages/auth.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
'heading' => 'Open your mind and discover new horizons.',
2424
'quote' => 'A lone developer is like an isolated node—limited in reach, influence, and growth. Just as software thrives on interconnected components, so do developers flourish in the collaborative ecosystem of a community.',
2525
'quote_authors' => 'by Andrew Hunt and David Thomas',
26+
'podcast' => 'Podcast',
27+
'podcast_description' => 'Follow podcasts on different topics with freelancers, developers, entrepreneurs, etc.',
28+
'discussion' => 'Discussions',
29+
'discussion_description' => 'Take part in open discussions and debates with several other participants.',
30+
'snippet' => 'Snippets code',
31+
'snippet_description' => 'Share source code for different languages to help other developers.',
32+
'premium' => 'Premium',
33+
'premium_description' => 'Become premium, support the community and access private content and source code.',
2634
],
27-
'podcast' => 'Podcast',
28-
'podcast_description' => 'Follow podcasts on different topics with freelancers, developers, entrepreneurs, etc.',
29-
'discussion' => 'Discussions',
30-
'discussion_description' => 'Take part in open discussions and debates with several other participants.',
31-
'snippet' => 'Snippets code',
32-
'snippet_description' => 'Share source code for different languages to help other developers.',
33-
'premium' => 'Premium',
34-
'premium_description' => 'Become premium, support the community and access private content and source code.',
3535
],
3636

3737
'forgot' => [

lang/fr/pages/article.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
return [
66

77
'title' => 'Blog Laravel Cameroun',
8-
'blog' => 'The Laravel Cameroon Blog',
8+
'blog' => 'Le Blog de Laravel Cameroun',
99
'blog_summary' => 'Tous les articles, tips et tutoriels récemment publiés juste pour vous.',
1010
'about_author' => 'À propos de l’auteur',
1111
'next_article' => 'Article suivant',

lang/fr/pages/auth.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@
3737
'forgot' => [
3838
'page_title' => 'Mot de passe oublié',
3939
'title' => 'Réinitialisation du mot de passe',
40-
'description' => "Mot de passe oublié ? Aucun problème. Communiquez-nous simplement votre adresse e-mail et nous vous
41-
enverrons par e-mail un lien de réinitialisation de mot de passe qui vous permettra d'en choisir un
42-
nouveau.",
40+
'description' => "Mot de passe oublié ? Aucun problème. Communiquez-nous simplement votre adresse e-mail et nous vous enverrons par e-mail un lien de réinitialisation de mot de passe qui vous permettra d'en choisir un nouveau.",
4341
],
4442

4543
'reset' => [
@@ -49,9 +47,7 @@
4947

5048
'verify' => [
5149
'page_title' => "Vérification de l'adresse e-mail",
52-
'description' => "Merci pour votre inscription ! Avant de commencer, pourriez-vous vérifier votre adresse e-mail en
53-
cliquant sur le lien que nous venons de vous envoyer par e-mail ? Si vous n'avez pas reçu l'e-mail, nous
54-
nous ferons un plaisir de vous en envoyer un autre.",
50+
'description' => "Merci pour votre inscription ! Avant de commencer, pourriez-vous vérifier votre adresse e-mail en cliquant sur le lien que nous venons de vous envoyer par e-mail ? Si vous n'avez pas reçu l'e-mail, nous nous ferons un plaisir de vous en envoyer un autre.",
5551
'success' => "Un nouveau lien de vérification a été envoyé à l'adresse e-mail que vous avez fournie lors de l'inscription ou la modification de votre adresse.",
5652
'submit' => "Renvoyer l'e-mail de vérification",
5753
],

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

+16-14
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,22 @@ class="ml-2 size-6 rounded-full"
5353
{{ __('global.joins_us.description') }}
5454
</p>
5555
</div>
56-
<div class="mt-6 flex items-center text-sm font-medium space-x-4 sm:space-x-6">
57-
<x-link href="https://discord.gg/KNp6brbyVD" class="inline-flex items-center gap-2 text-gray-300 hover:text-white">
58-
<x-icon.discord class="size-5 text-[#5865F2]" aria-hidden="true" />
59-
Discord
60-
</x-link>
61-
<x-link href="https://t.me/laravelcameroun" class="inline-flex items-center gap-2 text-gray-300 hover:text-white">
62-
<x-icon.telegram class="size-5 text-[#34AADF]" aria-hidden="true" />
63-
Telegram
64-
</x-link>
65-
<x-link href="https://chat.whatsapp.com/G8e98Ms0MgSLEOGd3Uai1i" class="inline-flex items-center gap-2 text-gray-300 hover:text-white">
66-
<x-icon.whatsapp class="size-5 text-[#28D146]" aria-hidden="true" />
67-
WhatsApp
68-
</x-link>
56+
<div class="mt-6 space-y-6">
57+
<div class="flex items-center text-sm font-medium space-x-4 sm:space-x-6">
58+
<x-link href="https://discord.gg/KNp6brbyVD" class="inline-flex items-center gap-2 text-gray-300 hover:text-white">
59+
<x-icon.discord class="size-5 text-[#5865F2]" aria-hidden="true" />
60+
Discord
61+
</x-link>
62+
<x-link href="https://t.me/laravelcameroun" class="inline-flex items-center gap-2 text-gray-300 hover:text-white">
63+
<x-icon.telegram class="size-5 text-[#34AADF]" aria-hidden="true" />
64+
Telegram
65+
</x-link>
66+
<x-link href="https://chat.whatsapp.com/G8e98Ms0MgSLEOGd3Uai1i" class="inline-flex items-center gap-2 text-gray-300 hover:text-white">
67+
<x-icon.whatsapp class="size-5 text-[#28D146]" aria-hidden="true" />
68+
WhatsApp
69+
</x-link>
70+
</div>
71+
<livewire:components.change-locale />
6972
</div>
7073
</div>
7174
</div>
@@ -99,7 +102,6 @@ class="ml-2 size-6 rounded-full"
99102
<x-icon.youtube class="size-6" aria-hidden="true" />
100103
</x-link>
101104
</div>
102-
<livewire:components.change-locale />
103105
</div>
104106
</x-container>
105107
</div>
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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()" >
6-
<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">
1+
<button
2+
type="button"
3+
class="group inline-flex text-sm px-3 py-1.5 rounded-md bg-white/10 items-center gap-2 text-gray-300 hover:text-white"
4+
wire:click="changeLocale"
5+
>
6+
<svg id="flag-icons-cm" viewBox="0 0 640 480" class="size-5 rounded">
77
<path fill="#007a5e" d="M0 0h213.3v480H0z"/>
88
<path fill="#ce1126" d="M213.3 0h213.4v480H213.3z"/>
99
<path fill="#fcd116" d="M426.7 0H640v480H426.7z"/>
@@ -18,9 +18,5 @@
1818
<use xlink:href="#cm-b" width="100%" height="100%" transform="rotate(-72)"/>
1919
</g>
2020
</svg>
21-
@if($selectedLang == 'fr')
22-
<button wire:click="changeLang('en')">EN</button>
23-
@else
24-
<button wire:click="changeLang('fr')">FR</button>
25-
@endif
26-
</div>
21+
<span>{{ $this->locale }}</span>
22+
</button>

0 commit comments

Comments
 (0)