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

Bugfix: error messages aren't displayed as translated strings #2160

Merged
merged 4 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wise-insects-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus": patch
---

Bugfix: use translated strings in mapErrorToString
18 changes: 18 additions & 0 deletions packages/perseus/src/strings.test.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay for future proofing!

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {clone} from "../../../testing/object-utils";

import {mapErrorToString, mockStrings} from "./strings";

describe("mapErrorToString", () => {
it("handles translated strings", () => {
// Assemble
const translated = clone(mockStrings);
translated.MISSING_PERCENT_ERROR =
"pretend this is a different language";

// Act
const rv = mapErrorToString("MISSING_PERCENT_ERROR", translated);

// Assert
expect(rv).toBe("pretend this is a different language");
});
});
38 changes: 24 additions & 14 deletions packages/perseus/src/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -976,26 +976,36 @@ export const mockStrings: PerseusStrings = {

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

/**
* Map an error string to a PerseusStrings key
* that we can use to get the translated error message
*/
const errorToString: ErrorStringMap = {
MISSING_PERCENT_ERROR: strings.MISSING_PERCENT_ERROR,
NEEDS_TO_BE_SIMPLIFIED_ERROR: strings.NEEDS_TO_BE_SIMPLFIED_ERROR,
APPROXIMATED_PI_ERROR: strings.APPROXIMATED_PI_ERROR,
EXTRA_SYMBOLS_ERROR: strings.EXTRA_SYMBOLS_ERROR,
WRONG_CASE_ERROR: strings.WRONG_CASE_ERROR,
WRONG_LETTER_ERROR: strings.WRONG_LETTER_ERROR,
MULTIPLICATION_SIGN_ERROR: strings.MULTIPLICATION_SIGN_ERROR,
INVALID_SELECTION_ERROR: strings.invalidSelection,
CHOOSE_CORRECT_NUM_ERROR: strings.chooseCorrectNum,
NOT_NONE_ABOVE_ERROR: strings.notNoneOfTheAbove,
FILL_ALL_CELLS_ERROR: strings.fillAllCells,
MISSING_PERCENT_ERROR: "MISSING_PERCENT_ERROR",
NEEDS_TO_BE_SIMPLIFIED_ERROR: "NEEDS_TO_BE_SIMPLFIED_ERROR",
APPROXIMATED_PI_ERROR: "APPROXIMATED_PI_ERROR",
EXTRA_SYMBOLS_ERROR: "EXTRA_SYMBOLS_ERROR",
WRONG_CASE_ERROR: "WRONG_CASE_ERROR",
WRONG_LETTER_ERROR: "WRONG_LETTER_ERROR",
MULTIPLICATION_SIGN_ERROR: "MULTIPLICATION_SIGN_ERROR",
INVALID_SELECTION_ERROR: "invalidSelection",
CHOOSE_CORRECT_NUM_ERROR: "chooseCorrectNum",
NOT_NONE_ABOVE_ERROR: "notNoneOfTheAbove",
FILL_ALL_CELLS_ERROR: "fillAllCells",
};

export function mapErrorToString(err: string | null | undefined) {
export function mapErrorToString(
// the string representing an error code
err: string | null | undefined,
// the translated Perseus strings
translatedStrings: PerseusStrings,
) {
if (!err) {
return err;
}
return errorToString[err] || err;

return translatedStrings[errorToString[err]] || err;
}
2 changes: 1 addition & 1 deletion packages/perseus/src/widgets/graded-group/graded-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export class GradedGroup
score.type === "points"
? score.message || ""
: score.message
? `${INVALID_MESSAGE_PREFIX} ${mapErrorToString(score.message)}`
? `${INVALID_MESSAGE_PREFIX} ${mapErrorToString(score.message, this.context.strings)}`
: `${INVALID_MESSAGE_PREFIX} ${DEFAULT_INVALID_MESSAGE_1}${DEFAULT_INVALID_MESSAGE_2}`;

this.setState({
Expand Down
Loading