File tree Expand file tree Collapse file tree 7 files changed +34
-34
lines changed Expand file tree Collapse file tree 7 files changed +34
-34
lines changed Original file line number Diff line number Diff line change @@ -20,21 +20,27 @@ final class LocaleMiddleware
20
20
public function handle (Request $ request , Closure $ next ): Response
21
21
{
22
22
$ 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
+
25
26
if (! Auth::check ()) {
27
+
26
28
if (! is_null ($ request ->server ('HTTP_ACCEPT_LANGUAGE ' ))) {
27
29
$ navigatorLang = substr ($ request ->server ('HTTP_ACCEPT_LANGUAGE ' ), 0 , 2 );
30
+
28
31
if (in_array ($ navigatorLang , $ supportLang )) {
29
32
$ lang = $ navigatorLang ;
30
33
}
31
34
}
32
35
}
36
+
33
37
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 ' ];
36
41
}
37
42
}
43
+
38
44
app ()->setLocale ($ lang );
39
45
40
46
return $ next ($ request );
Original file line number Diff line number Diff line change 5
5
namespace App \Livewire \Components ;
6
6
7
7
use Illuminate \Contracts \View \View ;
8
- use Illuminate \Routing \Redirector ;
9
8
use Illuminate \Support \Facades \Auth ;
10
9
use Livewire \Component ;
11
- use Symfony \Component \HttpFoundation \RedirectResponse ;
12
10
13
- final class Locale extends Component
11
+ final class ChangeLocale extends Component
14
12
{
15
13
public string $ selectedLang ;
16
14
@@ -19,22 +17,23 @@ public function mount(): void
19
17
$ this ->selectedLang = app ()->getLocale ();
20
18
}
21
19
22
- public function changeLang (string $ lang ): RedirectResponse | Redirector
20
+ public function changeLang (string $ lang ): void
23
21
{
24
22
$ user = Auth::user ();
23
+
25
24
if ($ user ) {
26
25
$ settings = $ user ->settings ;
27
- $ settings ['default_lang ' ] = $ lang ;
26
+ $ settings ['locale ' ] = $ lang ;
28
27
$ user ->settings = $ settings ;
29
28
$ user ->save ();
30
29
}
31
- app ()->setLocale ($ lang );
32
30
33
- return redirect ()->to (url ()->current ());
31
+ app ()->setLocale ($ lang );
32
+ $ this ->dispatch ('localeChanged ' );
34
33
}
35
34
36
35
public function render (): View
37
36
{
38
- return view ('livewire.components.locale ' );
37
+ return view ('livewire.components.change- locale ' );
39
38
}
40
39
}
Original file line number Diff line number Diff line change 18
18
'role ' => \Spatie \Permission \Middleware \RoleMiddleware::class,
19
19
'checkIfBanned ' => \App \Http \Middleware \CheckIfBanned::class,
20
20
]);
21
- $ middleware ->appendToGroup ( ' web ' , [
21
+ $ middleware ->web (append: [
22
22
LocaleMiddleware::class,
23
- \Illuminate \Session \Middleware \StartSession::class,
24
23
]);
25
24
})
26
25
->withExceptions (function (Exceptions $ exceptions ): void {
Original file line number Diff line number Diff line change 26
26
'web_hook ' => env ('SLACK_WEBHOOK_URL ' , '' ),
27
27
],
28
28
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 ' ],
33
32
34
33
'spa_url ' => env ('FRONTEND_APP_URL ' , 'http://localhost:4200 ' ),
35
34
Original file line number Diff line number Diff line change 8
8
9
9
return new class extends Migration
10
10
{
11
- private array $ tables = ['articles ' , 'discussions ' , 'threads ' ];
12
-
13
11
public function up (): void
14
12
{
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
+ }
26
19
}
27
20
28
21
public function down (): void
29
22
{
30
- foreach ($ this -> tables as $ tab ) {
23
+ foreach ([ ' articles ' , ' discussions ' , ' threads ' ] as $ tab ) {
31
24
Schema::table ($ tab , function (Blueprint $ table ): void {
32
25
$ table ->dropColumn ('locale ' );
33
26
});
Original file line number Diff line number Diff line change @@ -99,7 +99,7 @@ class="ml-2 size-6 rounded-full"
99
99
<x-icon .youtube class =" size-6" aria-hidden =" true" />
100
100
</x-link >
101
101
</div >
102
- <livewire:components .locale ></livewire:components .locale >
102
+ <livewire:components .change- locale ></livewire:components .change- locale >
103
103
</div >
104
104
</x-container >
105
105
</div >
Original file line number Diff line number Diff line change 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()" >
2
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" >
3
7
<path fill =" #007a5e" d =" M0 0h213.3v480H0z" />
4
8
<path fill =" #ce1126" d =" M213.3 0h213.4v480H213.3z" />
You can’t perform that action at this time.
0 commit comments