From 19cb492116b36844bcbb930bea0a6a750f431c8a Mon Sep 17 00:00:00 2001 From: Ben Wheeler Date: Thu, 20 Aug 2020 14:23:43 -0400 Subject: [PATCH] localize initial options, set analytics prefs every time --- src/entry/index.js | 27 +++++++++++++++++++-------- src/utils/InitialOptions.js | 30 ++++++++---------------------- src/utils/Localization.js | 12 ++++++++++++ 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/entry/index.js b/src/entry/index.js index b1e9480..0b4ec84 100644 --- a/src/entry/index.js +++ b/src/entry/index.js @@ -98,11 +98,24 @@ function indexHideSplash () { } } +// set analytics prefs for all initial options, whether set in this session +// or before +function indexSetAnalyticsPrefs () { + var prefs = InitialOptions.getCurrentVals(); + if (!prefs) return; + Object.keys(prefs).map(function (key) { + OS.setAnalyticsPref(key, prefs[key]); + }); +} + function indexLoadStart () { indexHideSplash(); showLogo(); gn('gettings').className = 'gettings show'; gn('startcode').className = 'startcode show'; + + indexSetAnalyticsPrefs(); + document.ontouchmove = function (e) { e.preventDefault(); }; @@ -174,9 +187,7 @@ function indexSetPlace (e) { } // Send one-time analytics event about usage OS.analyticsEvent('lobby', 'scratchjr_usage', usageText); - InitialOptions.setValue('place', usageText); - // we use 'place_preference' for this particular Firebase pref - OS.setAnalyticsPref('place_preference', usageText); + InitialOptions.setValue('place_preference', usageText); ScratchAudio.sndFX('tap.wav'); indexHidePlaceQuestion(); indexAskRemainingQuestions(); @@ -217,13 +228,13 @@ function indexShowQuestion (key) { indexHideSplash(); hideLogo(); var optionType = InitialOptions.optionTypeForKey(key); - if (optionType === 'place') { + if (optionType === 'place_preference') { indexAskPlace(); } else { // custom question var options = InitialOptions.optionsForKey(key); // if we could not find any options, choose 'n/a' if (!options || !options.length) { - indexSelectOption(key, 'n/a'); + indexSelectOption(key, 'none'); return; } // if there's only one option, don't bother asking, just choose it! @@ -232,7 +243,7 @@ function indexShowQuestion (key) { return; } // if we got here, there is more than one option... - var instructionText = InitialOptions.instructionForKey(key); + var instructionText = Localization.localizeOptional(InitialOptions.instructionForKey(key)); var instructionElem = document.getElementById('optionsInstruction'); instructionElem.appendChild(document.createTextNode(instructionText)); gn('optionsInstruction').className = 'optionsInstruction show'; @@ -256,7 +267,8 @@ function indexShowQuestion (key) { break; case 'text': default: - optionElem.appendChild(document.createTextNode(option)); + var translatedOption = Localization.localizeOptional(option); + optionElem.appendChild(document.createTextNode(translatedOption)); break; } optionNum = optionNum + 1; @@ -268,7 +280,6 @@ function indexShowQuestion (key) { // store user selection, and show next question function indexSelectOption (key, val) { InitialOptions.setValue(key, val); - OS.setAnalyticsPref(key, val); ScratchAudio.sndFX('tap.wav'); // clear out old options instruction diff --git a/src/utils/InitialOptions.js b/src/utils/InitialOptions.js index c24e875..862632b 100644 --- a/src/utils/InitialOptions.js +++ b/src/utils/InitialOptions.js @@ -79,15 +79,7 @@ export default class InitialOptions { if (!settingsSection) return; settingsSection.forEach(function (question) { // question is like {key: OPTION_NAME, options: [...]} - if (question.firstTime && !question.everyTime) { - InitialOptions.initKeyFromCookie(question.key); - } - }); - settingsSection.forEach(function (question) { - // question is like {key: OPTION_NAME, options: [...]} - if (question.everyTime) { - InitialOptions.initKeyFromCookie(question.key); - } + InitialOptions.initKeyFromCookie(question.key); }); } @@ -147,19 +139,6 @@ export default class InitialOptions { return true; } - /** - * Gets array of keys of all questions that still need to be answered - * in this app session - */ - // static unansweredQuestions () { - // if (!settingsSection || !settingsSection.length) return []; - // return settingsSection.filter(function (question) { - // return !InitialOptions.isAnswered(question); - // }).map(function (question) { - // return question.key; - // }); - // } - /** * Gets next question that needs to be asked */ @@ -172,6 +151,13 @@ export default class InitialOptions { return null; } + /** + * Returns the object of current values + */ + static getCurrentVals () { + return currentVals; + } + /** * Set an options value in both cookie, and local object. * @param {string} key indicates which options question this value is for diff --git a/src/utils/Localization.js b/src/utils/Localization.js index a11b00c..6e95b81 100644 --- a/src/utils/Localization.js +++ b/src/utils/Localization.js @@ -112,6 +112,18 @@ export default class Localization { return 'String missing: ' + key; } + // Translate a particular message given the message key and info; + // if key not found, assume it's just a raw text string without a translation, + // and return that + static localizeOptional (keyOrRawText, formatting) { + var message; + if (keyOrRawText in localizationMessages) { + message = new window.IntlMessageFormat(localizationMessages[keyOrRawText], currentLocale); + return message.format(formatting); + } + return keyOrRawText; + } + // For sample projects, some fields (sprite names, text on stage, and text in say blocks) // may have a special prefix to indicate that it should be replaced with a localized value. // E.g., we might have some text on the stage that says "Touch me" in English. This gets translated.