|
3 | 3 | 'use strict'; |
4 | 4 |
|
5 | 5 | const baseUrl = (document.querySelector('meta[name="base-url"]') || {}).content || ''; |
| 6 | + const pageLocale = (document.documentElement.lang || 'en').toLowerCase(); |
6 | 7 |
|
7 | 8 |
|
8 | 9 | /* ================================================================ |
|
98 | 99 | return /^\s*(import\s+\w+|from\s+\w+\s+import\s+|#!\/usr\/bin\/env\s+python\d*(?:\.\d+)*)/m.test(src); |
99 | 100 | } |
100 | 101 |
|
| 102 | + function defaultExecutionLanguage() { |
| 103 | + return pageLocale === 'fr' ? 'fr' : 'en'; |
| 104 | + } |
| 105 | + |
| 106 | + function resolveExecutionLanguage(pre, code) { |
| 107 | + return pre.dataset.lang || code.dataset.lang || defaultExecutionLanguage(); |
| 108 | + } |
| 109 | + |
101 | 110 | /* Render output (stdout + optional stderr) into a .code-output element. */ |
102 | 111 | function renderOutput(container, { stdout, stderr }) { |
103 | 112 | container.innerHTML = ''; |
|
134 | 143 | const replToggle = document.getElementById('repl-toggle'); |
135 | 144 |
|
136 | 145 | if (replPanel && replToggle) { |
| 146 | + const replLangSelect = document.getElementById('repl-lang'); |
| 147 | + if (replLangSelect && [...replLangSelect.options].some(option => option.value === pageLocale)) { |
| 148 | + replLangSelect.value = pageLocale; |
| 149 | + } |
| 150 | + |
137 | 151 | /* Show panel on toggle click. */ |
138 | 152 | replToggle.addEventListener('click', () => { |
139 | 153 | const open = !replPanel.hidden; |
|
162 | 176 | /* Run button — compile and execute via Pyodide ProgramExecutor. */ |
163 | 177 | document.getElementById('repl-run-btn').addEventListener('click', async () => { |
164 | 178 | const src = document.getElementById('repl-input').value.trim(); |
165 | | - const lang = document.getElementById('repl-lang').value; |
| 179 | + const lang = replLangSelect.value; |
166 | 180 | const output = document.getElementById('repl-output'); |
167 | 181 | if (!src) return; |
168 | 182 |
|
@@ -204,7 +218,7 @@ _repl_errs = '\\n'.join(_r.errors) if _r.errors else '' |
204 | 218 | } |
205 | 219 |
|
206 | 220 | const src = document.getElementById('repl-input').value.trim(); |
207 | | - const lang = document.getElementById('repl-lang').value; |
| 221 | + const lang = replLangSelect.value; |
208 | 222 |
|
209 | 223 | output.innerHTML = '<span class="repl-output-placeholder">Generating WAT…</span>'; |
210 | 224 | output.dataset.mode = 'wat'; |
@@ -281,7 +295,7 @@ except Exception as _e: |
281 | 295 |
|
282 | 296 | runBtn.addEventListener('click', async () => { |
283 | 297 | const src = code.textContent.trim(); |
284 | | - const lang = pre.dataset.lang || 'en'; |
| 298 | + const lang = resolveExecutionLanguage(pre, code); |
285 | 299 | runBtn.textContent = '…'; |
286 | 300 | runBtn.disabled = true; |
287 | 301 | outputPanel.hidden = false; |
@@ -330,7 +344,7 @@ _block_errs = '\\n'.join(_r.errors) if _r.errors else '' |
330 | 344 | outputPanel.innerHTML = '<span class="output-running">Generating WAT…</span>'; |
331 | 345 |
|
332 | 346 | const src = code.textContent.trim(); |
333 | | - const lang = pre.dataset.lang || 'en'; |
| 347 | + const lang = resolveExecutionLanguage(pre, code); |
334 | 348 |
|
335 | 349 | try { |
336 | 350 | await ensureReplPyodide(); |
|
0 commit comments