feat: locale

This commit is contained in:
2026-05-21 21:32:20 +02:00
parent 19e65e36f3
commit 8e5a50dd2b
21 changed files with 337 additions and 32 deletions
+18
View File
@@ -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];
}
}
+17
View File
@@ -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];
}
}
+17
View File
@@ -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];
}
}