Skip to content

An AI skill that turns books into animated, narrated, interactive web experiences

chore(site): sync the notebook edition's engine after the i18n merge

yzr7be635c7f749
diff --git a/site/physics-notebook/lib/engine.js b/site/physics-notebook/lib/engine.js
index 207de8f..2f6ba36 100644
--- a/site/physics-notebook/lib/engine.js
+++ b/site/physics-notebook/lib/engine.js
@@ -3,67 +3,163 @@
'use strict';
// A fast follow-up navigation can abort the incoming page transition.
addEventListener('pagereveal', e => e.viewTransition?.ready.catch(() => {}));
+
+/* ---------- interface language ---------- */
+// The page's <html lang> picks the interface text; CHAPTER.language picks the audio folder. Set both alike.
+// A value is a string or plural forms { one, few, many, other } chosen by vars.n. Missing keys fall back to English.
+const TRANSLATIONS = {
+ en: {
+ decimalSign: '.', stageLabel: 'Lesson animation', stageLabelTitled: 'Lesson animation: {title}',
+ shortcuts: 'Keyboard shortcuts', lesson: 'Lesson', questions: 'Questions',
+ helpPlay: 'Play or pause', helpStep: 'Previous or next step', helpStepInQuestion: 'Change step during a question', helpRestart: 'Start over',
+ helpCaptions: 'Captions on or off', helpSpeed: 'Slower or faster', helpFullScreen: 'Full screen', helpList: 'Show this list',
+ helpChoose: 'Choose an answer or a ring', helpTrueFalse: 'True or false', helpRows: 'Move between rows',
+ helpNumberLine: 'Move along the number line, or pick a number to sort', helpEnter: 'Check, then continue',
+ helpShowAnswer: 'Show the answer', helpLeaveBox: 'Leave an answer box', helpClose: 'Press {key} to close.',
+ paused: 'Paused. Press space or click to continue.',
+ soundFailed: 'Sound could not play. The lesson continues with the on-screen text.',
+ startLesson: 'Start lesson', coverKeys: 'Press {space} to start, {help} for shortcuts.', spaceKey: 'Space',
+ chapterNumber: 'Chapter {n}', aboutMinutes: 'About {n} minutes, with sound and quick checks.',
+ allChapters: 'All chapters', playLabel: 'Play or pause (space)', backLabel: 'Previous step (left arrow)', restartLabel: 'Restart lesson',
+ volumeLabel: 'Adjust volume', volumeTitle: 'Volume (0 = muted)', volumeSlider: 'Volume',
+ helpLabel: 'Keyboard shortcuts (?)', speedLabel: 'Playback speed (< and >)', captionsLabel: 'Captions (C)', fullScreenLabel: 'Full screen', goToStep: 'Go to step {n}: {title}',
+ quickCheck: 'Quick check', countOf: '{k} of {n}', check: 'Check', showAnswer: 'Show answer', nextQuestion: 'Next question', continue: 'Continue',
+ correct: 'Correct. ', notQuite: 'Not quite. ', answerPrefix: 'Answer: ', keysPrefix: 'Keys: ',
+ hintMove: ' move, ', hintChoose: ' choose', hintChooseOrClickGrid: ' choose, or click the grid', hintMoveAlongLine: ' move along the line, ',
+ hintChooseOrClick: ' choose, or click', hintRow: ' row, ', hintTypeFraction: 'type numbers like 3/4 or −2 1/4, ', hintTypeNumber: 'type the number, ',
+ hintTypeDecimal: 'type the number, ', hintNextBox: ' next box, ', hintCheck: ' check, ', hintLeaveBox: ' leave the box',
+ hintPickNumber: ' pick a number, ', hintPickRing: ' choose its ring',
+ rowsRight: '{r} of {n} right. Fix the rows marked ✗ and check again.',
+ choicesShown: 'the correct choices are now selected.', numbersShown: 'the correct numbers are filled in.',
+ notPlacedYet: '{x} is not placed yet.',
+ realRingIsEmpty: '{x}: every real number is rational or irrational, so that space has no numbers of its own.',
+ ringsRight: '{r} of {n} in the right ring.', ringsShown: 'every number is now in its smallest ring.',
+ chipPlaced: '{x}, placed in {ring}', chipNotPlaced: '{x}, not placed', pointLabel: 'The point {v}', answerBox: 'Answer',
+ stepsLeftOfZero: '{n} steps left of 0',
+ natural: 'Natural', whole: 'Whole', integers: 'Integers', rational: 'Rational', irrational: 'Irrational', real: 'Real', integerColumn: 'Integer',
+ trueLabel: 'True', falseLabel: 'False',
+ chapterComplete: 'Chapter {n} complete', quickChecks: 'Quick checks', chapterPractice: 'Chapter practice',
+ firstTryScore: '{name}: {r} of {t} right on the first try.', notAttempted: '{name}: not attempted.',
+ watchAgain: 'Watch again', nextChapter: 'Next chapter',
+ },
+ ru: {
+ decimalSign: ',', stageLabel: 'Анимация урока', stageLabelTitled: 'Анимация урока: {title}',
+ shortcuts: 'Горячие клавиши', lesson: 'Урок', questions: 'Вопросы',
+ helpPlay: 'Пуск или пауза', helpStep: 'Предыдущий или следующий шаг', helpStepInQuestion: 'Сменить шаг во время вопроса', helpRestart: 'Начать сначала',
+ helpCaptions: 'Субтитры вкл. или выкл.', helpSpeed: 'Медленнее или быстрее', helpFullScreen: 'Полноэкранный режим', helpList: 'Показать этот список',
+ helpChoose: 'Выбрать ответ или круг', helpTrueFalse: 'Верно или неверно', helpRows: 'Перейти между строками',
+ helpNumberLine: 'Двигаться по числовой прямой или выбрать число для сортировки', helpEnter: 'Проверить, затем продолжить',
+ helpShowAnswer: 'Показать ответ', helpLeaveBox: 'Выйти из поля ответа', helpClose: 'Нажмите {key}, чтобы закрыть.',
+ paused: 'Пауза. Нажмите пробел или щёлкните, чтобы продолжить.',
+ soundFailed: 'Звук не воспроизводится. Урок продолжится с текстом на экране.',
+ startLesson: 'Начать урок', coverKeys: '{space} — начать, {help} — горячие клавиши.', spaceKey: 'Пробел',
+ chapterNumber: 'Глава {n}',
+ aboutMinutes: { one: 'Около {n} минуты, со звуком и проверками.', few: 'Около {n} минут, со звуком и проверками.',
+ many: 'Около {n} минут, со звуком и проверками.', other: 'Около {n} минуты, со звуком и проверками.' },
+ allChapters: 'Все главы', playLabel: 'Пуск или пауза (пробел)', backLabel: 'Предыдущий шаг (стрелка влево)', restartLabel: 'Начать урок заново',
+ volumeLabel: 'Настроить громкость', volumeTitle: 'Громкость (0 — без звука)', volumeSlider: 'Громкость',
+ helpLabel: 'Горячие клавиши (?)', speedLabel: 'Скорость воспроизведения (< и >)', captionsLabel: 'Субтитры (C)', fullScreenLabel: 'Полный экран', goToStep: 'Перейти к шагу {n}: {title}',
+ quickCheck: 'Проверь себя', countOf: '{k} из {n}', check: 'Проверить', showAnswer: 'Показать ответ', nextQuestion: 'Следующий вопрос', continue: 'Продолжить',
+ correct: 'Верно. ', notQuite: 'Не совсем. ', answerPrefix: 'Ответ: ', keysPrefix: 'Клавиши: ',
+ hintMove: ' двигаться, ', hintChoose: ' выбрать', hintChooseOrClickGrid: ' выбрать или щёлкнуть по сетке', hintMoveAlongLine: ' двигаться по прямой, ',
+ hintChooseOrClick: ' выбрать или щёлкнуть', hintRow: ' строка, ', hintTypeFraction: 'вводите числа вида 3/4 или −2 1/4, ', hintTypeNumber: 'введите число, ',
+ hintTypeDecimal: 'вводите числа вида 0,75, ', hintNextBox: ' следующее поле, ', hintCheck: ' проверить, ', hintLeaveBox: ' выйти из поля',
+ hintPickNumber: ' выбрать число, ', hintPickRing: ' выбрать его круг',
+ rowsRight: 'Верно {r} из {n}. Исправьте строки с ✗ и проверьте снова.',
+ choicesShown: 'правильные варианты отмечены.', numbersShown: 'правильные числа вписаны.',
+ notPlacedYet: '{x}: ещё не размещено.',
+ realRingIsEmpty: '{x}: каждое действительное число рационально или иррационально, поэтому своих чисел у этой области нет.',
+ ringsRight: 'В нужном круге: {r} из {n}.', ringsShown: 'каждое число теперь в своём наименьшем круге.',
+ chipPlaced: '{x}, в круге «{ring}»', chipNotPlaced: '{x}, не размещено', pointLabel: 'Точка {v}', answerBox: 'Ответ',
+ stepsLeftOfZero: { one: '{n} шаг влево от 0', few: '{n} шага влево от 0', many: '{n} шагов влево от 0', other: '{n} шага влево от 0' },
+ natural: 'Натуральные', whole: 'Целые неотрицательные', integers: 'Целые', rational: 'Рациональные', irrational: 'Иррациональные', real: 'Действительные', integerColumn: 'Целые',
+ trueLabel: 'Верно', falseLabel: 'Неверно',
+ chapterComplete: 'Глава {n} пройдена', quickChecks: 'Проверь себя', chapterPractice: 'Практика по главе',
+ firstTryScore: '{name}: {r} из {t} верно с первой попытки.', notAttempted: '{name}: не выполнялось.',
+ watchAgain: 'Смотреть снова', nextChapter: 'Следующая глава',
+ },
+};
+const LANGUAGE = (document.documentElement.lang || 'en').toLowerCase().split('-')[0];
+const pluralRules = {};
+// Interface text in LANGUAGE: translate('chapterNumber', { n: 3 }) → 'Chapter 3'. Chapters may use it too, e.g. for practice labels.
+function translate(key, vars = {}) {
+ const language = TRANSLATIONS[LANGUAGE]?.[key] !== undefined ? LANGUAGE : 'en';
+ let text = TRANSLATIONS[language][key] ?? key;
+ if (typeof text === 'object') {
+ try { pluralRules[language] ??= new Intl.PluralRules(language); } catch { pluralRules[language] = new Intl.PluralRules('en'); }
+ text = text[pluralRules[language].select(vars.n)] ?? text.other;
+ }
+ return text.replace(/\{(\w+)\}/g, (placeholder, name) => name in vars ? vars[name] : placeholder);
+}
+const DECIMAL_SIGN = translate('decimalSign'); // shown by num() and accepted by answer boxes
+const isLatinLetter = key => /^[a-z]$/i.test(key);
+// A letter shortcut on any keyboard layout: the typed letter, or the same physical key when the layout types a non-Latin letter.
+const isShortcut = (event, letter) => event.key.toLowerCase() === letter.toLowerCase()
+ || (isLatinLetter(letter) && !isLatinLetter(event.key) && event.code === 'Key' + letter.toUpperCase());
+// A punctuation shortcut (code 'Comma', 'Period') on a layout that types a non-Latin letter on that key.
+const isShortcutCode = (event, code) => /^\p{L}$/u.test(event.key) && !isLatinLetter(event.key) && event.code === code;
+
document.body.insertAdjacentHTML('afterbegin', `<div id="frame">
- <svg id="stage" viewBox="0 0 1600 900" role="img" aria-label="Lesson animation">
+ <svg id="stage" viewBox="0 0 1600 900" role="img" aria-label="${translate('stageLabel')}">
<g id="scene"></g>
</svg>
<div id="ui"></div>
- <div class="paused-mark"><div>Paused. Press space or click to continue.</div></div>
+ <div class="paused-mark"><div>${translate('paused')}</div></div>
<div class="caption" id="caption" hidden></div>
- <div class="help" id="help" hidden role="dialog" aria-label="Keyboard shortcuts">
+ <div class="help" id="help" hidden role="dialog" aria-label="${translate('shortcuts')}">
<div class="help-box">
- <h2>Keyboard shortcuts</h2>
+ <h2>${translate('shortcuts')}</h2>
<div class="help-cols">
- <div><h3>Lesson</h3><dl>
- <dt><kbd>Space</kbd></dt><dd>Play or pause</dd>
- <dt><kbd>←</kbd><kbd>→</kbd></dt><dd>Previous or next step</dd>
- <dt><kbd>Shift</kbd><kbd>←</kbd><kbd>→</kbd></dt><dd>Change step during a question</dd>
- <dt><kbd>Home</kbd></dt><dd>Start over</dd>
- <dt><kbd>C</kbd></dt><dd>Captions on or off</dd>
- <dt><kbd>&lt;</kbd><kbd>&gt;</kbd></dt><dd>Slower or faster</dd>
- <dt><kbd>F</kbd></dt><dd>Full screen</dd>
- <dt><kbd>?</kbd></dt><dd>Show this list</dd>
+ <div><h3>${translate('lesson')}</h3><dl>
+ <dt><kbd>${translate('spaceKey')}</kbd></dt><dd>${translate('helpPlay')}</dd>
+ <dt><kbd>←</kbd><kbd>→</kbd></dt><dd>${translate('helpStep')}</dd>
+ <dt><kbd>Shift</kbd><kbd>←</kbd><kbd>→</kbd></dt><dd>${translate('helpStepInQuestion')}</dd>
+ <dt><kbd>Home</kbd></dt><dd>${translate('helpRestart')}</dd>
+ <dt><kbd>C</kbd></dt><dd>${translate('helpCaptions')}</dd>
+ <dt><kbd>&lt;</kbd><kbd>&gt;</kbd></dt><dd>${translate('helpSpeed')}</dd>
+ <dt><kbd>F</kbd></dt><dd>${translate('helpFullScreen')}</dd>
+ <dt><kbd>?</kbd></dt><dd>${translate('helpList')}</dd>
</dl></div>
- <div><h3>Questions</h3><dl>
- <dt><kbd>1</kbd>–<kbd>6</kbd></dt><dd>Choose an answer or a ring</dd>
- <dt><kbd>T</kbd><kbd>F</kbd></dt><dd>True or false</dd>
- <dt><kbd>↑</kbd><kbd>↓</kbd></dt><dd>Move between rows</dd>
- <dt><kbd>←</kbd><kbd>→</kbd></dt><dd>Move along the number line, or pick a number to sort</dd>
- <dt><kbd>Enter</kbd></dt><dd>Check, then continue</dd>
- <dt><kbd>S</kbd></dt><dd>Show the answer</dd>
- <dt><kbd>Esc</kbd></dt><dd>Leave an answer box</dd>
+ <div><h3>${translate('questions')}</h3><dl>
+ <dt><kbd>1</kbd>–<kbd>6</kbd></dt><dd>${translate('helpChoose')}</dd>
+ <dt><kbd>T</kbd><kbd>F</kbd></dt><dd>${translate('helpTrueFalse')}</dd>
+ <dt><kbd>↑</kbd><kbd>↓</kbd></dt><dd>${translate('helpRows')}</dd>
+ <dt><kbd>←</kbd><kbd>→</kbd></dt><dd>${translate('helpNumberLine')}</dd>
+ <dt><kbd>Enter</kbd></dt><dd>${translate('helpEnter')}</dd>
+ <dt><kbd>S</kbd></dt><dd>${translate('helpShowAnswer')}</dd>
+ <dt><kbd>Esc</kbd></dt><dd>${translate('helpLeaveBox')}</dd>
</dl></div>
</div>
- <p class="help-close">Press <kbd>Esc</kbd> to close.</p>
+ <p class="help-close">${translate('helpClose', { key: '<kbd>Esc</kbd>' })}</p>
</div>
</div>
- <p class="sound-note" id="soundNote" hidden>Sound could not play. The lesson continues with the on-screen text.</p>
+ <p class="sound-note" id="soundNote" hidden>${translate('soundFailed')}</p>
<button class="cover" id="cover"><div>
<p class="cover-k" id="coverK"></p>
<h1 class="cover-t" id="coverT"></h1>
- <span class="go"><svg width="24" height="24" viewBox="0 0 18 18"><path d="M3 1.5v15l13-7.5z" fill="currentColor"/></svg>Start lesson</span>
- <small><span id="coverMeta"></span> Press <kbd>Space</kbd> to start, <kbd>?</kbd> for shortcuts.</small>
+ <span class="go"><svg width="24" height="24" viewBox="0 0 18 18"><path d="M3 1.5v15l13-7.5z" fill="currentColor"/></svg>${translate('startLesson')}</span>
+ <small><span id="coverMeta"></span> ${translate('coverKeys', { space: `<kbd>${translate('spaceKey')}</kbd>`, help: '<kbd>?</kbd>' })}</small>
</div></button>
<div id="bar">
- <a class="icon" id="bHome" href="../" aria-label="All chapters" title="All chapters">
+ <a class="icon" id="bHome" href="../" aria-label="${translate('allChapters')}" title="${translate('allChapters')}">
<svg viewBox="0 0 18 18"><path d="M2 2h6v6H2zM10 2h6v6h-6zM2 10h6v6H2zM10 10h6v6h-6z"/></svg>
</a>
- <button class="icon" id="bPlay" aria-label="Play or pause (space)">
+ <button class="icon" id="bPlay" aria-label="${translate('playLabel')}">
<svg class="i-play" viewBox="0 0 18 18"><path d="M4 2v14l12-7z"/></svg>
<svg class="i-pause" viewBox="0 0 18 18"><path d="M3.5 2h4v14h-4zM10.5 2h4v14h-4z"/></svg>
</button>
- <button class="icon" id="bBack" aria-label="Previous step (left arrow)">
+ <button class="icon" id="bBack" aria-label="${translate('backLabel')}">
<svg viewBox="0 0 18 18"><path d="M3 2h2.5v14H3zM16 2v14L6.5 9z"/></svg>
</button>
- <button class="icon" id="bRestart" aria-label="Restart lesson">
+ <button class="icon" id="bRestart" aria-label="${translate('restartLabel')}">
<svg viewBox="0 0 18 18"><path d="M9 2.5a6.5 6.5 0 1 1-6.3 8.1l2-.5A4.5 4.5 0 1 0 9 4.5V7L4.5 3.5 9 0z"/></svg>
</button>
<div class="segs" id="segs"></div>
<div class="step" id="stepName"></div>
- <button class="icon txt" id="bHelp" aria-label="Keyboard shortcuts (?)" title="Keyboard shortcuts (?)">?</button>
- <button class="icon txt speed" id="bSpeed" aria-label="Playback speed (&lt; and &gt;)" title="Playback speed (&lt; and &gt;)">1×</button>
- <button class="icon cc" id="bCC" aria-pressed="false" aria-label="Captions (C)" title="Captions (C)">CC</button>
- <button class="icon" id="bFull" aria-label="Full screen">
+ <button class="icon txt" id="bHelp" aria-label="${translate('helpLabel')}" title="${translate('helpLabel')}">?</button>
+ <button class="icon txt speed" id="bSpeed" aria-label="${translate('speedLabel')}" title="${translate('speedLabel')}">1×</button>
+ <button class="icon cc" id="bCC" aria-pressed="false" aria-label="${translate('captionsLabel')}" title="${translate('captionsLabel')}">CC</button>
+ <button class="icon" id="bFull" aria-label="${translate('fullScreenLabel')}">
<svg viewBox="0 0 18 18"><path d="M1 1h6v2H3v4H1zM11 1h6v6h-2V3h-4zM1 11h2v4h4v2H1zM15 11h2v6h-6v-2h4z"/></svg>
</button>
</div>
@@ -89,7 +185,7 @@ const UI = '"Avenir Next","Segoe UI","Helvetica Neue",Arial,sans-serif';
const BOOK = getComputedStyle(document.documentElement);
const bookSetting = name => BOOK.getPropertyValue(name).trim().replace(/^(["'])([^"']*)\1$/, '$2'); // unquote a lone string only
const MATH = bookSetting('--math-font') || '"STIX Two Text","Cambria Math","Iowan Old Style",Palatino,Georgia,serif';
-const NAME = { nat: 'Natural', whole: 'Whole', int: 'Integers', rat: 'Rational', irr: 'Irrational', real: 'Real' };
+const NAME = { nat: translate('natural'), whole: translate('whole'), int: translate('integers'), rat: translate('rational'), irr: translate('irrational'), real: translate('real') };
// Card positions inside the 1600×900 picture.
const BAND = { x: 96, y: 686, w: 1408, cls: 'band' }; // bottom band (moves up when taller than the space left)
const RIGHT = { x: 1258, y: 546, w: 330, cls: 'side', maxH: 346 }; // right of the number map
@@ -758,7 +854,7 @@ const pickPoint = (P, ans, yes, no, onRight, test) => (body, api) => {
return {
reveal() { moveTo(...ans); P.dot(ans[0], ans[1], COL.good, 0, { run: fx, into: L }); onRight?.(); return typeof yes === 'function' ? yes(...ans) : yes; },
lock() { locked = true; },
- hint: [kbd('←'), kbd('↑'), kbd('↓'), kbd('→'), ' move, ', kbd('Enter'), ' choose, or click the grid'],
+ hint: [kbd('←'), kbd('↑'), kbd('↓'), kbd('→'), translate('hintMove'), kbd('Enter'), translate('hintChooseOrClickGrid')],
key(e) {
if (locked) return false;
const d = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, 1], ArrowDown: [0, -1] }[e.key];
@@ -987,7 +1083,7 @@ const scene = document.getElementById('scene');
const S = {};
const AXIS = { o: 800, u: 120 }; // number line: x of 0, pixels per unit; a chapter may change it
const X = v => AXIS.o + AXIS.u * v, Y0 = 610;
-const num = v => (v < 0 ? '−' : '') + Math.abs(v);
+const num = v => (v < 0 ? '−' : '') + String(Math.abs(v)).replace('.', DECIMAL_SIGN);
function reset() {
scene.replaceChildren();
for (const k in S) delete S[k];
@@ -1058,29 +1154,29 @@ function hopsFx(n) {
const a = X(1 - k), b = X(-k);
fx(path(S.q, `M${a} ${Y0 - 12}Q${(a + b) / 2} ${Y0 - 84} ${b} ${Y0 - 12}`, { stroke: COL.int, 'stroke-width': 3 }, { d: 0 }), { d: 1 }, (k - 1) * .35, .35, lin);
}
- floatText(`${-n} steps left of 0`, X(n / 2), Y0 - 100, COL.int);
+ floatText(translate('stepsLeftOfZero', { n: -n }), X(n / 2), Y0 - 100, COL.int);
}
/* ---------- shared answer columns ---------- */
const FAMILIES = ['nat', 'whole', 'int', 'rat', 'irr', 'real'];
-const FAM_COLS = FAMILIES.map(k => [k, k === 'int' ? 'Integer' : NAME[k], COL[k]]);
-const TF = [['t', 'True', null, 'T'], ['f', 'False', null, 'F']];
+const FAM_COLS = FAMILIES.map(k => [k, k === 'int' ? translate('integerColumn') : NAME[k], COL[k]]);
+const TF = [['t', translate('trueLabel'), null, 'T'], ['f', translate('falseLabel'), null, 'F']];
function finishCard() {
const c = card(SCREEN, 'fin');
const sum = pre => Object.entries(SCORE).filter(([k]) => k.startsWith(pre)).reduce((a, [, v]) => [a[0] + v.right, a[1] + v.total], [0, 0]);
- const line = (name, [r, t]) => h('p', 'fb', t ? `${name}: ${r} of ${t} right on the first try.` : `${name}: not attempted.`);
+ const line = (name, [r, t]) => h('p', 'fb', t ? translate('firstTryScore', { name, r, t }) : translate('notAttempted', { name }));
progress(p => { p.done = [...new Set([...(p.done || []), CHAPTER.number])]; });
- const home = h('a', 'btn quiet', 'All chapters');
+ const home = h('a', 'btn quiet', translate('allChapters'));
home.href = $('bHome').href;
- const again = h('button', 'btn' + (CHAPTER.next ? ' quiet' : ' go'), ['Watch again', kbd(CHAPTER.next ? 'R' : 'Enter')]);
+ const again = h('button', 'btn' + (CHAPTER.next ? ' quiet' : ' go'), [translate('watchAgain'), kbd(CHAPTER.next ? 'R' : 'Enter')]);
again.onclick = restart;
- const next = CHAPTER.next && h('button', 'btn go', ['Next chapter', kbd('Enter')]);
+ const next = CHAPTER.next && h('button', 'btn go', [translate('nextChapter'), kbd('Enter')]);
if (next) next.onclick = () => { location.href = CHAPTER.next; };
P.keys = e => e.key === 'Enter' ? ((next ? next.onclick() : restart()), true)
- : (e.key === 'r' || e.key === 'R') ? (restart(), true) : false;
+ : isShortcut(e, 'r') ? (restart(), true) : false;
const guide = mascotEl(240);
- c.append(h('div', 'finwrap', [guide, h('p', 'kicker', `Chapter ${CHAPTER.number} complete`), h('p', 'prompt', CHAPTER.title),
- line('Quick checks', sum('c-')), line('Chapter practice', sum('p-')), h('div', 'acts', next ? [home, again, next] : [home, again])]));
+ c.append(h('div', 'finwrap', [guide, h('p', 'kicker', translate('chapterComplete', { n: CHAPTER.number })), h('p', 'prompt', CHAPTER.title),
+ line(translate('quickChecks'), sum('c-')), line(translate('chapterPractice'), sum('p-')), h('div', 'acts', next ? [home, again, next] : [home, again])]));
setTimeout(() => mood(guide, 'happy'), 400);
}
@@ -1149,15 +1245,15 @@ function mood(el, m) {
if (m) el.classList.add(m);
}
// A sequence of questions in one card. Each question: { id, prompt, build(body, api) -> { check?, reveal, lock? } }.
-function quiz(pos, qs, done, label = 'Quick check') {
+function quiz(pos, qs, done, label = translate('quickCheck')) {
const c = card(pos, 'quiz');
let k = 0;
const show = () => {
const q = qs[k];
let first = true, resolved = false;
const fb = h('div', 'fb'), body = h('div', 'body'), acts = h('div', 'acts');
- const bCheck = h('button', 'btn', ['Check', kbd('Enter')]), bShow = h('button', 'btn quiet', ['Show answer', kbd('S')]);
- const bNext = h('button', 'btn go', [k < qs.length - 1 ? 'Next question' : 'Continue', kbd('Enter')]);
+ const bCheck = h('button', 'btn', [translate('check'), kbd('Enter')]), bShow = h('button', 'btn quiet', [translate('showAnswer'), kbd('S')]);
+ const bNext = h('button', 'btn go', [k < qs.length - 1 ? translate('nextQuestion') : translate('continue'), kbd('Enter')]);
bShow.hidden = bNext.hidden = true;
const guide = pos.cls === 'band' || pos.cls === 'side' ? mascotEl(pos.cls === 'band' ? 108 : 70) : null;
const say = (cls, parts) => { fb.className = 'fb ' + cls; fb.replaceChildren(rich(parts, 24)); fb.style.animation = 'none'; void fb.offsetWidth; fb.style.animation = ''; };
@@ -1172,7 +1268,7 @@ function quiz(pos, qs, done, label = 'Quick check') {
grade(ok, msg, score) {
if (resolved) return;
if (first) { if (!Object.hasOwn(SCORE, q.id)) SCORE[q.id] = score || { right: +ok, total: 1 }; first = false; }
- say(ok ? 'ok' : 'no', [ok ? 'Correct. ' : 'Not quite. ', ...[].concat(msg || [])]);
+ say(ok ? 'ok' : 'no', [ok ? translate('correct') : translate('notQuite'), ...[].concat(msg || [])]);
if (guide) {
mood(guide, ok ? 'happy' : 'oops');
if (!ok) setTimeout(() => { if (guide.classList.contains('oops')) mood(guide, null); }, 1600);
@@ -1183,18 +1279,18 @@ function quiz(pos, qs, done, label = 'Quick check') {
const ctl = q.build(body, api);
if (ctl.check) { bCheck.onclick = () => ctl.check(); acts.append(bCheck); }
acts.append(bShow, bNext);
- bShow.onclick = () => { say('', ['Answer: ', ...[].concat(ctl.reveal())]); resolve(); };
+ bShow.onclick = () => { say('', [translate('answerPrefix'), ...[].concat(ctl.reveal())]); resolve(); };
bNext.onclick = () => { if (k < qs.length - 1) { k++; show(); } else done(); };
P.keys = e => {
if (e.key === 'Enter' && resolved) { bNext.click(); return true; }
if (!resolved && ctl.key?.(e)) return true;
if (e.key === 'Enter' && !resolved && ctl.check) { bCheck.click(); return true; }
- if ((e.key === 's' || e.key === 'S') && !bShow.hidden) { bShow.click(); return true; }
+ if (isShortcut(e, 's') && !bShow.hidden) { bShow.click(); return true; }
return false;
};
- const kick = label + (qs.length > 1 ? ` ${k + 1} of ${qs.length}` : '');
+ const kick = label + (qs.length > 1 ? ' ' + translate('countOf', { k: k + 1, n: qs.length }) : '');
const head = h('div', 'head', [h('p', 'kicker', kick), h('p', 'prompt', [rich(q.prompt, pos.cls.startsWith('screen') ? 38 : 28)])]); // match the prompt's type size
- if (ctl.hint) head.append(h('p', 'keys', ['Keys: ', ...ctl.hint]));
+ if (ctl.hint) head.append(h('p', 'keys', [translate('keysPrefix'), ...ctl.hint]));
c.replaceChildren(...(guide ? [guide] : []), head, fb, body, acts);
};
show();
@@ -1236,7 +1332,7 @@ const tap = (values, right, yes, no, onRight) => (body, api) => {
g.classList.add('hit');
g.setAttribute('tabindex', 0);
g.setAttribute('role', 'button');
- g.setAttribute('aria-label', `The point ${num(v)}`);
+ g.setAttribute('aria-label', translate('pointLabel', { v: num(v) }));
mk('circle', { r: 30, class: 'ring' }, g);
const act = () => {
if (locked) return;
@@ -1252,7 +1348,7 @@ const tap = (values, right, yes, no, onRight) => (body, api) => {
return {
reveal() { hits[right].classList.add('good'); onRight?.(); return yes; },
lock() { locked = true; },
- hint: [kbd('←'), kbd('→'), ' move along the line, ', kbd('Enter'), ' choose'],
+ hint: [kbd('←'), kbd('→'), translate('hintMoveAlongLine'), kbd('Enter'), translate('hintChoose')],
key(e) {
const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
if (!d || locked) return false;
@@ -1285,7 +1381,7 @@ const tapEls = (row, idx, right, yes, no, onRight) => (body, api) => {
return {
reveal() { hits[idx.indexOf(right)].classList.add('good'); onRight?.(); return yes; },
lock() { locked = true; },
- hint: [kbd('←'), kbd('→'), ' move, ', kbd('Enter'), ' choose'],
+ hint: [kbd('←'), kbd('→'), translate('hintMove'), kbd('Enter'), translate('hintChoose')],
key(e) {
const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
if (!d || locked) return false;
@@ -1318,7 +1414,7 @@ const pickEls = (items, right, yes, no, onRight) => (body, api) => {
return {
reveal() { hits[right].classList.add('good'); onRight?.(); return yes; },
lock() { locked = true; },
- hint: [kbd('←'), kbd('→'), ' move, ', kbd('Enter'), ' choose, or click'],
+ hint: [kbd('←'), kbd('→'), translate('hintMove'), kbd('Enter'), translate('hintChooseOrClick')],
key(e) {
const d = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;
if (!d || locked) return false;
@@ -1374,7 +1470,7 @@ const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) =
check() {
const right = rows.filter(judge).length;
const all = right === rows.length;
- api.grade(all, single ? rows[0].it.why : all ? [] : [`${right} of ${rows.length} right. Fix the rows marked ✗ and check again.`], { right, total: rows.length });
+ api.grade(all, single ? rows[0].it.why : all ? [] : [translate('rowsRight', { r: right, n: rows.length })], { right, total: rows.length });
},
reveal() {
for (const r of rows) {
@@ -1382,10 +1478,10 @@ const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) =
judge(r);
if (!single) r.why.replaceChildren(rich(r.it.why, 20));
}
- return single ? rows[0].it.why : 'the correct choices are now selected.';
+ return single ? rows[0].it.why : translate('choicesShown');
},
lock() { locked = true; showCur(); rows.forEach(r => r.bs.forEach(b => { b.disabled = true; })); },
- hint: [...(single ? [] : [kbd('↑'), kbd('↓'), ' row, ']), ...(/\d/.test(hot[0]) ? [kbd(hot[0]), '–', kbd(hot[hot.length - 1])] : hot.map(kbd)), ' choose'],
+ hint: [...(single ? [] : [kbd('↑'), kbd('↓'), translate('hintRow')]), ...(/\d/.test(hot[0]) ? [kbd(hot[0]), '–', kbd(hot[hot.length - 1])] : hot.map(kbd)), translate('hintChoose')],
key(e) {
if (locked) return false;
if ((e.key === 'ArrowDown' || e.key === 'ArrowUp') && !single) {
@@ -1393,7 +1489,7 @@ const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) =
showCur();
return true;
}
- const j = hot.findIndex(x => x.toLowerCase() === e.key.toLowerCase());
+ const j = hot.findIndex(x => isShortcut(e, x));
if (j < 0) return false;
rows[cur].bs[j].click();
if (!multi && cur < rows.length - 1) { cur++; showCur(); }
@@ -1402,9 +1498,10 @@ const grid = (items, cols, { multi = true, text = false } = {}) => (body, api) =
};
};
// Exact value of a typed answer: integer, decimal, fraction a/b or mixed number "w a/b"; returns [num, den] or null.
-// Accepts −, – or - as the sign. Never evaluates the text as code.
+// Accepts −, – or - as the sign, and a decimal comma when DECIMAL_SIGN is ','. Never evaluates the text as code.
function rat(str) {
- const t = String(str).trim().replace(/^[−–]/, '-').replace(/\s+/g, ' ');
+ let t = String(str).trim().replace(/^[−–]/, '-').replace(/\s+/g, ' ');
+ if (DECIMAL_SIGN === ',') t = t.replace(/^(-?\d+),(\d+)$/, '$1.$2'); // 0,75 where the decimal sign is a comma
let m = t.match(/^(-?)(\d+)(?:\.(\d+))?$/);
if (m) { const d = 10 ** (m[3] || '').length, n = (m[1] ? -1 : 1) * (Number(m[2]) * d + Number(m[3] || 0)); return Number.isSafeInteger(n) && Number.isSafeInteger(d) ? [n, d] : null; }
m = t.match(/^(-?)(?:(\d+) )?(\d+) ?\/ ?(\d+)$/);
@@ -1429,7 +1526,7 @@ const blanks = rowsIn => (body, api) => {
flush();
const b = h('input', 'box');
Object.assign(b, { type: 'text', inputMode: 'decimal', autocomplete: 'off', spellcheck: false });
- b.setAttribute('aria-label', 'Answer');
+ b.setAttribute('aria-label', translate('answerBox'));
b.dataset.ans = p.box;
if (p.lowest) b.dataset.lowest = '1';
b.style.width = `${Math.max(2, String(p.box).length) + 2}ch`;
@@ -1469,7 +1566,7 @@ const blanks = rowsIn => (body, api) => {
check() {
const right = rows.filter(judge).length, done = right === rows.length;
const msg = single ? (done ? rows[0].it.why : rows[0].it.hint ?? rows[0].it.why)
- : done ? [] : [`${right} of ${rows.length} right. Fix the rows marked ✗ and check again.`];
+ : done ? [] : [translate('rowsRight', { r: right, n: rows.length })];
api.grade(done, msg, { right, total: rows.length });
},
reveal() {
@@ -1478,10 +1575,11 @@ const blanks = rowsIn => (body, api) => {
judge(r);
if (!single) r.why.replaceChildren(rich(r.it.why, 20));
}
- return single ? rows[0].it.why : 'the correct numbers are filled in.';
+ return single ? rows[0].it.why : translate('numbersShown');
},
lock() { all.forEach(b => { b.disabled = true; }); },
- hint: [all.some(b => b.dataset.ans.includes('/')) ? 'type numbers like 3/4 or −2 1/4, ' : 'type the number, ', kbd('Tab'), ' next box, ', kbd('Enter'), ' check, ', kbd('Esc'), ' leave the box'],
+ hint: [translate(all.some(b => b.dataset.ans.includes('/')) ? 'hintTypeFraction' : all.some(b => /[.,]/.test(b.dataset.ans)) ? 'hintTypeDecimal' : 'hintTypeNumber'),
+ kbd('Tab'), translate('hintNextBox'), kbd('Enter'), translate('hintCheck'), kbd('Esc'), translate('hintLeaveBox')],
};
};
// Drag number chips into the stage number map. items: [parts, smallest ring, explanation].
@@ -1497,7 +1595,7 @@ const sorter = (items, trayXY) => (body, api) => {
const svg = $('stage');
const pt = e => { const p = svg.createSVGPoint(); p.x = e.clientX; p.y = e.clientY; return p.matrixTransform(svg.getScreenCTM().inverse()); };
let sel = null, drag = null, locked = false;
- const label = c => c.setAttribute('aria-label', `${c.say}, ${c.region ? 'placed in ' + NAME[c.region] : 'not placed'}`);
+ const label = c => c.setAttribute('aria-label', c.region ? translate('chipPlaced', { x: c.say, ring: NAME[c.region] }) : translate('chipNotPlaced', { x: c.say }));
const outline = (c, colr, w = 2.5) => { c._rect.setAttribute('stroke', colr); c._rect.setAttribute('stroke-width', w); };
const choose = c => {
if (sel) outline(sel, COL.chalk);
@@ -1559,7 +1657,7 @@ const sorter = (items, trayXY) => (body, api) => {
const notes = h('ul');
for (const c of chips) {
c.querySelector('.mark')?.remove();
- if (!c.region) { notes.append(h('li', '', `${c.say} is not placed yet.`)); continue; }
+ if (!c.region) { notes.append(h('li', '', translate('notPlacedYet', { x: c.say }))); continue; }
const ok = c.region === c.answer;
right += ok;
outline(c, ok ? COL.good : COL.bad, 4);
@@ -1567,11 +1665,11 @@ const sorter = (items, trayXY) => (body, api) => {
const w = +c._rect.getAttribute('width');
T(c, ok ? '✓' : '✗', { x: w / 2 + 4, y: -16, size: 26, fill: ok ? COL.good : COL.bad, weight: 700 }).classList.add('mark');
if (!ok) notes.append(h('li', '', c.region === 'real'
- ? `${c.say}: every real number is rational or irrational, so that space has no numbers of its own.`
+ ? translate('realRingIsEmpty', { x: c.say })
: c.why));
}
const all = right === chips.length;
- api.grade(all, all ? [] : [`${right} of ${chips.length} in the right ring.`, notes], { right, total: chips.length });
+ api.grade(all, all ? [] : [translate('ringsRight', { r: right, n: chips.length }), notes], { right, total: chips.length });
},
reveal() {
const count = {};
@@ -1583,10 +1681,10 @@ const sorter = (items, trayXY) => (body, api) => {
outline(c, COL.good, 4);
fx(c, { x, y }, i * .12, .8);
});
- return 'every number is now in its smallest ring.';
+ return translate('ringsShown');
},
lock() { locked = true; choose(null); },
- hint: [kbd('←'), kbd('→'), ' pick a number, ', kbd('1'), '–', kbd('6'), ' choose its ring'],
+ hint: [kbd('←'), kbd('→'), translate('hintPickNumber'), kbd('1'), '–', kbd('6'), translate('hintPickRing')],
key(e) {
if (locked) return false;
const d = e.key === 'ArrowRight' || e.key === 'ArrowDown' ? 1 : e.key === 'ArrowLeft' || e.key === 'ArrowUp' ? -1 : 0;
@@ -1749,7 +1847,7 @@ function buildSegs() {
const s = h('button', 'seg' + (b.ask ? ' ask' : ''), [h('i')]);
s.style.flex = TIMINGS[b.id].dur + (b.ask ? 12 : 0);
s.title = b.title;
- s.setAttribute('aria-label', `Go to step ${i + 1}: ${b.title}`);
+ s.setAttribute('aria-label', translate('goToStep', { n: i + 1, title: b.title }));
// A click lands at that point of the step; keyboard activation (no pointer position) starts the step.
s.onclick = e => {
const r = s.getBoundingClientRect();
@@ -1827,10 +1925,10 @@ document.addEventListener('keydown', e => {
else if (k === 'ArrowLeft' && free) back1();
else if (k === 'ArrowRight' && free) { if (P.i < BEATS.length - 1) seek(P.i + 1); }
else if (k === 'Home') restart();
- else if (k === 'c' || k === 'C') setCaptions(!captions);
- else if (k === '<' || k === ',') stepSpeed(-1);
- else if (k === '>' || k === '.') stepSpeed(1);
- else if (k === 'f' || k === 'F') toggleFull();
+ else if (isShortcut(e, 'c')) setCaptions(!captions);
+ else if (k === '<' || k === ',' || isShortcutCode(e, 'Comma')) stepSpeed(-1);
+ else if (k === '>' || k === '.' || isShortcutCode(e, 'Period')) stepSpeed(1);
+ else if (isShortcut(e, 'f')) toggleFull();
else used = false;
if (used) e.preventDefault();
});
@@ -1854,12 +1952,12 @@ function progress(f) {
} catch {}
}
function boot() {
- $('coverK').textContent = `Chapter ${CHAPTER.number}`;
+ $('coverK').textContent = translate('chapterNumber', { n: CHAPTER.number });
$('coverT').textContent = CHAPTER.title;
$('bHome').href = `../#ch${String(CHAPTER.number).padStart(2, '0')}`;
progress(p => { p.last = CHAPTER.number; });
- document.getElementById('stage').setAttribute('aria-label', `Lesson animation: ${CHAPTER.title}`);
- $('coverMeta').textContent = `About ${CHAPTER.minutes} minutes, with sound and quick checks.`;
+ document.getElementById('stage').setAttribute('aria-label', translate('stageLabelTitled', { title: CHAPTER.title }));
+ $('coverMeta').textContent = translate('aboutMinutes', { n: num(CHAPTER.minutes) });
buildSegs();
const qs = new URLSearchParams(location.search);
if (qs.has('beat')) {
diff --git a/site/physics-notebook/lib/volume.js b/site/physics-notebook/lib/volume.js
index 91695ee..8616688 100644
--- a/site/physics-notebook/lib/volume.js
+++ b/site/physics-notebook/lib/volume.js
@@ -3,13 +3,13 @@
'use strict';
{
document.getElementById('bHelp').insertAdjacentHTML('beforebegin', `<div class="volume">
- <button class="icon" id="bVolume" aria-label="Adjust volume" aria-controls="volume" title="Volume (0 = muted)">
+ <button class="icon" id="bVolume" aria-label="${translate('volumeLabel')}" aria-controls="volume" title="${translate('volumeTitle')}">
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M3 9v6h4l5 4V5L7 9z" fill="currentColor"/>
<path d="M16 8a6 6 0 0 1 0 8m3-11a10 10 0 0 1 0 14" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/>
</svg>
</button>
- <input id="volume" type="range" min="0" max="1" step="0.05" value="1" aria-label="Volume">
+ <input id="volume" type="range" min="0" max="1" step="0.05" value="1" aria-label="${translate('volumeSlider')}">
</div>`);
const input = document.getElementById('volume');
document.getElementById('bVolume').onclick = () => input.focus();