Skip to content

Commit

Permalink
Translation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Shreya Bhandari committed Sep 4, 2024
1 parent 591ecf5 commit 2193fe8
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 40 deletions.
9 changes: 6 additions & 3 deletions src/components/problem-layout/HintTextbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import ProblemInput from "../problem-input/ProblemInput";
import { stagingProp } from "../../util/addStagingProperty";
import { toastNotifyCorrectness } from "./ToastNotifyCorrectness";
import { joinList } from "../../util/formListString";
import withTranslation from "../../util/withTranslation.js"

class HintTextbox extends React.Component {
static contextType = ThemeContext;

constructor(props, context) {
super(props);
this.translate = props.translate
this.hint = props.hint;
this.index = props.index;
this.giveStuFeedback = props.giveStuFeedback
Expand Down Expand Up @@ -49,7 +51,7 @@ class HintTextbox extends React.Component {

const isCorrect = !!correctAnswer

toastNotifyCorrectness(isCorrect, reason);
toastNotifyCorrectness(isCorrect, reason, this.translate);

this.setState({
isCorrect,
Expand All @@ -67,6 +69,7 @@ class HintTextbox extends React.Component {
}

render() {
const { translate } = this.props;
const { classes, index, hintNum } = this.props;
const { isCorrect } = this.state;
const { debug, use_expanded_view } = this.context;
Expand Down Expand Up @@ -123,7 +126,7 @@ class HintTextbox extends React.Component {
"data-selenium-target": `submit-button-${hintIndex}`
})}
>
Submit
{translate('problem.Submit')}
</Button>
</center>
</Grid>
Expand Down Expand Up @@ -172,4 +175,4 @@ class HintTextbox extends React.Component {
}
}

export default withStyles(styles)(HintTextbox);
export default withStyles(styles)(withTranslation(HintTextbox));
13 changes: 7 additions & 6 deletions src/components/problem-layout/ProblemCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ProblemCard extends React.Component {
constructor(props, context) {
super(props);
// console.log("problem lesson props:", props);
this.translate = props.translate
this.step = props.step;
this.index = props.index;
this.giveStuFeedback = props.giveStuFeedback;
Expand Down Expand Up @@ -96,8 +97,8 @@ class ProblemCard extends React.Component {
// Bottom out hints
this.hints.push({
id: this.step.id + "-h" + (this.hints.length + 1),
title: "Answer",
text: "The answer is " + this.step.stepAnswer,
title: this.translate('hintsystem.answer'),
text: this.translate('hintsystem.answerIs') + this.step.stepAnswer,
type: "bottomOut",
dependencies: Array.from(Array(this.hints.length).keys()),
});
Expand All @@ -114,8 +115,8 @@ class ProblemCard extends React.Component {
i +
"-s" +
(hint.subHints.length + 1),
title: "Answer",
text: "The answer is " + hint.hintAnswer[0],
title: this.translate('hintsystem.answer'),
text: this.translate('hintsystem.answerIs') + hint.hintAnswer[0],
type: "bottomOut",
dependencies: Array.from(
Array(hint.subHints.length).keys()
Expand Down Expand Up @@ -248,9 +249,9 @@ class ProblemCard extends React.Component {
);

if (this.showCorrectness) {
toastNotifyCorrectness(isCorrect, reason);
toastNotifyCorrectness(isCorrect, reason, this.translate);
} else {
toastNotifyCompletion();
toastNotifyCompletion(this.translate);
}

this.setState({
Expand Down
6 changes: 4 additions & 2 deletions src/components/problem-layout/SubHintSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import HintTextbox from './HintTextbox.js';
import { renderText, chooseVariables } from '../../platform-logic/renderText.js';
import Spacer from "../Spacer";
import { ThemeContext } from "../../config/config";
import withTranslation from "../../util/withTranslation.js"


class SubHintSystem extends React.Component {
Expand Down Expand Up @@ -50,6 +51,7 @@ class SubHintSystem extends React.Component {
}

render() {
const { translate } = this.props;
const { classes, index, parent, hintVars, problemID, seed } = this.props;
const { currentExpanded } = this.state;
const { debug, use_expanded_view } = this.context;
Expand All @@ -68,7 +70,7 @@ class SubHintSystem extends React.Component {
id="panel1a-header"
>
<Typography className={classes.heading}>
Hint {i + 1}: {renderText(hint.title, problemID,
{translate('hintsystem.hint') + (i + 1) + ": "} {renderText(hint.title, problemID,
chooseVariables(Object.assign({}, hintVars, hint.variabilization), seed), this.context)} </Typography>
</AccordionSummary>
<AccordionDetails>
Expand Down Expand Up @@ -106,4 +108,4 @@ const styles = theme => ({
});


export default withStyles(styles)(SubHintSystem);
export default withStyles(styles)(withTranslation(SubHintSystem));
14 changes: 7 additions & 7 deletions src/components/problem-layout/ToastNotifyCorrectness.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import { toast } from "react-toastify";
import WrongAnswerReasons from "../../util/wrongAnswerReasons";

export function toastNotifyCorrectness(isCorrect, reason) {
export function toastNotifyCorrectness(isCorrect, reason, translate) {
if (isCorrect) {
toast.success("Correct Answer!", {
toast.success(translate("toast.correct"), {
autoClose: 3000
})
} else {
if (reason === WrongAnswerReasons.wrong) {
toast.error("Incorrect Answer!", {
toast.error(translate("toast.incorrect"), {
autoClose: 3000
})
} else if (reason === WrongAnswerReasons.sameAsProblem) {
toast.error("Please simplify!", {
toast.error(translate("toast.simplify"), {
autoClose: 3000
})
} else if (reason === WrongAnswerReasons.errored) {
toast.error("Our system could not process that answer", {
toast.error(translate("toast.cantProcess"), {
autoClose: 3000
})
}
}
}

export function toastNotifyCompletion() {
toast.info("Step Completed!", {
export function toastNotifyCompletion(translate) {
toast.info(translate("toast.stepComplete"), {
autoClose: 3000
})
}
12 changes: 11 additions & 1 deletion src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
},
"hintsystem":
{
"hint": "Hint "
"hint": "Hint ",
"answer": "Answer",
"answerIs": "The answer is "
},
"toast":
{
"correct": "Correct Answer!",
"incorrect": "Incorrect Answer!",
"simplify": "Please simplify!",
"cantProcess": "Our system could not process that answer",
"stepComplete": "Step Completed!"
}
}
12 changes: 11 additions & 1 deletion src/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
},
"hintsystem":
{
"hint": "Pista "
"hint": "Pista ",
"answer": "Respuesta",
"answerIs": "La respuesta es "
},
"toast":
{
"correct": "¡Respuesta correcta!",
"incorrect": "¡Respuesta incorrecta!",
"simplify": "¡Por favor, simplifica!",
"cantProcess": "Nuestro sistema no pudo procesar esa respuesta",
"stepComplete": "¡Paso completado!"
}
}
28 changes: 19 additions & 9 deletions src/locales/se.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
{
"lessonSelection":
{
"welcomeInstructor": "Välkommen Instruktör!",
"welcomeInstructor": "Välkommen, lärare!",
"welcomeTo": "Välkommen till",
"resetprogress": "Återställ Framsteg",
"resetprogress": "Nollställ framsteg",
"select": "Vänligen välj en",
"course": "kurs",
"lessonplan": "lektionsplan",
"onlyselect": "Välj"
},
"problem":
{
"NextProblem": "Nästa Problem",
"PreviousProblem": "Föregående Problem",
"NextProblem": "Nästa uppgift",
"PreviousProblem": "Föregående uppgift",
"Submit": "Skicka in",
"Feedback": "Återkoppling",
"Response": "Svar",
"Thanks": "Tack för din återkoppling!",
"Description": "Känn dig fri att skicka återkoppling om detta problem om du stöter på några fel. Skicka återkoppling för alla delar av problemet på en gång.",
"Description": "Du får gärna skicka in feedback om du stöter på några buggar i denna uppgift. Skicka gärna all feedback om uppgiftens olika delar på en gång.",
"Derivative": "är en härledning av",
"Used": ", används under"
},
"platform":
{
"Mastery": "Behärska Lektionen: ",
"LoggedIn": "Inte inloggad"
"Mastery": "Bemästrande av lektionen: ",
"LoggedIn": "Utloggad"
},
"hintsystem":
{
"hint": "Ledtråd "
}
"hint": "Ledtråd ",
"answer": "Svar",
"answerIs": "Svaret är "
},
"toast":
{
"correct": "Rätt svar!",
"incorrect": "Fel svar!",
"simplify": "Vänligen förenkla!",
"cantProcess": "Vårt system kunde inte bearbeta det svaret",
"stepComplete": "Steg slutfört!"
}
}
21 changes: 10 additions & 11 deletions src/util/useTranslation.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { useState, useEffect } from "react";
import { useLocalization } from "./LocalizationContext";

import translationsEn from "../locales/en.json";
import translationsEs from "../locales/es.json";
import translationsSe from "../locales/se.json";

export const useTranslation = () => {
const { language } = useLocalization();
const [translations, setTranslations] = useState({});

useEffect(() => {
const loadTranslations = async (lang) => {
const module = await import(`../locales/${lang}.json`);
setTranslations(module.default || module);
};
const translationsMap = {
en: translationsEn,
es: translationsEs,
se: translationsSe,
};

if (["en", "es", "se"].includes(language)) {
loadTranslations(language);
}
}, [language]);
const translations = translationsMap[language] || translationsMap['en'];

const translate = (key) => {
return key.split(".").reduce((obj, k) => (obj || {})[k], translations);
Expand Down

0 comments on commit 2193fe8

Please sign in to comment.