localize initial options, set analytics prefs every time

This commit is contained in:
Ben Wheeler 2020-08-20 14:23:43 -04:00
parent 64019e6fd8
commit 19cb492116
3 changed files with 39 additions and 30 deletions

View file

@ -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 () { function indexLoadStart () {
indexHideSplash(); indexHideSplash();
showLogo(); showLogo();
gn('gettings').className = 'gettings show'; gn('gettings').className = 'gettings show';
gn('startcode').className = 'startcode show'; gn('startcode').className = 'startcode show';
indexSetAnalyticsPrefs();
document.ontouchmove = function (e) { document.ontouchmove = function (e) {
e.preventDefault(); e.preventDefault();
}; };
@ -174,9 +187,7 @@ function indexSetPlace (e) {
} }
// Send one-time analytics event about usage // Send one-time analytics event about usage
OS.analyticsEvent('lobby', 'scratchjr_usage', usageText); OS.analyticsEvent('lobby', 'scratchjr_usage', usageText);
InitialOptions.setValue('place', usageText); InitialOptions.setValue('place_preference', usageText);
// we use 'place_preference' for this particular Firebase pref
OS.setAnalyticsPref('place_preference', usageText);
ScratchAudio.sndFX('tap.wav'); ScratchAudio.sndFX('tap.wav');
indexHidePlaceQuestion(); indexHidePlaceQuestion();
indexAskRemainingQuestions(); indexAskRemainingQuestions();
@ -217,13 +228,13 @@ function indexShowQuestion (key) {
indexHideSplash(); indexHideSplash();
hideLogo(); hideLogo();
var optionType = InitialOptions.optionTypeForKey(key); var optionType = InitialOptions.optionTypeForKey(key);
if (optionType === 'place') { if (optionType === 'place_preference') {
indexAskPlace(); indexAskPlace();
} else { // custom question } else { // custom question
var options = InitialOptions.optionsForKey(key); var options = InitialOptions.optionsForKey(key);
// if we could not find any options, choose 'n/a' // if we could not find any options, choose 'n/a'
if (!options || !options.length) { if (!options || !options.length) {
indexSelectOption(key, 'n/a'); indexSelectOption(key, 'none');
return; return;
} }
// if there's only one option, don't bother asking, just choose it! // if there's only one option, don't bother asking, just choose it!
@ -232,7 +243,7 @@ function indexShowQuestion (key) {
return; return;
} }
// if we got here, there is more than one option... // 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'); var instructionElem = document.getElementById('optionsInstruction');
instructionElem.appendChild(document.createTextNode(instructionText)); instructionElem.appendChild(document.createTextNode(instructionText));
gn('optionsInstruction').className = 'optionsInstruction show'; gn('optionsInstruction').className = 'optionsInstruction show';
@ -256,7 +267,8 @@ function indexShowQuestion (key) {
break; break;
case 'text': case 'text':
default: default:
optionElem.appendChild(document.createTextNode(option)); var translatedOption = Localization.localizeOptional(option);
optionElem.appendChild(document.createTextNode(translatedOption));
break; break;
} }
optionNum = optionNum + 1; optionNum = optionNum + 1;
@ -268,7 +280,6 @@ function indexShowQuestion (key) {
// store user selection, and show next question // store user selection, and show next question
function indexSelectOption (key, val) { function indexSelectOption (key, val) {
InitialOptions.setValue(key, val); InitialOptions.setValue(key, val);
OS.setAnalyticsPref(key, val);
ScratchAudio.sndFX('tap.wav'); ScratchAudio.sndFX('tap.wav');
// clear out old options instruction // clear out old options instruction

View file

@ -79,15 +79,7 @@ export default class InitialOptions {
if (!settingsSection) return; if (!settingsSection) return;
settingsSection.forEach(function (question) { settingsSection.forEach(function (question) {
// question is like {key: OPTION_NAME, options: [...]} // question is like {key: OPTION_NAME, options: [...]}
if (question.firstTime && !question.everyTime) { InitialOptions.initKeyFromCookie(question.key);
InitialOptions.initKeyFromCookie(question.key);
}
});
settingsSection.forEach(function (question) {
// question is like {key: OPTION_NAME, options: [...]}
if (question.everyTime) {
InitialOptions.initKeyFromCookie(question.key);
}
}); });
} }
@ -147,19 +139,6 @@ export default class InitialOptions {
return true; 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 * Gets next question that needs to be asked
*/ */
@ -172,6 +151,13 @@ export default class InitialOptions {
return null; return null;
} }
/**
* Returns the object of current values
*/
static getCurrentVals () {
return currentVals;
}
/** /**
* Set an options value in both cookie, and local object. * Set an options value in both cookie, and local object.
* @param {string} key indicates which options question this value is for * @param {string} key indicates which options question this value is for

View file

@ -112,6 +112,18 @@ export default class Localization {
return 'String missing: ' + key; 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) // 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. // 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. // E.g., we might have some text on the stage that says "Touch me" in English. This gets translated.