Skip to content

Commit

Permalink
Merge pull request #175 from Larsklopstra/main
Browse files Browse the repository at this point in the history
User editable locale
  • Loading branch information
Cannonb4ll authored Oct 18, 2022
2 parents 6c62a39 + c4315a6 commit 524e4d5
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Kernel extends HttpKernel
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\PasswordProtected::class,
\App\Http\Middleware\Localize::class,
\App\Http\Middleware\LocalizeDates::class,
],

Expand Down
7 changes: 7 additions & 0 deletions app/Http/Livewire/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function mount(): void
'email' => $this->user->email,
'notification_settings' => $this->user->notification_settings,
'per_page_setting' => $this->user->per_page_setting ?? [5],
'locale' => $this->user->locale,
'date_locale' => $this->user->date_locale,
]);
}
Expand All @@ -53,6 +54,7 @@ protected function getFormSchema(): array
])
->unique(table: User::class, column: 'username', ignorable: auth()->user()),
Forms\Components\TextInput::make('email')->label(trans('auth.email'))->required()->email(),
Forms\Components\Select::make('locale')->label(trans('auth.locale'))->options($this->locales)->placeholder(trans('auth.locale_null_value')),
Forms\Components\Select::make('date_locale')->label(trans('auth.date_locale'))->options($this->locales)->placeholder(trans('auth.date_locale_null_value')),
])->collapsible(),

Expand Down Expand Up @@ -92,9 +94,14 @@ public function submit(): void
'username' => $data['username'],
'notification_settings' => $data['notification_settings'],
'per_page_setting' => $data['per_page_setting'],
'locale' => $data['locale'],
'date_locale' => $data['date_locale'],
]);

if ($this->user->wasChanged('locale', 'date_locale')) {
$this->notify('success', 'Refresh the page to show locale changes.');
}

$this->notify('success', 'Profile has been saved.');
}

Expand Down
25 changes: 25 additions & 0 deletions app/Http/Middleware/Localize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class Localize
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if (auth()->check()) {
app()->setLocale(auth()->user()->locale ?? config('app.locale'));
}

return $next($request);
}
}
1 change: 1 addition & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class User extends Authenticatable implements FilamentUser, HasAvatar, MustVerif
'password',
'notification_settings',
'per_page_setting',
'locale',
'date_locale',
];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('locale')->nullable()->after('remember_token');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
};
3 changes: 3 additions & 0 deletions lang/en/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@
'date_locale' => 'Date locale',
'date_locale_null_value' => 'Use app locale',

'locale' => 'Locale',
'locale_null_value' => 'Use default locale',

];
3 changes: 3 additions & 0 deletions lang/nl/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@

'date_locale' => 'Datum & tijd vertaling',
'date_locale_null_value' => 'Gebruik app voorkeurstaal',

'locale' => 'Taal',
'locale_null_value' => 'Gebruik standaard taal',
];

0 comments on commit 524e4d5

Please sign in to comment.