Scheduling infrastructure for absolutely everyone.
fix: Allow i18n of form builder max/min character count. Earlier strings missed due to variable. (#22367)
Anshumancanrock5d8c4ad13d7a
diff --git a/apps/web/public/static/locales/en/common.json b/apps/web/public/static/locales/en/common.jsonindex 53fbce5eefb..9d0d0f85ac6 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.tsindex e88e2701fc5..44a7c24d38e 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<string, unknown>) => 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.tsindex a637f45e5d6..681e4672f26 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-typestype View = ALL_VIEWS | (string & {});type BookingFields = (z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">) | null;-type CommonParams = { bookingFields: BookingFields; view: View };+type TranslationFunction = (key: string, options?: Record<string, unknown>) => 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 onlyreturn 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<T extends z.ZodType>({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<T extends z.ZodType>({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<string, unknown>) => {+ 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.tsindex 9f248075df0..345feba7cfe 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, unknown>) => 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;}