From 5854d973eaef053a3a7add3da7e505d9c3a05031 Mon Sep 17 00:00:00 2001 From: Stavros Date: Tue, 15 Jul 2025 02:15:17 +0300 Subject: [PATCH] i18n: internationalize required error --- frontend/src/components/auth/login-form.tsx | 6 ++++++ frontend/src/components/auth/totp-form.tsx | 8 ++++++++ frontend/src/lib/i18n/locales/en-US.json | 4 +++- frontend/src/lib/i18n/locales/en.json | 4 +++- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/auth/login-form.tsx b/frontend/src/components/auth/login-form.tsx index 3a1b915..e788262 100644 --- a/frontend/src/components/auth/login-form.tsx +++ b/frontend/src/components/auth/login-form.tsx @@ -12,6 +12,7 @@ import { } from "../ui/form"; import { Button } from "../ui/button"; import { loginSchema, LoginSchema } from "@/schemas/login-schema"; +import z from "zod"; interface Props { onSubmit: (data: LoginSchema) => void; @@ -22,6 +23,11 @@ export const LoginForm = (props: Props) => { const { onSubmit, loading } = props; const { t } = useTranslation(); + z.config({ + customError: (iss) => + iss.input === undefined ? t("fieldRequired") : t("invalidInput"), + }); + const form = useForm({ resolver: zodResolver(loginSchema), }); diff --git a/frontend/src/components/auth/totp-form.tsx b/frontend/src/components/auth/totp-form.tsx index 1194fd5..ded33ad 100644 --- a/frontend/src/components/auth/totp-form.tsx +++ b/frontend/src/components/auth/totp-form.tsx @@ -8,6 +8,8 @@ import { import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { totpSchema, TotpSchema } from "@/schemas/totp-schema"; +import { useTranslation } from "react-i18next"; +import z from "zod"; interface Props { formId: string; @@ -17,6 +19,12 @@ interface Props { export const TotpForm = (props: Props) => { const { formId, onSubmit, loading } = props; + const { t } = useTranslation(); + + z.config({ + customError: (iss) => + iss.input === undefined ? t("fieldRequired") : t("invalidInput"), + }); const form = useForm({ resolver: zodResolver(totpSchema), diff --git a/frontend/src/lib/i18n/locales/en-US.json b/frontend/src/lib/i18n/locales/en-US.json index 05386de..74e422f 100644 --- a/frontend/src/lib/i18n/locales/en-US.json +++ b/frontend/src/lib/i18n/locales/en-US.json @@ -51,5 +51,7 @@ "failedToFetchProvidersTitle": "Failed to load authentication providers. Please check your configuration.", "errorTitle": "An error occurred", "errorSubtitle": "An error occurred while trying to perform this action. Please check the console for more information.", - "forgotPasswordMessage": "You can reset your password by changing the `USERS` environment variable." + "forgotPasswordMessage": "You can reset your password by changing the `USERS` environment variable.", + "fieldRequired": "This field is required", + "invalidInput": "Invalid input" } \ No newline at end of file diff --git a/frontend/src/lib/i18n/locales/en.json b/frontend/src/lib/i18n/locales/en.json index 05386de..74e422f 100644 --- a/frontend/src/lib/i18n/locales/en.json +++ b/frontend/src/lib/i18n/locales/en.json @@ -51,5 +51,7 @@ "failedToFetchProvidersTitle": "Failed to load authentication providers. Please check your configuration.", "errorTitle": "An error occurred", "errorSubtitle": "An error occurred while trying to perform this action. Please check the console for more information.", - "forgotPasswordMessage": "You can reset your password by changing the `USERS` environment variable." + "forgotPasswordMessage": "You can reset your password by changing the `USERS` environment variable.", + "fieldRequired": "This field is required", + "invalidInput": "Invalid input" } \ No newline at end of file