@@ -21,13 +21,15 @@ let startTime = Date.now();
2121let BACKQUOTE = "`" ; // Make it easy to use `${BACKQUOTE}`
2222let DOLLAR = "$" ; // Make it easy to use `${DOLLAR}`
2323
24- // Current language
25- let lang ;
24+ // Current language. Guess English until we learn otherwise.
25+ let lang = "en" ;
2626
2727const resources = {
2828 en : {
2929 translation : {
3030 already_correct : 'The answer is already correct!' ,
31+ complete : 'COMPLETE!' ,
32+ completed : 'Completed' ,
3133 congrats : 'Congratulations! Your answer is correct!' ,
3234 congrats_all : 'Great work! All your answers are correct!' ,
3335 expecting : "We were expecting an answer like this:\n{0}" ,
@@ -36,12 +38,15 @@ const resources = {
3638 no_hints : 'Sorry, there are no hints for this lab.' ,
3739 no_matching_hint : 'Sorry, I cannot find a hint that matches your attempt.' ,
3840 reset_title : 'Reset initial state (throwing away current attempt).' ,
41+ to_be_completed : 'to be completed' ,
3942 try_harder : "Try harder! Don't give up so soon. Current time spent (in seconds): {0}" ,
4043 }
4144 } ,
4245 ja : {
4346 translation : {
4447 already_correct : '答えはすでに正しいです!' ,
48+ complete : '完了' ,
49+ completed : '完了しました' ,
4550 congrats : '「おめでとうございます!」あなたの答えは正解です!' ,
4651 congrats_all : '素晴らしい仕事でした!あなたの答えはすべて正解です!' ,
4752 expecting : "次のような答えを期待していました:\n{0}" ,
@@ -50,6 +55,7 @@ const resources = {
5055 no_hints : '申し訳ありませんが、このラボにはヒントがありません。' ,
5156 no_matching_hint : '申し訳ありませんが、あなたの試みに一致するヒントが見つかりません。' ,
5257 reset_title : '初期状態をリセットします (現在の試行を破棄します)。' ,
58+ to_be_completed : '完成する' ,
5359 try_harder : '「もっと頑張ってください! すぐに諦めないでください。現在の所要時間 (秒): {0}」' ,
5460 }
5561 } ,
@@ -87,6 +93,7 @@ function retrieve_t(obj, field) {
8793 return result ;
8894}
8995
96+ // Determine language of document. Set with <html lang="...">.
9097function determine_locale ( ) {
9198 let lang = document . documentElement . lang ;
9299 if ( ! lang ) {
@@ -409,10 +416,10 @@ function makeStamp() {
409416 let hash = cyrb64Hash ( resultBeginning ) ;
410417 let gave_up_indicator = '' ;
411418 if ( user_gave_up ) { // If user gave up first, subtly indicate this.
412- // The user could reload later this and cause this marking to be omitted.
419+ // The user could reload this and cause this marking to be omitted.
413420 gave_up_indicator = ' (GA)' ;
414421 }
415- return `Completed ${ resultBeginning } ${ hash } ${ gave_up_indicator } ` ;
422+ return `${ t ( 'completed' ) } ${ resultBeginning } ${ hash } ${ gave_up_indicator } ` ;
416423}
417424
418425/**
@@ -432,7 +439,7 @@ function runCheck() {
432439 } ;
433440 // isCorrect is now true only if everything matched
434441 let oldGrade = document . getElementById ( 'grade' ) . innerHTML ;
435- let newGrade = isCorrect ? 'COMPLETE!' : 'to be completed' ;
442+ let newGrade = isCorrect ? t ( 'complete' ) : t ( 'to_be_completed' ) ;
436443 document . getElementById ( 'grade' ) . innerHTML = newGrade ;
437444
438445 if ( isCorrect && ( oldGrade !== newGrade ) ) {
@@ -557,17 +564,14 @@ function processHints(requestedHints) {
557564 Array . from ( forbiddenFields ) . join ( ' ' ) ) ;
558565 }
559566
560- let newHint = { } ;
567+ let newHint = { ... hint } ; // Copy everything over, incl. translations
561568 newHint . index = hint . index ? Number ( hint . index ) : 0 ;
562- newHint . text = hint . text ;
563569 // Precompile all regular expressions & report any failures
564570 if ( hint . present ) {
565- newHint . present = hint . present ;
566571 newHint . presentRe = processRegex ( hint . present ,
567572 `hint[${ i } ].present` , false ) ;
568573 } ;
569574 if ( hint . absent ) {
570- newHint . absent = hint . absent ;
571575 newHint . absentRe = processRegex ( hint . absent ,
572576 `hint[${ i } ].absent` , false ) ;
573577 } ;
@@ -715,8 +719,9 @@ function runSelftest() {
715719 // values if the index > 0. We only return hints with the
716720 // given hint index.
717721 actualHint = findHint ( example , [ hint . index ] ) ;
718- if ( actualHint != hint . text ) {
719- alert ( `Lab Error: Unexpected hint!\n\nExample:\n${ example } \n\nExpected hint:\n${ hint . text } \n\nProduced hint:\n${ actualHint } \n\nExpected (passing example)=${ JSON . stringify ( expected ) } \n\ntestAttempt=${ JSON . stringify ( testAttempt ) } \nFailing hint=${ JSON . stringify ( hint ) } ` ) ;
722+ expectedHint = retrieve_t ( hint , 'text' ) ;
723+ if ( actualHint != expectedHint ) {
724+ alert ( `Lab Error: Unexpected hint!\n\nExample:\n${ example } \n\n\nExpected hint:\n${ expectedHint } \n\nActual hint:\n${ actualHint } \n\nExample:\n${ JSON . stringify ( example ) } \n\nFailing hint=${ JSON . stringify ( hint ) } ` ) ;
720725 } ;
721726 } ;
722727 } ;
@@ -795,15 +800,15 @@ function setupInfo() {
795800}
796801
797802function initPage ( ) {
803+ // Set current locale
804+ lang = determine_locale ( ) ;
805+
798806 // Use configuration info to set up all relevant global values.
799807 setupInfo ( ) ;
800808
801809 // Run a selftest on page load, to prevent later problems
802810 runSelftest ( ) ;
803811
804- // Set current locale
805- lang = determine_locale ( ) ;
806-
807812 // Set up user interaction for all attempts.
808813 let current = 0 ;
809814 while ( true ) {
0 commit comments