diff --git a/apps/web/public/static/locales/en/common.json b/apps/web/public/static/locales/en/common.json index 53fbce5eef..9d0d0f85ac 100644 --- a/apps/web/public/static/locales/en/common.json +++ b/apps/web/public/static/locales/en/common.json @@ -10,6 +10,8 @@ "accept_invitation": "Accept Invitation", "max_characters": "Max. Characters", "min_characters": "Min. Characters", + "min_characters_required": "Min. {{count}} characters required", + "max_characters_allowed": "Max. {{count}} characters allowed", "calcom_explained": "{{appName}} provides scheduling infrastructure for absolutely everyone.", "calcom_explained_new_user": "Finish setting up your {{appName}} account! You're just a few steps away from solving all your scheduling problems.", "have_any_questions": "Have questions? We're here to help.", diff --git a/packages/features/bookings/Booker/components/hooks/useBookingForm.ts b/packages/features/bookings/Booker/components/hooks/useBookingForm.ts index e88e2701fc..44a7c24d38 100644 --- a/packages/features/bookings/Booker/components/hooks/useBookingForm.ts +++ b/packages/features/bookings/Booker/components/hooks/useBookingForm.ts @@ -48,6 +48,7 @@ export const useBookingForm = ({ ? getBookingResponsesSchema({ bookingFields: event.bookingFields, view: rescheduleUid ? "reschedule" : "booking", + translateFn: (key: string, options?: Record) => t(key, options ?? {}), }) : // Fallback until event is loaded. z.object({}), diff --git a/packages/features/bookings/lib/getBookingResponsesSchema.ts b/packages/features/bookings/lib/getBookingResponsesSchema.ts index a637f45e5d..681e4672f2 100644 --- a/packages/features/bookings/lib/getBookingResponsesSchema.ts +++ b/packages/features/bookings/lib/getBookingResponsesSchema.ts @@ -10,7 +10,8 @@ import { bookingResponses, emailSchemaRefinement } from "@calcom/prisma/zod-util // eslint-disable-next-line @typescript-eslint/ban-types type View = ALL_VIEWS | (string & {}); type BookingFields = (z.infer & z.BRAND<"HAS_SYSTEM_FIELDS">) | null; -type CommonParams = { bookingFields: BookingFields; view: View }; +type TranslationFunction = (key: string, options?: Record) => string; +type CommonParams = { bookingFields: BookingFields; view: View; translateFn?: TranslationFunction }; export const bookingResponse = dbReadResponseSchema; export const bookingResponsesDbSchema = z.record(dbReadResponseSchema); @@ -23,24 +24,35 @@ const ensureValidPhoneNumber = (value: string) => { // Replace the space(s) in the beginning with + as it is supposed to be provided in the beginning only return value.replace(/^ +/, "+"); }; -export const getBookingResponsesPartialSchema = ({ bookingFields, view }: CommonParams) => { +export const getBookingResponsesPartialSchema = ({ bookingFields, view, translateFn }: CommonParams) => { const schema = bookingResponses.unwrap().partial().and(catchAllSchema); - return preprocess({ schema, bookingFields, isPartialSchema: true, view }); + return preprocess({ schema, bookingFields, isPartialSchema: true, view, translateFn }); }; // Should be used when we know that not all fields responses are present // - Can happen when we are parsing the prefill query string // - Can happen when we are parsing a booking's responses (which was created before we added a new required field) -export default function getBookingResponsesSchema({ bookingFields, view }: CommonParams) { +export default function getBookingResponsesSchema({ bookingFields, view, translateFn }: CommonParams) { const schema = bookingResponses.and(z.record(z.any())); - return preprocess({ schema, bookingFields, isPartialSchema: false, view }); + return preprocess({ schema, bookingFields, isPartialSchema: false, view, translateFn }); } // Should be used when we want to check if the optional fields are entered and valid as well -export function getBookingResponsesSchemaWithOptionalChecks({ bookingFields, view }: CommonParams) { +export function getBookingResponsesSchemaWithOptionalChecks({ + bookingFields, + view, + translateFn, +}: CommonParams) { const schema = bookingResponses.and(z.record(z.any())); - return preprocess({ schema, bookingFields, isPartialSchema: false, view, checkOptional: true }); + return preprocess({ + schema, + bookingFields, + isPartialSchema: false, + view, + checkOptional: true, + translateFn, + }); } // TODO: Move preprocess of `booking.responses` to FormBuilder schema as that is going to parse the fields supported by FormBuilder @@ -51,6 +63,7 @@ function preprocess({ isPartialSchema, view: currentView, checkOptional = false, + translateFn, }: CommonParams & { schema: T; // It is useful when we want to prefill the responses with the partial values. Partial can be in 2 ways @@ -150,7 +163,10 @@ function preprocess({ return isValidPhoneNumber(val); }); // Tag the message with the input name so that the message can be shown at appropriate place - const m = (message: string) => `{${bookingField.name}}${message}`; + const m = (message: string, options?: Record) => { + const translatedMessage = translateFn ? translateFn(message, options) : message; + return `{${bookingField.name}}${translatedMessage}`; + }; const views = bookingField.views; const isFieldApplicableToCurrentView = currentView === "ALL_VIEWS" ? true : views ? views.find((view) => view.id === currentView) : true; diff --git a/packages/features/form-builder/schema.ts b/packages/features/form-builder/schema.ts index 9f248075df..345feba7cf 100644 --- a/packages/features/form-builder/schema.ts +++ b/packages/features/form-builder/schema.ts @@ -116,7 +116,7 @@ export const fieldTypesSchemaMap: Partial< response: string; isPartialSchema: boolean; ctx: z.RefinementCtx; - m: (key: string) => string; + m: (key: string, options?: Record) => string; }) => void; } > @@ -203,16 +203,18 @@ export const fieldTypesSchemaMap: Partial< const hasExceededMaxLength = value.length > maxLength; const hasNotReachedMinLength = value.length < minLength; if (hasExceededMaxLength) { + const message = m(`max_characters_allowed`, { count: maxLength }); ctx.addIssue({ code: z.ZodIssueCode.custom, - message: m(`Max. ${maxLength} characters allowed`), + message, }); return; } if (hasNotReachedMinLength) { + const message = m(`min_characters_required`, { count: minLength }); ctx.addIssue({ code: z.ZodIssueCode.custom, - message: m(`Min. ${minLength} characters required`), + message, }); return; }