Skip to content
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

Merged
merged 12 commits into from
Mar 20, 2024
Merged

fix: translate survey and migration script #2290

merged 12 commits into from
Mar 20, 2024

Conversation

Dhruwang
Copy link
Contributor

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

  • Filled out the "How to test" section in this PR
  • Read How we Code at Formbricks
  • Self-reviewed my own code
  • Commented on my code in hard-to-understand bits
  • Ran pnpm build
  • Checked for warnings, there are none
  • Removed all console.logs
  • Merged the latest changes from main onto my branch with git pull origin main
  • My changes don't cause any responsiveness issues

Appreciated

  • If a UI change was made: Added a screen recording or screenshots to this PR
  • Updated the Formbricks Docs if changes were necessary

Copy link

vercel bot commented Mar 20, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Ignored Deployments
Name Status Preview Comments Updated (UTC)
formbricks-cloud ⬜️ Ignored (Inspect) Visit Preview Mar 20, 2024 4:58pm
formbricks-com ⬜️ Ignored (Inspect) Visit Preview Mar 20, 2024 4:58pm

Copy link
Contributor

github-actions bot commented Mar 20, 2024

Thank you for following the naming conventions for pull request titles! 🙏

Copy link
Contributor

packages/lib/i18n/utils.ts

Consider 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.
Create Issue
See the diff
Checkout the fix

    // 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":
        // ...
      // ...
    }
git fetch origin && git checkout -b ReviewBot/The-c-tql1m1l origin/ReviewBot/The-c-tql1m1l

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.
Create Issue
See the diff
Checkout the fix

    // 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);
    }
git fetch origin && git checkout -b ReviewBot/The-c-rwzas5k origin/ReviewBot/The-c-rwzas5k

Comment on lines 177 to 294
(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;
};
Copy link
Contributor

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.

Suggested change
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":
// ...
// ...
}
}

Comment on lines 177 to 294
(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;
};
Copy link
Contributor

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.

Suggested change
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);
}
}

Copy link
Member

@mattinannt mattinannt left a 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 😊💪

@mattinannt mattinannt added this pull request to the merge queue Mar 20, 2024
Merged via the queue into main with commit 7b2cf9f Mar 20, 2024
12 of 14 checks passed
@mattinannt mattinannt deleted the mls-fixes branch March 20, 2024 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants