Skip to content

Commit b1644ee

Browse files
committed
setup learn quizzes for translation
1 parent b389fc6 commit b1644ee

File tree

8 files changed

+666
-450
lines changed

8 files changed

+666
-450
lines changed

src/components/Quiz/QuizRadioGroup.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ import {
1010
useRadio,
1111
useRadioGroup,
1212
} from "@chakra-ui/react"
13+
import { useIntl } from "react-intl"
14+
15+
// Components
16+
import Translation from "../Translation"
1317

1418
// Import types
1519
import { Question } from "../../types"
1620

21+
// Utils
22+
import { translateMessageId, TranslationKey } from "../../utils/translations"
23+
1724
// Interfaces
1825
export interface CustomRadioProps extends RadioProps {
1926
index: number
@@ -33,13 +40,14 @@ const QuizRadioGroup: React.FC<IProps> = ({
3340
handleSelection,
3441
selectedAnswer,
3542
}) => {
43+
const intl = useIntl()
3644
const { getRadioProps, getRootProps } = useRadioGroup({
3745
onChange: handleSelection,
3846
})
3947
const { prompt, answers, correctAnswerId } = questionData
4048

4149
// Memoized values
42-
const explanation = useMemo<string>(() => {
50+
const explanation = useMemo<TranslationKey>(() => {
4351
if (!selectedAnswer) return ""
4452
return answers.filter(({ id }) => id === selectedAnswer)[0].explanation
4553
}, [selectedAnswer])
@@ -121,7 +129,7 @@ const QuizRadioGroup: React.FC<IProps> = ({
121129
return (
122130
<Flex {...getRootProps()} direction="column" w="100%">
123131
<Text fontWeight="700" fontSize="2xl" mb={6}>
124-
{prompt}
132+
{translateMessageId(prompt, intl)}
125133
</Text>
126134
<Flex direction="column" gap={4}>
127135
{answers.map(({ id, label }, index) => {
@@ -132,7 +140,7 @@ const QuizRadioGroup: React.FC<IProps> = ({
132140
key={id}
133141
display={display}
134142
index={index}
135-
label={label}
143+
label={translateMessageId(label, intl)}
136144
{...getRadioProps({ value: id })}
137145
/>
138146
)
@@ -141,9 +149,9 @@ const QuizRadioGroup: React.FC<IProps> = ({
141149
{showAnswer && (
142150
<Box mt={5}>
143151
<Text fontWeight="bold" mt={0} mb={2}>
144-
Explanation
152+
<Translation id="explanation" />
145153
</Text>
146-
<Text m={0}>{explanation}</Text>
154+
<Text m={0}>{translateMessageId(explanation, intl)}</Text>
147155
</Box>
148156
)}
149157
</Flex>

src/components/Quiz/QuizSummary.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// Import libraries
2-
import React, { useMemo } from "react"
2+
import React from "react"
33
import { Box, Flex, Text, useMediaQuery } from "@chakra-ui/react"
44
import { useIntl } from "react-intl"
55

6+
// Components
7+
import Translation from "../Translation"
8+
69
// Import utilities
710
import { numberToPercent } from "../../utils/numberToPercent"
811

@@ -55,16 +58,22 @@ const QuizSummary: React.FC<IProps> = ({
5558
>
5659
<Flex>
5760
<Text {...valueStyles}>{numberToPercent(ratioCorrect, locale)}</Text>
58-
<Text {...labelStyles}>Score</Text>
61+
<Text {...labelStyles}>
62+
<Translation id="score" />
63+
</Text>
5964
</Flex>
6065
<Flex>
6166
<Text {...valueStyles}>+{correctCount}</Text>
62-
<Text {...labelStyles}>Correct</Text>
67+
<Text {...labelStyles}>
68+
<Translation id="correct" />
69+
</Text>
6370
</Flex>
6471
{largerThanMobile && (
6572
<Flex>
6673
<Text {...valueStyles}>{questionCount}</Text>
67-
<Text {...labelStyles}>Total</Text>
74+
<Text {...labelStyles}>
75+
<Translation id="total" />
76+
</Text>
6877
</Flex>
6978
)}
7079
</Flex>

src/components/Quiz/QuizWidget.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ import {
1313
} from "@chakra-ui/react"
1414
import { shuffle } from "lodash"
1515
import { FaTwitter } from "react-icons/fa"
16+
import { useIntl } from "react-intl"
1617

1718
// Import components
1819
import Button from "../Button"
1920
import QuizRadioGroup from "./QuizRadioGroup"
2021
import QuizSummary from "./QuizSummary"
22+
import Translation from "../Translation"
2123

2224
// Import SVGs
2325
import Trophy from "../../assets/quiz/trophy.svg"
@@ -31,6 +33,7 @@ import questionBank from "../../data/quizzes/questionBank"
3133

3234
// Import utilities
3335
import { trackCustomEvent } from "../../utils/matomo"
36+
import { translateMessageId } from "../../utils/translations"
3437

3538
// Import types
3639
import { AnswerChoice, RawQuiz, Quiz, RawQuestion, Question } from "../../types"
@@ -49,6 +52,7 @@ export interface IProps {
4952

5053
// Component
5154
const QuizWidget: React.FC<IProps> = ({ quizKey, maxQuestions }) => {
55+
const intl = useIntl()
5256
const [quizData, setQuizData] = useState<Quiz | null>(null)
5357
const [userQuizProgress, setUserQuizProgress] = useState<Array<AnswerChoice>>(
5458
[]
@@ -86,7 +90,10 @@ const QuizWidget: React.FC<IProps> = ({ quizKey, maxQuestions }) => {
8690
const trimmedQuestions = maxQuestions
8791
? shuffledQuestions.slice(0, maxQuestions)
8892
: shuffledQuestions
89-
const quiz: Quiz = { title: rawQuiz.title, questions: trimmedQuestions }
93+
const quiz: Quiz = {
94+
title: translateMessageId(rawQuiz.title, intl),
95+
questions: trimmedQuestions,
96+
}
9097
setQuizData(quiz)
9198
}
9299
useEffect(initialize, [quizKey])
@@ -233,7 +240,7 @@ const QuizWidget: React.FC<IProps> = ({ quizKey, maxQuestions }) => {
233240
scrollMarginTop={24}
234241
id="quiz"
235242
>
236-
Test your knowledge
243+
<Translation id="test-your-knowledge" />
237244
</Heading>
238245
<Box
239246
w="full"
@@ -361,7 +368,7 @@ const QuizWidget: React.FC<IProps> = ({ quizKey, maxQuestions }) => {
361368
onClick={handleRetryQuestion}
362369
variant="outline-color"
363370
>
364-
Try again
371+
<Translation id="try-again" />
365372
</Button>
366373
)}
367374
{showResults ? (
@@ -370,17 +377,19 @@ const QuizWidget: React.FC<IProps> = ({ quizKey, maxQuestions }) => {
370377
leftIcon={<Icon as={FaTwitter} />}
371378
onClick={handleShare}
372379
>
373-
Share results
380+
<Translation id="share-results" />
374381
</Button>
375382
{score < 100 && (
376-
<Button onClick={initialize}>Try again</Button>
383+
<Button onClick={initialize}>
384+
<Translation id="try-again" />
385+
</Button>
377386
)}
378387
</>
379388
) : showAnswer ? (
380389
<Button onClick={handleContinue}>
381390
{userQuizProgress.length === quizData.questions.length - 1
382-
? "See results"
383-
: "Next question"}
391+
? translateMessageId("see-results", intl)
392+
: translateMessageId("next-question", intl)}
384393
</Button>
385394
) : (
386395
<Button
@@ -392,7 +401,7 @@ const QuizWidget: React.FC<IProps> = ({ quizKey, maxQuestions }) => {
392401
}
393402
disabled={!currentQuestionAnswerChoice}
394403
>
395-
Submit answer
404+
<Translation id="submit-answer" />
396405
</Button>
397406
)}
398407
</Flex>

src/data/quizzes/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,35 @@ import { RawQuizzes } from "../../types"
44
// Declare hash-map of quizzes based on slug key
55
const quizzes: RawQuizzes = {
66
"what-is-ethereum": {
7-
title: "What is Ethereum?",
7+
title: "what-is-ethereum",
88
questions: ["a001", "a002", "a003", "a004", "a005"],
99
},
1010
"what-is-ether": {
11-
title: "What is ether?",
11+
title: "page-what-is-ethereum-what-is-ether",
1212
questions: ["b001", "b002", "b003", "b004"],
1313
},
1414
web3: {
15-
title: "Web3",
15+
title: "web3-title",
1616
questions: ["c001", "c002", "c003", "c004", "c005"],
1717
},
1818
wallets: {
19-
title: "Wallets",
19+
title: "wallets",
2020
questions: ["d001", "d002", "d003", "d004"],
2121
},
2222
security: {
23-
title: "Security",
23+
title: "security",
2424
questions: ["e001", "e002", "e003", "e004", "d003"],
2525
},
2626
nfts: {
27-
title: "NFTs",
27+
title: "nfts",
2828
questions: ["f001", "f002", "f003", "f004", "f005"],
2929
},
3030
"layer-2": {
31-
title: "Layer 2s",
31+
title: "layer-2",
3232
questions: ["g001", "g002", "g003", "g004"],
3333
},
3434
merge: {
35-
title: "The Merge",
35+
title: "page-assets-merge",
3636
questions: ["h001", "h002", "h003", "h004", "h005"],
3737
},
3838
}

0 commit comments

Comments
 (0)