feat: locale
This commit is contained in:
@@ -35,6 +35,11 @@ class UserForm
|
||||
->required()
|
||||
->unique(ignoreRecord: true)
|
||||
->maxLength(255),
|
||||
Select::make('locale')
|
||||
->label(__('admin.users.fields.locale'))
|
||||
->options(self::localeOptions())
|
||||
->default('en')
|
||||
->required(),
|
||||
FileUpload::make('avatar_url')
|
||||
->label(__('admin.users.fields.avatar'))
|
||||
->image()
|
||||
@@ -136,4 +141,18 @@ class UserForm
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private static function localeOptions(): array
|
||||
{
|
||||
$labels = __('admin.users.locales');
|
||||
|
||||
return collect(config('app.supported_locales', ['fr', 'en']))
|
||||
->mapWithKeys(fn (string $locale): array => [
|
||||
$locale => is_array($labels) ? ($labels[$locale] ?? $locale) : $locale,
|
||||
])
|
||||
->all();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,6 +65,10 @@ class AuthController extends Controller
|
||||
], 401);
|
||||
}
|
||||
|
||||
if (isset($data['locale']) && $user->locale !== $data['locale']) {
|
||||
$user->forceFill(['locale' => $data['locale']])->save();
|
||||
}
|
||||
|
||||
if (! $user->hasVerifiedEmail()) {
|
||||
return response()->json([
|
||||
'message' => __('api.auth.email_not_verified'),
|
||||
@@ -162,9 +166,13 @@ class AuthController extends Controller
|
||||
|
||||
public function showResetPasswordForm(Request $request, string $token): View
|
||||
{
|
||||
$email = (string) $request->query('email', '');
|
||||
$locale = $this->localeForEmail($email);
|
||||
|
||||
return view('auth.reset-password', [
|
||||
'appName' => config('app.name'),
|
||||
'email' => (string) $request->query('email', ''),
|
||||
'email' => $email,
|
||||
'locale' => $locale,
|
||||
'passwordReset' => false,
|
||||
'token' => $token,
|
||||
]);
|
||||
@@ -172,6 +180,9 @@ class AuthController extends Controller
|
||||
|
||||
public function resetPasswordFromView(ResetPasswordRequest $request): RedirectResponse|View
|
||||
{
|
||||
$locale = $this->localeForEmail((string) $request->validated('email'));
|
||||
app()->setLocale($locale);
|
||||
|
||||
$status = $this->resetPasswordFor($request->safe());
|
||||
|
||||
if ($status !== Password::PASSWORD_RESET) {
|
||||
@@ -183,6 +194,7 @@ class AuthController extends Controller
|
||||
return view('auth.reset-password', [
|
||||
'appName' => config('app.name'),
|
||||
'email' => strtolower($request->validated('email')),
|
||||
'locale' => $locale,
|
||||
'passwordReset' => true,
|
||||
'token' => '',
|
||||
]);
|
||||
@@ -298,6 +310,11 @@ class AuthController extends Controller
|
||||
);
|
||||
}
|
||||
|
||||
private function localeForEmail(string $email): string
|
||||
{
|
||||
return User::where('email', strtolower($email))->value('locale') ?: 'en';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @return array<string, mixed>
|
||||
@@ -307,6 +324,7 @@ class AuthController extends Controller
|
||||
$attributes = Arr::only($data, [
|
||||
'name',
|
||||
'avatar_url',
|
||||
'locale',
|
||||
'height',
|
||||
'bio',
|
||||
'sex',
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class LoginRequest extends FormRequest
|
||||
{
|
||||
@@ -23,7 +24,24 @@ class LoginRequest extends FormRequest
|
||||
{
|
||||
return [
|
||||
'email' => ['required', 'string'],
|
||||
'locale' => ['sometimes', 'string', Rule::in(config('app.supported_locales', ['fr', 'en']))],
|
||||
'password' => ['required', 'string'],
|
||||
];
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
if ($this->has('locale')) {
|
||||
$this->merge([
|
||||
'locale' => $this->normalizeLocale((string) $this->input('locale')),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function normalizeLocale(string $locale): string
|
||||
{
|
||||
$locale = strtolower(str_replace('_', '-', $locale));
|
||||
|
||||
return explode('-', $locale)[0];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ class RegisterRequest extends FormRequest
|
||||
'unique:users,name',
|
||||
],
|
||||
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
|
||||
'locale' => ['sometimes', 'string', Rule::in(config('app.supported_locales', ['fr', 'en']))],
|
||||
'password' => ['required', 'string', 'min:8'],
|
||||
'avatar' => ['sometimes', 'nullable', 'image', 'max:2048'],
|
||||
'bio' => ['nullable', 'string'],
|
||||
@@ -56,4 +57,20 @@ class RegisterRequest extends FormRequest
|
||||
'avatar.max' => __('api.validation.image_max_2mb'),
|
||||
];
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
if ($this->has('locale')) {
|
||||
$this->merge([
|
||||
'locale' => $this->normalizeLocale((string) $this->input('locale')),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function normalizeLocale(string $locale): string
|
||||
{
|
||||
$locale = strtolower(str_replace('_', '-', $locale));
|
||||
|
||||
return explode('-', $locale)[0];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ class UpdateUserRequest extends FormRequest
|
||||
return [
|
||||
'bio' => ['sometimes', 'nullable', 'string'],
|
||||
'height' => ['sometimes', 'nullable', 'integer'],
|
||||
'locale' => ['sometimes', 'string', Rule::in(config('app.supported_locales', ['fr', 'en']))],
|
||||
'avatar' => ['sometimes', 'nullable', 'image', 'max:2048'],
|
||||
'nutritionGoals' => ['sometimes', 'array'],
|
||||
'nutritionGoals.calories' => ['required_with:nutritionGoals', 'integer', 'min:0', 'max:100000'],
|
||||
@@ -46,4 +47,20 @@ class UpdateUserRequest extends FormRequest
|
||||
'nutritionGoals.*.max' => __('api.validation.nutrition_goals_max'),
|
||||
];
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
if ($this->has('locale')) {
|
||||
$this->merge([
|
||||
'locale' => $this->normalizeLocale((string) $this->input('locale')),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function normalizeLocale(string $locale): string
|
||||
{
|
||||
$locale = strtolower(str_replace('_', '-', $locale));
|
||||
|
||||
return explode('-', $locale)[0];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ class UserResource extends JsonResource
|
||||
return [
|
||||
'name' => $this->name,
|
||||
'email' => $this->email,
|
||||
'locale' => $this->locale,
|
||||
'emailVerified' => $this->hasVerifiedEmail(),
|
||||
'emailVerifiedAt' => $this->email_verified_at?->toISOString(),
|
||||
'height' => $this->height,
|
||||
|
||||
@@ -27,7 +27,7 @@ class ResetPasswordMail extends Mailable
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
return new Envelope(
|
||||
subject: 'Réinitialise ton mot de passe Bowli',
|
||||
subject: trans('mail.reset_password.subject', [], $this->mailLocale()),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ class ResetPasswordMail extends Mailable
|
||||
view: 'emails.reset-password',
|
||||
with: [
|
||||
'expiresInMinutes' => (int) config('auth.passwords.users.expire', 60),
|
||||
'locale' => $this->mailLocale(),
|
||||
'resetUrl' => $this->resetUrl,
|
||||
'userName' => $this->user->name,
|
||||
],
|
||||
@@ -55,4 +56,9 @@ class ResetPasswordMail extends Mailable
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
private function mailLocale(): string
|
||||
{
|
||||
return $this->user->preferredLocale() ?? 'en';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ class VerifyAccount extends Mailable
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
return new Envelope(
|
||||
subject: 'Vérifie ton compte Daily Meal',
|
||||
subject: trans('mail.verify_account.subject', [], $this->mailLocale()),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ class VerifyAccount extends Mailable
|
||||
view: 'emails.verify-account',
|
||||
with: [
|
||||
'expiresInMinutes' => (int) config('auth.verification.expire', 60),
|
||||
'locale' => $this->mailLocale(),
|
||||
'userName' => $this->user->name,
|
||||
'verificationUrl' => $this->verificationUrl,
|
||||
],
|
||||
@@ -55,4 +56,9 @@ class VerifyAccount extends Mailable
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
private function mailLocale(): string
|
||||
{
|
||||
return $this->user->preferredLocale() ?? 'en';
|
||||
}
|
||||
}
|
||||
|
||||
+8
-1
@@ -9,6 +9,7 @@ use App\Enums\WeightGoal;
|
||||
use Filament\Models\Contracts\FilamentUser;
|
||||
use Filament\Panel;
|
||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Contracts\Translation\HasLocalePreference;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
@@ -18,7 +19,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
|
||||
class User extends Authenticatable implements FilamentUser, MustVerifyEmail
|
||||
class User extends Authenticatable implements FilamentUser, HasLocalePreference, MustVerifyEmail
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
||||
use HasApiTokens, HasFactory, HasUlids, Notifiable;
|
||||
@@ -31,6 +32,7 @@ class User extends Authenticatable implements FilamentUser, MustVerifyEmail
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'locale',
|
||||
'email_verified_at',
|
||||
'password',
|
||||
'avatar_url',
|
||||
@@ -115,6 +117,11 @@ class User extends Authenticatable implements FilamentUser, MustVerifyEmail
|
||||
->all();
|
||||
}
|
||||
|
||||
public function preferredLocale(): ?string
|
||||
{
|
||||
return $this->locale ?: 'en';
|
||||
}
|
||||
|
||||
public function isAdmin()
|
||||
{
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user