Skip to content

Commit 26de8f4

Browse files
authored
Bugfix: error messages aren't displayed as translated strings (#2160)
## Summary: Proposing a bug fix for error messages not showing up as translated: https://khanacademy.slack.com/archives/C06FULVQLSV/p1738014199191129?thread_ts=1738012970.693379&cid=C06FULVQLSV **What I thought we were doing:** Originally I thought `strings` were getting replaced during the translation process (I don't know why I thought that). So we were mapping error codes to error strings directly off of `strings`. **What we were actually doing:** `strings` isn't actually changed, it's used to make a new object that has all of the translated strings in it. So we were pulling the English strings off of the base object that was used to create a separate translated object. **What this PR does:** instead of pulling from `strings`, we instead pass the translated strings into `mapErrorToString`. Then we map an error code to a key that we use to access the translated message from the passed in object. ## Test plan: - Go into a non-English language (like Portuguese) - Go to a Radio widget that requires multiple selections - Select one right answer, but not all right answers - Submit question - There should be an error message displayed - broken: that message will be English - patched: that message will be Portuguese Author: handeyeco Reviewers: jeresig, catandthemachines Required Reviewers: Approved By: jeresig, catandthemachines Checks: ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x) Pull Request URL: #2160
1 parent 91a9a2a commit 26de8f4

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

.changeset/wise-insects-occur.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@khanacademy/perseus": patch
3+
---
4+
5+
Bugfix: use translated strings in mapErrorToString

packages/perseus/src/strings.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {clone} from "../../../testing/object-utils";
2+
3+
import {mapErrorToString, mockStrings} from "./strings";
4+
5+
describe("mapErrorToString", () => {
6+
it("handles translated strings", () => {
7+
// Assemble
8+
const translated = clone(mockStrings);
9+
translated.MISSING_PERCENT_ERROR =
10+
"pretend this is a different language";
11+
12+
// Act
13+
const rv = mapErrorToString("MISSING_PERCENT_ERROR", translated);
14+
15+
// Assert
16+
expect(rv).toBe("pretend this is a different language");
17+
});
18+
});

packages/perseus/src/strings.ts

+24-14
Original file line numberDiff line numberDiff line change
@@ -976,26 +976,36 @@ export const mockStrings: PerseusStrings = {
976976

977977
// This type helps us make sure all error codes are mapped to strings
978978
type ErrorStringMap = {
979-
[K in keyof typeof ErrorCodes]: string;
979+
[K in keyof typeof ErrorCodes]: keyof PerseusStrings;
980980
};
981981

982+
/**
983+
* Map an error string to a PerseusStrings key
984+
* that we can use to get the translated error message
985+
*/
982986
const errorToString: ErrorStringMap = {
983-
MISSING_PERCENT_ERROR: strings.MISSING_PERCENT_ERROR,
984-
NEEDS_TO_BE_SIMPLIFIED_ERROR: strings.NEEDS_TO_BE_SIMPLFIED_ERROR,
985-
APPROXIMATED_PI_ERROR: strings.APPROXIMATED_PI_ERROR,
986-
EXTRA_SYMBOLS_ERROR: strings.EXTRA_SYMBOLS_ERROR,
987-
WRONG_CASE_ERROR: strings.WRONG_CASE_ERROR,
988-
WRONG_LETTER_ERROR: strings.WRONG_LETTER_ERROR,
989-
MULTIPLICATION_SIGN_ERROR: strings.MULTIPLICATION_SIGN_ERROR,
990-
INVALID_SELECTION_ERROR: strings.invalidSelection,
991-
CHOOSE_CORRECT_NUM_ERROR: strings.chooseCorrectNum,
992-
NOT_NONE_ABOVE_ERROR: strings.notNoneOfTheAbove,
993-
FILL_ALL_CELLS_ERROR: strings.fillAllCells,
987+
MISSING_PERCENT_ERROR: "MISSING_PERCENT_ERROR",
988+
NEEDS_TO_BE_SIMPLIFIED_ERROR: "NEEDS_TO_BE_SIMPLFIED_ERROR",
989+
APPROXIMATED_PI_ERROR: "APPROXIMATED_PI_ERROR",
990+
EXTRA_SYMBOLS_ERROR: "EXTRA_SYMBOLS_ERROR",
991+
WRONG_CASE_ERROR: "WRONG_CASE_ERROR",
992+
WRONG_LETTER_ERROR: "WRONG_LETTER_ERROR",
993+
MULTIPLICATION_SIGN_ERROR: "MULTIPLICATION_SIGN_ERROR",
994+
INVALID_SELECTION_ERROR: "invalidSelection",
995+
CHOOSE_CORRECT_NUM_ERROR: "chooseCorrectNum",
996+
NOT_NONE_ABOVE_ERROR: "notNoneOfTheAbove",
997+
FILL_ALL_CELLS_ERROR: "fillAllCells",
994998
};
995999

996-
export function mapErrorToString(err: string | null | undefined) {
1000+
export function mapErrorToString(
1001+
// the string representing an error code
1002+
err: string | null | undefined,
1003+
// the translated Perseus strings
1004+
translatedStrings: PerseusStrings,
1005+
) {
9971006
if (!err) {
9981007
return err;
9991008
}
1000-
return errorToString[err] || err;
1009+
1010+
return translatedStrings[errorToString[err]] || err;
10011011
}

packages/perseus/src/widgets/graded-group/graded-group.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export class GradedGroup
192192
score.type === "points"
193193
? score.message || ""
194194
: score.message
195-
? `${INVALID_MESSAGE_PREFIX} ${mapErrorToString(score.message)}`
195+
? `${INVALID_MESSAGE_PREFIX} ${mapErrorToString(score.message, this.context.strings)}`
196196
: `${INVALID_MESSAGE_PREFIX} ${DEFAULT_INVALID_MESSAGE_1}${DEFAULT_INVALID_MESSAGE_2}`;
197197

198198
this.setState({

0 commit comments

Comments
 (0)