Skip to content

Commit

Permalink
feat: Add ability to pick preferred locale for a user (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
danjohnson95 authored Jan 17, 2025
1 parent 371e372 commit 3fe7c89
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 1 deletion.
13 changes: 13 additions & 0 deletions config/cachet.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,17 @@
'prune_logs_after_days' => 30,
],
],

/*
|--------------------------------------------------------------------------
| Cachet Supported Locales
|--------------------------------------------------------------------------
|
| Configure which locales are supported by Cachet.
|
*/
'supported_locales' => [
'en' => 'English',
'en_GB' => 'English (UK)',
],
];
28 changes: 28 additions & 0 deletions database/migrations/2025_01_16_225001_add_locale_to_user.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

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

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('preferred_locale');
});
}
};
2 changes: 2 additions & 0 deletions resources/lang/en/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
'email_label' => 'Email Address',
'password_label' => 'Password',
'password_confirmation_label' => 'Confirm Password',
'preferred_locale' => 'Preferred Locale',
'preferred_locale_system_default' => 'System Default',
'is_admin_label' => 'Admin',
],
];
2 changes: 2 additions & 0 deletions src/CachetDashboardServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cachet;

use Cachet\Http\Middleware\SetAppLocale;
use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
Expand Down Expand Up @@ -86,6 +87,7 @@ public function panel(Panel $panel): Panel
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
SetAppLocale::class,
])
->authMiddleware([
Authenticate::class,
Expand Down
8 changes: 8 additions & 0 deletions src/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public static function form(Form $form): Form
->same('password')
->label(__('cachet::user.form.password_confirmation_label')),

Forms\Components\Select::make('preferred_locale')
->selectablePlaceholder(false)
->options([
null => __('cachet::user.form.preferred_locale_system_default'),
...config('cachet.supported_locales')
])
->label(__('cachet::user.form.preferred_locale')),

Forms\Components\Toggle::make('is_admin')
->label(__('cachet::user.form.is_admin_label'))
->disabled(fn (?User $record) => $record?->is(auth()->user())),
Expand Down
23 changes: 23 additions & 0 deletions src/Http/Middleware/SetAppLocale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Cachet\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class SetAppLocale
{
public function handle(Request $request, Closure $next)
{
/** @var ?\Cachet\Models\User */
$user = $request->user();

if ($user) {
app()->setLocale($user->preferredLocale() ?? $request->getPreferredLanguage(
array_keys(config('cachet.supported_locales'))
));
}

return $next($request);
}
}
10 changes: 9 additions & 1 deletion src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Cachet\Concerns\CachetUser;
use Cachet\Database\Factories\UserFactory;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
Expand All @@ -17,8 +18,9 @@
* @property string $email
* @property string $password
* @property bool $is_admin
* @property string $preferred_locale
*/
class User extends Authenticatable implements CachetUser, MustVerifyEmail
class User extends Authenticatable implements CachetUser, MustVerifyEmail, HasLocalePreference
{
/** @use HasFactory<\Cachet\Database\Factories\UserFactory> */
use HasApiTokens, HasFactory, Notifiable;
Expand All @@ -33,6 +35,7 @@ class User extends Authenticatable implements CachetUser, MustVerifyEmail
'email',
'password',
'is_admin',
'preferred_locale',
];

/**
Expand Down Expand Up @@ -74,4 +77,9 @@ protected static function newFactory(): Factory
{
return UserFactory::new();
}

public function preferredLocale()
{
return $this->preferred_locale;
}
}

0 comments on commit 3fe7c89

Please sign in to comment.