-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: translate survey and migration script #2290
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 2 Ignored Deployments
|
Thank you for following the naming conventions for pull request titles! 🙏 |
packages/lib/i18n/utils.tsConsider using destructuring to improve readability and reduce redundancy. This can be applied in several places in the code where properties are being accessed multiple times from the same object. // For example, in the translateQuestion function, instead of accessing question.type multiple times, you can destructure it at the beginning of the function.
const { type } = question;
switch (type) {
case "openText":
// ...
case "multipleChoiceSingle":
case "multipleChoiceMulti":
// ...
// ...
}
Consider using a lookup object instead of multiple if-else or switch-case statements. This can improve performance as lookup in an object is generally faster than multiple conditional checks. // For example, in the translateQuestion function, instead of using a switch-case statement, you can define a lookup object.
const questionTypeHandlers = {
"openText": handleOpenTextQuestion,
"multipleChoiceSingle": handleMultipleChoiceSingleQuestion,
"multipleChoiceMulti": handleMultipleChoiceMultiQuestion,
// ...
};
const handler = questionTypeHandlers[question.type];
if (handler) {
return handler(question, languages);
}
|
packages/lib/i18n/utils.ts
Outdated
(clonedQuestion as TSurveyRatingQuestion).upperLabel = createI18nString( | ||
question.upperLabel ?? "", | ||
languages, | ||
targetLanguageCode | ||
); | ||
switch (question.type) { | ||
case "openText": | ||
if (typeof question.placeholder === "string") { | ||
(clonedQuestion as TSurveyOpenTextQuestion).placeholder = createI18nString( | ||
question.placeholder ?? "", | ||
languages | ||
); | ||
} | ||
return ZSurveyOpenTextQuestion.parse(clonedQuestion); | ||
|
||
case "multipleChoiceSingle": | ||
case "multipleChoiceMulti": | ||
(clonedQuestion as TSurveyMultipleChoiceSingleQuestion | TSurveyMultipleChoiceMultiQuestion).choices = | ||
question.choices.map((choice) => { | ||
return translateChoice(choice, languages); | ||
}); | ||
if ( | ||
typeof (clonedQuestion as TSurveyMultipleChoiceSingleQuestion | TSurveyMultipleChoiceMultiQuestion) | ||
.otherOptionPlaceholder === "string" | ||
) { | ||
( | ||
clonedQuestion as TSurveyMultipleChoiceSingleQuestion | TSurveyMultipleChoiceMultiQuestion | ||
).otherOptionPlaceholder = createI18nString(question.otherOptionPlaceholder ?? "", languages); | ||
} | ||
if (question.type === "multipleChoiceSingle") { | ||
return ZSurveyMultipleChoiceSingleQuestion.parse(clonedQuestion); | ||
} else return ZSurveyMultipleChoiceMultiQuestion.parse(clonedQuestion); | ||
|
||
case "cta": | ||
if (typeof question.dismissButtonLabel === "string") { | ||
(clonedQuestion as TSurveyCTAQuestion).dismissButtonLabel = createI18nString( | ||
question.dismissButtonLabel ?? "", | ||
languages | ||
); | ||
} | ||
if (typeof question.html === "string") { | ||
(clonedQuestion as TSurveyCTAQuestion).html = createI18nString(question.html ?? "", languages); | ||
} | ||
return ZSurveyCTAQuestion.parse(clonedQuestion); | ||
|
||
case "consent": | ||
if (typeof question.html === "string") { | ||
(clonedQuestion as TSurveyConsentQuestion).html = createI18nString(question.html ?? "", languages); | ||
} | ||
|
||
if (typeof question.label === "string") { | ||
(clonedQuestion as TSurveyConsentQuestion).label = createI18nString(question.label ?? "", languages); | ||
} | ||
return ZSurveyConsentQuestion.parse(clonedQuestion); | ||
|
||
case "nps": | ||
if (typeof question.lowerLabel === "string") { | ||
(clonedQuestion as TSurveyNPSQuestion).lowerLabel = createI18nString( | ||
question.lowerLabel ?? "", | ||
languages | ||
); | ||
} | ||
if (typeof question.upperLabel === "string") { | ||
(clonedQuestion as TSurveyNPSQuestion).upperLabel = createI18nString( | ||
question.upperLabel ?? "", | ||
languages | ||
); | ||
} | ||
return ZSurveyNPSQuestion.parse(clonedQuestion); | ||
|
||
case "rating": | ||
if (typeof question.lowerLabel === "string") { | ||
(clonedQuestion as TSurveyRatingQuestion).lowerLabel = createI18nString( | ||
question.lowerLabel ?? "", | ||
languages | ||
); | ||
} | ||
|
||
if (typeof question.upperLabel === "string") { | ||
(clonedQuestion as TSurveyRatingQuestion).upperLabel = createI18nString( | ||
question.upperLabel ?? "", | ||
languages | ||
); | ||
} | ||
return ZSurveyRatingQuestion.parse(clonedQuestion); | ||
|
||
case "fileUpload": | ||
return ZSurveyFileUploadQuestion.parse(clonedQuestion); | ||
|
||
case "pictureSelection": | ||
return ZSurveyPictureSelectionQuestion.parse(clonedQuestion); | ||
|
||
case "cal": | ||
return ZSurveyCalQuestion.parse(clonedQuestion); | ||
|
||
default: | ||
return ZSurveyQuestion.parse(clonedQuestion); | ||
} | ||
return clonedQuestion; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactor the translateQuestion function to use destructuring for improved readability and reduced redundancy.
export const translateQuestion = ( | |
question: TSurveyQuestion, | |
languages: string[], | |
targetLanguageCode?: string | |
) => { | |
question: TLegacySurveyQuestion | TSurveyQuestion, | |
languages: string[] | |
): TSurveyQuestion => { | |
// Clone the question to avoid mutating the original | |
const clonedQuestion = structuredClone(question); | |
clonedQuestion.headline = createI18nString(question.headline, languages, targetLanguageCode); | |
if (clonedQuestion.subheader) { | |
clonedQuestion.subheader = createI18nString(question.subheader ?? "", languages, targetLanguageCode); | |
//common question properties | |
if (typeof question.headline === "string") { | |
clonedQuestion.headline = createI18nString(question.headline ?? "", languages); | |
} | |
if (clonedQuestion.buttonLabel) { | |
clonedQuestion.buttonLabel = createI18nString(question.buttonLabel ?? "", languages, targetLanguageCode); | |
if (typeof question.subheader === "string") { | |
clonedQuestion.subheader = createI18nString(question.subheader ?? "", languages); | |
} | |
if (clonedQuestion.backButtonLabel) { | |
clonedQuestion.backButtonLabel = createI18nString( | |
question.backButtonLabel ?? "", | |
languages, | |
targetLanguageCode | |
); | |
if (typeof question.buttonLabel === "string") { | |
clonedQuestion.buttonLabel = createI18nString(question.buttonLabel ?? "", languages); | |
} | |
if (question.type === "multipleChoiceSingle" || question.type === "multipleChoiceMulti") { | |
(clonedQuestion as TSurveyMultipleChoiceMultiQuestion | TSurveyMultipleChoiceMultiQuestion).choices = | |
question.choices.map((choice) => ({ | |
...choice, | |
label: createI18nString(choice.label, languages, targetLanguageCode), | |
})); | |
( | |
clonedQuestion as TSurveyMultipleChoiceMultiQuestion | TSurveyMultipleChoiceMultiQuestion | |
).otherOptionPlaceholder = question.otherOptionPlaceholder | |
? createI18nString(question.otherOptionPlaceholder, languages, targetLanguageCode) | |
: undefined; | |
if (typeof question.backButtonLabel === "string") { | |
clonedQuestion.backButtonLabel = createI18nString(question.backButtonLabel ?? "", languages); | |
} | |
if (question.type === "openText") { | |
if (question.placeholder) { | |
(clonedQuestion as TSurveyOpenTextQuestion).placeholder = createI18nString( | |
question.placeholder, | |
languages, | |
targetLanguageCode | |
); | |
} | |
} | |
if (question.type === "cta") { | |
if (question.dismissButtonLabel) { | |
(clonedQuestion as TSurveyCTAQuestion).dismissButtonLabel = createI18nString( | |
question.dismissButtonLabel, | |
languages, | |
targetLanguageCode | |
); | |
} | |
if (question.html) { | |
(clonedQuestion as TSurveyCTAQuestion).html = createI18nString( | |
question.html, | |
languages, | |
targetLanguageCode | |
); | |
} | |
} | |
if (question.type === "consent") { | |
if (question.html) { | |
(clonedQuestion as TSurveyConsentQuestion).html = createI18nString( | |
question.html, | |
languages, | |
targetLanguageCode | |
); | |
} | |
if (question.label) { | |
(clonedQuestion as TSurveyConsentQuestion).label = createI18nString( | |
question.label, | |
languages, | |
targetLanguageCode | |
); | |
} | |
} | |
if (question.type === "nps") { | |
(clonedQuestion as TSurveyNPSQuestion).lowerLabel = createI18nString( | |
question.lowerLabel ?? "", | |
languages, | |
targetLanguageCode | |
); | |
(clonedQuestion as TSurveyNPSQuestion).upperLabel = createI18nString( | |
question.upperLabel ?? "", | |
languages, | |
targetLanguageCode | |
); | |
} | |
if (question.type === "rating") { | |
(clonedQuestion as TSurveyRatingQuestion).lowerLabel = createI18nString( | |
question.lowerLabel ?? "", | |
languages, | |
targetLanguageCode | |
); | |
(clonedQuestion as TSurveyRatingQuestion).upperLabel = createI18nString( | |
question.upperLabel ?? "", | |
languages, | |
targetLanguageCode | |
); | |
switch (question.type) { | |
case "openText": | |
if (typeof question.placeholder === "string") { | |
(clonedQuestion as TSurveyOpenTextQuestion).placeholder = createI18nString( | |
question.placeholder ?? "", | |
languages | |
); | |
} | |
return ZSurveyOpenTextQuestion.parse(clonedQuestion); | |
case "multipleChoiceSingle": | |
case "multipleChoiceMulti": | |
(clonedQuestion as TSurveyMultipleChoiceSingleQuestion | TSurveyMultipleChoiceMultiQuestion).choices = | |
question.choices.map((choice) => { | |
return translateChoice(choice, languages); | |
}); | |
if ( | |
typeof (clonedQuestion as TSurveyMultipleChoiceSingleQuestion | TSurveyMultipleChoiceMultiQuestion) | |
.otherOptionPlaceholder === "string" | |
) { | |
( | |
clonedQuestion as TSurveyMultipleChoiceSingleQuestion | TSurveyMultipleChoiceMultiQuestion | |
).otherOptionPlaceholder = createI18nString(question.otherOptionPlaceholder ?? "", languages); | |
} | |
if (question.type === "multipleChoiceSingle") { | |
return ZSurveyMultipleChoiceSingleQuestion.parse(clonedQuestion); | |
} else return ZSurveyMultipleChoiceMultiQuestion.parse(clonedQuestion); | |
case "cta": | |
if (typeof question.dismissButtonLabel === "string") { | |
(clonedQuestion as TSurveyCTAQuestion).dismissButtonLabel = createI18nString( | |
question.dismissButtonLabel ?? "", | |
languages | |
); | |
} | |
if (typeof question.html === "string") { | |
(clonedQuestion as TSurveyCTAQuestion).html = createI18nString(question.html ?? "", languages); | |
} | |
return ZSurveyCTAQuestion.parse(clonedQuestion); | |
case "consent": | |
if (typeof question.html === "string") { | |
(clonedQuestion as TSurveyConsentQuestion).html = createI18nString(question.html ?? "", languages); | |
} | |
if (typeof question.label === "string") { | |
(clonedQuestion as TSurveyConsentQuestion).label = createI18nString(question.label ?? "", languages); | |
} | |
return ZSurveyConsentQuestion.parse(clonedQuestion); | |
case "nps": | |
if (typeof question.lowerLabel === "string") { | |
(clonedQuestion as TSurveyNPSQuestion).lowerLabel = createI18nString( | |
question.lowerLabel ?? "", | |
languages | |
); | |
} | |
if (typeof question.upperLabel === "string") { | |
(clonedQuestion as TSurveyNPSQuestion).upperLabel = createI18nString( | |
question.upperLabel ?? "", | |
languages | |
); | |
} | |
return ZSurveyNPSQuestion.parse(clonedQuestion); | |
case "rating": | |
if (typeof question.lowerLabel === "string") { | |
(clonedQuestion as TSurveyRatingQuestion).lowerLabel = createI18nString( | |
question.lowerLabel ?? "", | |
languages | |
); | |
} | |
if (typeof question.upperLabel === "string") { | |
(clonedQuestion as TSurveyRatingQuestion).upperLabel = createI18nString( | |
question.upperLabel ?? "", | |
languages | |
); | |
} | |
return ZSurveyRatingQuestion.parse(clonedQuestion); | |
case "fileUpload": | |
return ZSurveyFileUploadQuestion.parse(clonedQuestion); | |
case "pictureSelection": | |
return ZSurveyPictureSelectionQuestion.parse(clonedQuestion); | |
case "cal": | |
return ZSurveyCalQuestion.parse(clonedQuestion); | |
default: | |
return ZSurveyQuestion.parse(clonedQuestion); | |
} | |
return clonedQuestion; | |
}; | |
export const translateQuestion = ( | |
question: TLegacySurveyQuestion | TSurveyQuestion, | |
languages: string[] | |
): TSurveyQuestion => { | |
// Clone the question to avoid mutating the original | |
const clonedQuestion = structuredClone(question); | |
//common question properties | |
if (typeof question.headline === "string") { | |
clonedQuestion.headline = createI18nString(question.headline ?? "", languages); | |
} | |
if (typeof question.subheader === "string") { | |
clonedQuestion.subheader = createI18nString(question.subheader ?? "", languages); | |
} | |
if (typeof question.buttonLabel === "string") { | |
clonedQuestion.buttonLabel = createI18nString(question.buttonLabel ?? "", languages); | |
} | |
if (typeof question.backButtonLabel === "string") { | |
clonedQuestion.backButtonLabel = createI18nString(question.backButtonLabel ?? "", languages); | |
} | |
const { type } = question; | |
switch (type) { | |
case "openText": | |
// ... | |
case "multipleChoiceSingle": | |
case "multipleChoiceMulti": | |
// ... | |
// ... | |
} | |
} |
packages/lib/i18n/utils.ts
Outdated
(clonedQuestion as TSurveyRatingQuestion).upperLabel = createI18nString( | ||
question.upperLabel ?? "", | ||
languages, | ||
targetLanguageCode | ||
); | ||
switch (question.type) { | ||
case "openText": | ||
if (typeof question.placeholder === "string") { | ||
(clonedQuestion as TSurveyOpenTextQuestion).placeholder = createI18nString( | ||
question.placeholder ?? "", | ||
languages | ||
); | ||
} | ||
return ZSurveyOpenTextQuestion.parse(clonedQuestion); | ||
|
||
case "multipleChoiceSingle": | ||
case "multipleChoiceMulti": | ||
(clonedQuestion as TSurveyMultipleChoiceSingleQuestion | TSurveyMultipleChoiceMultiQuestion).choices = | ||
question.choices.map((choice) => { | ||
return translateChoice(choice, languages); | ||
}); | ||
if ( | ||
typeof (clonedQuestion as TSurveyMultipleChoiceSingleQuestion | TSurveyMultipleChoiceMultiQuestion) | ||
.otherOptionPlaceholder === "string" | ||
) { | ||
( | ||
clonedQuestion as TSurveyMultipleChoiceSingleQuestion | TSurveyMultipleChoiceMultiQuestion | ||
).otherOptionPlaceholder = createI18nString(question.otherOptionPlaceholder ?? "", languages); | ||
} | ||
if (question.type === "multipleChoiceSingle") { | ||
return ZSurveyMultipleChoiceSingleQuestion.parse(clonedQuestion); | ||
} else return ZSurveyMultipleChoiceMultiQuestion.parse(clonedQuestion); | ||
|
||
case "cta": | ||
if (typeof question.dismissButtonLabel === "string") { | ||
(clonedQuestion as TSurveyCTAQuestion).dismissButtonLabel = createI18nString( | ||
question.dismissButtonLabel ?? "", | ||
languages | ||
); | ||
} | ||
if (typeof question.html === "string") { | ||
(clonedQuestion as TSurveyCTAQuestion).html = createI18nString(question.html ?? "", languages); | ||
} | ||
return ZSurveyCTAQuestion.parse(clonedQuestion); | ||
|
||
case "consent": | ||
if (typeof question.html === "string") { | ||
(clonedQuestion as TSurveyConsentQuestion).html = createI18nString(question.html ?? "", languages); | ||
} | ||
|
||
if (typeof question.label === "string") { | ||
(clonedQuestion as TSurveyConsentQuestion).label = createI18nString(question.label ?? "", languages); | ||
} | ||
return ZSurveyConsentQuestion.parse(clonedQuestion); | ||
|
||
case "nps": | ||
if (typeof question.lowerLabel === "string") { | ||
(clonedQuestion as TSurveyNPSQuestion).lowerLabel = createI18nString( | ||
question.lowerLabel ?? "", | ||
languages | ||
); | ||
} | ||
if (typeof question.upperLabel === "string") { | ||
(clonedQuestion as TSurveyNPSQuestion).upperLabel = createI18nString( | ||
question.upperLabel ?? "", | ||
languages | ||
); | ||
} | ||
return ZSurveyNPSQuestion.parse(clonedQuestion); | ||
|
||
case "rating": | ||
if (typeof question.lowerLabel === "string") { | ||
(clonedQuestion as TSurveyRatingQuestion).lowerLabel = createI18nString( | ||
question.lowerLabel ?? "", | ||
languages | ||
); | ||
} | ||
|
||
if (typeof question.upperLabel === "string") { | ||
(clonedQuestion as TSurveyRatingQuestion).upperLabel = createI18nString( | ||
question.upperLabel ?? "", | ||
languages | ||
); | ||
} | ||
return ZSurveyRatingQuestion.parse(clonedQuestion); | ||
|
||
case "fileUpload": | ||
return ZSurveyFileUploadQuestion.parse(clonedQuestion); | ||
|
||
case "pictureSelection": | ||
return ZSurveyPictureSelectionQuestion.parse(clonedQuestion); | ||
|
||
case "cal": | ||
return ZSurveyCalQuestion.parse(clonedQuestion); | ||
|
||
default: | ||
return ZSurveyQuestion.parse(clonedQuestion); | ||
} | ||
return clonedQuestion; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactor the translateQuestion function to use a lookup object instead of a switch-case statement. This will improve performance as lookup in an object is generally faster than multiple conditional checks.
export const translateQuestion = ( | |
question: TSurveyQuestion, | |
languages: string[], | |
targetLanguageCode?: string | |
) => { | |
question: TLegacySurveyQuestion | TSurveyQuestion, | |
languages: string[] | |
): TSurveyQuestion => { | |
// Clone the question to avoid mutating the original | |
const clonedQuestion = structuredClone(question); | |
clonedQuestion.headline = createI18nString(question.headline, languages, targetLanguageCode); | |
if (clonedQuestion.subheader) { | |
clonedQuestion.subheader = createI18nString(question.subheader ?? "", languages, targetLanguageCode); | |
//common question properties | |
if (typeof question.headline === "string") { | |
clonedQuestion.headline = createI18nString(question.headline ?? "", languages); | |
} | |
if (clonedQuestion.buttonLabel) { | |
clonedQuestion.buttonLabel = createI18nString(question.buttonLabel ?? "", languages, targetLanguageCode); | |
if (typeof question.subheader === "string") { | |
clonedQuestion.subheader = createI18nString(question.subheader ?? "", languages); | |
} | |
if (clonedQuestion.backButtonLabel) { | |
clonedQuestion.backButtonLabel = createI18nString( | |
question.backButtonLabel ?? "", | |
languages, | |
targetLanguageCode | |
); | |
if (typeof question.buttonLabel === "string") { | |
clonedQuestion.buttonLabel = createI18nString(question.buttonLabel ?? "", languages); | |
} | |
if (question.type === "multipleChoiceSingle" || question.type === "multipleChoiceMulti") { | |
(clonedQuestion as TSurveyMultipleChoiceMultiQuestion | TSurveyMultipleChoiceMultiQuestion).choices = | |
question.choices.map((choice) => ({ | |
...choice, | |
label: createI18nString(choice.label, languages, targetLanguageCode), | |
})); | |
( | |
clonedQuestion as TSurveyMultipleChoiceMultiQuestion | TSurveyMultipleChoiceMultiQuestion | |
).otherOptionPlaceholder = question.otherOptionPlaceholder | |
? createI18nString(question.otherOptionPlaceholder, languages, targetLanguageCode) | |
: undefined; | |
if (typeof question.backButtonLabel === "string") { | |
clonedQuestion.backButtonLabel = createI18nString(question.backButtonLabel ?? "", languages); | |
} | |
if (question.type === "openText") { | |
if (question.placeholder) { | |
(clonedQuestion as TSurveyOpenTextQuestion).placeholder = createI18nString( | |
question.placeholder, | |
languages, | |
targetLanguageCode | |
); | |
} | |
} | |
if (question.type === "cta") { | |
if (question.dismissButtonLabel) { | |
(clonedQuestion as TSurveyCTAQuestion).dismissButtonLabel = createI18nString( | |
question.dismissButtonLabel, | |
languages, | |
targetLanguageCode | |
); | |
} | |
if (question.html) { | |
(clonedQuestion as TSurveyCTAQuestion).html = createI18nString( | |
question.html, | |
languages, | |
targetLanguageCode | |
); | |
} | |
} | |
if (question.type === "consent") { | |
if (question.html) { | |
(clonedQuestion as TSurveyConsentQuestion).html = createI18nString( | |
question.html, | |
languages, | |
targetLanguageCode | |
); | |
} | |
if (question.label) { | |
(clonedQuestion as TSurveyConsentQuestion).label = createI18nString( | |
question.label, | |
languages, | |
targetLanguageCode | |
); | |
} | |
} | |
if (question.type === "nps") { | |
(clonedQuestion as TSurveyNPSQuestion).lowerLabel = createI18nString( | |
question.lowerLabel ?? "", | |
languages, | |
targetLanguageCode | |
); | |
(clonedQuestion as TSurveyNPSQuestion).upperLabel = createI18nString( | |
question.upperLabel ?? "", | |
languages, | |
targetLanguageCode | |
); | |
} | |
if (question.type === "rating") { | |
(clonedQuestion as TSurveyRatingQuestion).lowerLabel = createI18nString( | |
question.lowerLabel ?? "", | |
languages, | |
targetLanguageCode | |
); | |
(clonedQuestion as TSurveyRatingQuestion).upperLabel = createI18nString( | |
question.upperLabel ?? "", | |
languages, | |
targetLanguageCode | |
); | |
switch (question.type) { | |
case "openText": | |
if (typeof question.placeholder === "string") { | |
(clonedQuestion as TSurveyOpenTextQuestion).placeholder = createI18nString( | |
question.placeholder ?? "", | |
languages | |
); | |
} | |
return ZSurveyOpenTextQuestion.parse(clonedQuestion); | |
case "multipleChoiceSingle": | |
case "multipleChoiceMulti": | |
(clonedQuestion as TSurveyMultipleChoiceSingleQuestion | TSurveyMultipleChoiceMultiQuestion).choices = | |
question.choices.map((choice) => { | |
return translateChoice(choice, languages); | |
}); | |
if ( | |
typeof (clonedQuestion as TSurveyMultipleChoiceSingleQuestion | TSurveyMultipleChoiceMultiQuestion) | |
.otherOptionPlaceholder === "string" | |
) { | |
( | |
clonedQuestion as TSurveyMultipleChoiceSingleQuestion | TSurveyMultipleChoiceMultiQuestion | |
).otherOptionPlaceholder = createI18nString(question.otherOptionPlaceholder ?? "", languages); | |
} | |
if (question.type === "multipleChoiceSingle") { | |
return ZSurveyMultipleChoiceSingleQuestion.parse(clonedQuestion); | |
} else return ZSurveyMultipleChoiceMultiQuestion.parse(clonedQuestion); | |
case "cta": | |
if (typeof question.dismissButtonLabel === "string") { | |
(clonedQuestion as TSurveyCTAQuestion).dismissButtonLabel = createI18nString( | |
question.dismissButtonLabel ?? "", | |
languages | |
); | |
} | |
if (typeof question.html === "string") { | |
(clonedQuestion as TSurveyCTAQuestion).html = createI18nString(question.html ?? "", languages); | |
} | |
return ZSurveyCTAQuestion.parse(clonedQuestion); | |
case "consent": | |
if (typeof question.html === "string") { | |
(clonedQuestion as TSurveyConsentQuestion).html = createI18nString(question.html ?? "", languages); | |
} | |
if (typeof question.label === "string") { | |
(clonedQuestion as TSurveyConsentQuestion).label = createI18nString(question.label ?? "", languages); | |
} | |
return ZSurveyConsentQuestion.parse(clonedQuestion); | |
case "nps": | |
if (typeof question.lowerLabel === "string") { | |
(clonedQuestion as TSurveyNPSQuestion).lowerLabel = createI18nString( | |
question.lowerLabel ?? "", | |
languages | |
); | |
} | |
if (typeof question.upperLabel === "string") { | |
(clonedQuestion as TSurveyNPSQuestion).upperLabel = createI18nString( | |
question.upperLabel ?? "", | |
languages | |
); | |
} | |
return ZSurveyNPSQuestion.parse(clonedQuestion); | |
case "rating": | |
if (typeof question.lowerLabel === "string") { | |
(clonedQuestion as TSurveyRatingQuestion).lowerLabel = createI18nString( | |
question.lowerLabel ?? "", | |
languages | |
); | |
} | |
if (typeof question.upperLabel === "string") { | |
(clonedQuestion as TSurveyRatingQuestion).upperLabel = createI18nString( | |
question.upperLabel ?? "", | |
languages | |
); | |
} | |
return ZSurveyRatingQuestion.parse(clonedQuestion); | |
case "fileUpload": | |
return ZSurveyFileUploadQuestion.parse(clonedQuestion); | |
case "pictureSelection": | |
return ZSurveyPictureSelectionQuestion.parse(clonedQuestion); | |
case "cal": | |
return ZSurveyCalQuestion.parse(clonedQuestion); | |
default: | |
return ZSurveyQuestion.parse(clonedQuestion); | |
} | |
return clonedQuestion; | |
}; | |
export const translateQuestion = ( | |
question: TLegacySurveyQuestion | TSurveyQuestion, | |
languages: string[] | |
): TSurveyQuestion => { | |
// Clone the question to avoid mutating the original | |
const clonedQuestion = structuredClone(question); | |
//common question properties | |
if (typeof question.headline === "string") { | |
clonedQuestion.headline = createI18nString(question.headline ?? "", languages); | |
} | |
if (typeof question.subheader === "string") { | |
clonedQuestion.subheader = createI18nString(question.subheader ?? "", languages); | |
} | |
if (typeof question.buttonLabel === "string") { | |
clonedQuestion.buttonLabel = createI18nString(question.buttonLabel ?? "", languages); | |
} | |
if (typeof question.backButtonLabel === "string") { | |
clonedQuestion.backButtonLabel = createI18nString(question.backButtonLabel ?? "", languages); | |
} | |
const questionTypeHandlers = { | |
"openText": handleOpenTextQuestion, | |
"multipleChoiceSingle": handleMultipleChoiceSingleQuestion, | |
"multipleChoiceMulti": handleMultipleChoiceMultiQuestion, | |
// ... | |
}; | |
const handler = questionTypeHandlers[question.type]; | |
if (handler) { | |
return handler(question, languages); | |
} | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Dhruwang thanks a lot 😊 It's now working for all my tests 😊💪
What does this PR do?
Improved translate survey function and added migration script 2
How should this be tested?
pnpm run data-migration2:mls
Checklist
Required
pnpm build
console.logs
git pull origin main
Appreciated