Skip to content

Commit 9340375

Browse files
authored
Translate demo instructions (#1230)
## What's Changed? - Converted `demoInstructions.md` into a tetmplate that references the keys from the translation files - Added a helper to replace the keys in the template with the correct translations - Applied this to the demo instructions when they are set as the project instructions when the user clicks "Add instructions"
1 parent a7fbfa8 commit 9340375

7 files changed

Lines changed: 81 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66

77
## Unreleased
88

9-
## Changed
9+
### Added
1010

11+
- Ability to translate demo project instructions (#1230)
12+
13+
### Changed
14+
15+
- Improved status bar styling (#1221)
16+
- Added method to translate last saved time (#1223)
1117
- Deleting unused strings and components (#1225)
1218

13-
## Fixed
19+
### Fixed
1420

1521
- Styling issue preventing scrolling in the sidebar (#1216)
1622
- Styling issue on status bar on mobile (#1217)
@@ -20,11 +26,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
2026
- Removed fixed size from `ProjectBar` to prevent overflow when text wraps (#1221)
2127
- Added missing translation strings (#1222)
2228

23-
## Changed
24-
25-
- Improved status bar styling (#1221)
26-
- Added method to translate last saved time (#1223)
27-
2829
## [0.30.1] - 2025-06-09
2930

3031
### Added

public/translations/en.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@
7878
"info": "Information"
7979
},
8080
"instructionsPanel": {
81+
"demoInstructions": {
82+
"title": "How instructions work",
83+
"enablingInstructions": "Enabling instructions",
84+
"visible": "Any text written here will be visible to students in the sidebar.",
85+
"writingInstructions": "Writing instructions",
86+
"markdown": "Write your instructions using [Markdown](https://www.markdownguide.org/).",
87+
"whatYouCanDo": "What you can do",
88+
"lists": "Lists",
89+
"bulletPoints": "Bullet points",
90+
"numberedSteps": "Numbered steps"
91+
},
8192
"emptyState": {
8293
"addInstructions": "Add instructions",
8394
"edits": "Like project code, students will not see any edits you make to the instructions after they have saved their version of the project.",

public/translations/xx-XX.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@
7878
"info": "Informationen"
7979
},
8080
"instructionsPanel": {
81+
"demoInstructions": {
82+
"title": "So funktionieren Anweisungen",
83+
"enablingInstructions": "Anweisungen aktivieren",
84+
"visible": "Jeder hier geschriebene Text ist für die Schüler in der Seitenleiste sichtbar.",
85+
"writingInstructions": "Anweisungen schreiben",
86+
"markdown": "Schreiben Sie Ihre Anweisungen mit [Markdown](https://www.markdownguide.org/).",
87+
"whatYouCanDo": "Was Sie tun können",
88+
"lists": "Listen",
89+
"bulletPoints": "Aufzählungszeichen",
90+
"numberedSteps": "Nummerierte Schritte"
91+
},
8192
"emptyState": {
8293
"addInstructions": "Anweisungen hinzufügen",
8394
"edits": "Wie beim Projektcode sehen die Schüler keine Änderungen, die Sie an den Anweisungen vornehmen, nachdem sie ihre Version des Projekts gespeichert haben.",
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
# How instructions work
1+
# {{instructionsPanel.demoInstructions.title}}
22

3-
## Enabling instructions
3+
## {{instructionsPanel.demoInstructions.enablingInstructions}}
44

5-
Any text written here will be visible to students in the sidebar
5+
{{instructionsPanel.demoInstructions.visible}}
66

7-
## Writing instructions
7+
## {{instructionsPanel.demoInstructions.writingInstructions}}
88

9-
Write your instructions using [Markdown](https://www.markdownguide.org/)
10-
### What you can do
9+
{{instructionsPanel.demoInstructions.markdown}}
1110

12-
Lists:
11+
### {{instructionsPanel.demoInstructions.whatYouCanDo}}
1312

14-
- Bullet points
15-
- Bullet points
13+
{{instructionsPanel.demoInstructions.lists}}:
1614

17-
1. numbered steps
18-
2. numbered steps
15+
- {{instructionsPanel.demoInstructions.bulletPoints}}
16+
- {{instructionsPanel.demoInstructions.bulletPoints}}
17+
18+
1. {{instructionsPanel.demoInstructions.numberedSteps}}
19+
2. {{instructionsPanel.demoInstructions.numberedSteps}}

src/components/Menus/Sidebar/InstructionsPanel/InstructionsPanel.jsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import demoInstructions from "../../../../assets/markdown/demoInstructions.md";
2020
import RemoveInstructionsModal from "../../../Modals/RemoveInstructionsModal";
2121
import Prism from "prismjs";
2222
import "prismjs/components/prism-python";
23+
import populateMarkdownTemplate from "../../../../utils/populateMarkdownTemplate";
2324

2425
const InstructionsPanel = () => {
2526
const [showModal, setShowModal] = useState(false);
@@ -110,7 +111,11 @@ const InstructionsPanel = () => {
110111
}, [quizCompleted, currentStepPosition, numberOfSteps, dispatch, isQuiz]);
111112

112113
const addInstructions = () => {
113-
dispatch(setProjectInstructions(demoInstructions));
114+
const translatedInstructions = populateMarkdownTemplate(
115+
demoInstructions,
116+
t,
117+
);
118+
dispatch(setProjectInstructions(translatedInstructions));
114119
};
115120

116121
const removeInstructions = () => {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const populateMarkdownTemplate = (markdown, t) => {
2+
return markdown.replace(/{{(.*?)}}/g, (_, key) => {
3+
const translation = t(key);
4+
return translation ? translation : `{{${key}}}`;
5+
});
6+
};
7+
8+
export default populateMarkdownTemplate;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import populateMarkdownTemplate from "./populateMarkdownTemplate";
2+
3+
describe("populatemarkdownTemplate", () => {
4+
const t = (key) => {
5+
const translations = {
6+
name: "Alice",
7+
place: "Wonderland",
8+
};
9+
return translations[key];
10+
};
11+
12+
it("should replace placeholders with translations", () => {
13+
const markdown = "Hello, {{name}}! Welcome to {{place}}.";
14+
15+
const result = populateMarkdownTemplate(markdown, t);
16+
expect(result).toBe("Hello, Alice! Welcome to Wonderland.");
17+
});
18+
19+
it("should leave placeholders unchanged if no translation is found", () => {
20+
const markdown = "Hello, {{anotherName}}! Welcome to {{anotherPlace}}.";
21+
22+
const result = populateMarkdownTemplate(markdown, t);
23+
expect(result).toBe("Hello, {{anotherName}}! Welcome to {{anotherPlace}}.");
24+
});
25+
});

0 commit comments

Comments
 (0)