Skip to content

Commit

Permalink
move phet.chipper.strings.get to initialize-globals, renaming it; del…
Browse files Browse the repository at this point in the history
…ete chipper-strings.js; mapString doesn't need stringTest parameter, #796
  • Loading branch information
zepumph committed Oct 7, 2019
1 parent 55bac8e commit 42f5648
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 50 deletions.
3 changes: 0 additions & 3 deletions js/grunt/buildRunnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,6 @@ module.exports = async function( repo, minifyOptions, instrument, allHTML, brand
// Preloads
...getPreloads( repo, brand, true ).map( filename => grunt.file.read( filename ) ),

// Strings code (requires preloads first, and should be done before requireJS)
grunt.file.read( '../chipper/templates/chipper-strings.js' ),

// Our main require.js content, wrapped in a function called in the startup below
requireJS,

Expand Down
43 changes: 40 additions & 3 deletions js/initialize-globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,31 +612,68 @@
*/
window.phet.chipper.brand = window.phet.chipper.brand || phet.chipper.queryParameters.brand || 'adapted-from-phet';

// {string|null} - See documentation of stringTest query parameter - we need to support this during build, where
// there aren't any query parameters.
const stringTest = ( typeof window !== 'undefined' && phet.chipper.queryParameters.stringTest ) ?
phet.chipper.queryParameters.stringTest :
null;

/**
* Maps an input string to a final string, accommodating tricks like doubleStrings.
* This function is used to modify all strings in a sim when the stringTest query parameter is used.
* The stringTest query parameter and its options are documented in the query parameter docs above.
* It is used in string.js and sim.html.
* @param string - the string to be mapped
* @param stringTest - the value of the stringTest query parameter
* @returns {string}
*/
window.phet.chipper.mapString = function( string, stringTest ) {
window.phet.chipper.mapString = function( string ) {
return stringTest === null ? string :
stringTest === 'double' ? string + ':' + string :
stringTest === 'long' ? '12345678901234567890123456789012345678901234567890' :
stringTest === 'rtl' ? '\u202b\u062a\u0633\u062a (\u0632\u0628\u0627\u0646)\u202c' :
stringTest === 'xss' ? string + '<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQIW2NkYGD4DwABCQEBtxmN7wAAAABJRU5ErkJggg==" onload="window.location.href=atob(\'aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1kUXc0dzlXZ1hjUQ==\')" />' :
stringTest === 'none' ? string :

//In the fallback case, supply whatever string was given in the query parameter value
// In the fallback case, supply whatever string was given in the query parameter value
stringTest;
};

// If locale was provided as a query parameter, then change the locale used by Google Analytics.
if ( QueryStringMachine.containsKey( 'locale' ) ) {
window.phet.chipper.locale = phet.chipper.queryParameters.locale;
}

const stringOverrides = JSON.parse( phet.chipper.queryParameters.strings || '{}' );

/**
* Get a string given the key. This implementation is meant for use only in the build sim. For more info see the
* string plugin.
* @param {string} key
* @returns {string}
*/
phet.chipper.getStringForBuiltSim = key => {
assert && assert( !!phet.chipper.isProduction, 'expected to be running a built sim' );
assert && assert( !!phet.chipper.strings, 'phet.chipper.strings should be filled out by initialization script' );
assert && assert( !!phet.chipper.locale, 'locale is required to look up the correct strings' );

// override strings via the 'strings' query parameter
if ( stringOverrides[ key ] ) {
return stringOverrides[ key ];
}
let stringMap = phet.chipper.strings[ phet.chipper.locale ];

// Don't fail out on unsupported locales, see https://github.com/phetsims/chipper/issues/694
if ( !stringMap ) {

// See if there's a translation for just the language code
stringMap = phet.chipper.strings[ phet.chipper.locale.slice( 0, 2 ) ];

if ( !stringMap ) {
stringMap = phet.chipper.strings.en;
}
}
return phet.chipper.mapString( stringMap[ key ] );
};
}() );

/** Create a random seed in the preload code that can be used to make sure playback simulations use the same seed
Expand Down
9 changes: 2 additions & 7 deletions js/requirejs-plugins/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ define( require => {
// back for that url. This way there aren't many `text.get` calls kicked off before the first can come back with text.
const callbacksFromUnloadedURLs = {};

// {string|null} - See documentation of stringTest query parameter in initialize-globals.js
const stringTest = ( typeof window !== 'undefined' && window.phet.chipper.queryParameters.stringTest ) ?
window.phet.chipper.queryParameters.stringTest :
null;

/**
* When running in requirejs mode, check to see if we have already loaded the specified file
* Also parses it so that only happens once per file (instead of once per string key)
Expand Down Expand Up @@ -91,7 +86,7 @@ define( require => {
if ( stringFromMap === null ) {
throw new Error( `String not found: ${key}` );
}
return window.phet.chipper.mapString( stringFromMap, stringTest );
return window.phet.chipper.mapString( stringFromMap );
};

return {
Expand Down Expand Up @@ -238,7 +233,7 @@ define( require => {
* is useful for text transform plugins, like a CoffeeScript plugin.
*/
write: ( pluginName, moduleName, write ) => {
write( `define("${pluginName}!${moduleName}",function(){return window.phet.chipper.strings.get("${moduleName}");});\n` );
write( `define("${pluginName}!${moduleName}",function(){return window.phet.chipper.getStringForBuiltSim("${moduleName}");});\n` );
}
};
} );
37 changes: 0 additions & 37 deletions templates/chipper-strings.js

This file was deleted.

0 comments on commit 42f5648

Please sign in to comment.