From 42f564883f1de201816c30dff40294a88617286a Mon Sep 17 00:00:00 2001 From: zepumph Date: Mon, 7 Oct 2019 14:14:09 -0800 Subject: [PATCH] move phet.chipper.strings.get to initialize-globals, renaming it; delete chipper-strings.js; mapString doesn't need stringTest parameter, https://github.com/phetsims/chipper/issues/796 --- js/grunt/buildRunnable.js | 3 --- js/initialize-globals.js | 43 +++++++++++++++++++++++++++++++--- js/requirejs-plugins/string.js | 9 ++----- templates/chipper-strings.js | 37 ----------------------------- 4 files changed, 42 insertions(+), 50 deletions(-) delete mode 100644 templates/chipper-strings.js diff --git a/js/grunt/buildRunnable.js b/js/grunt/buildRunnable.js index f67539396..2b281a525 100644 --- a/js/grunt/buildRunnable.js +++ b/js/grunt/buildRunnable.js @@ -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, diff --git a/js/initialize-globals.js b/js/initialize-globals.js index b2906cd96..2a16ec23c 100644 --- a/js/initialize-globals.js +++ b/js/initialize-globals.js @@ -612,16 +612,21 @@ */ 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' : @@ -629,7 +634,7 @@ stringTest === 'xss' ? string + '' : 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; }; @@ -637,6 +642,38 @@ 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 diff --git a/js/requirejs-plugins/string.js b/js/requirejs-plugins/string.js index f77cc2e1a..b5b5b8f56 100644 --- a/js/requirejs-plugins/string.js +++ b/js/requirejs-plugins/string.js @@ -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) @@ -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 { @@ -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` ); } }; } ); diff --git a/templates/chipper-strings.js b/templates/chipper-strings.js deleted file mode 100644 index 2be58b2a1..000000000 --- a/templates/chipper-strings.js +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2018, University of Colorado Boulder - -( function() { - 'use strict'; - - assert && assert( !!phet.chipper.queryParameters, 'Query parameters are required for custom strings or string tests' ); - assert && assert( !!phet.chipper.strings, 'The initialization script should have filled out phet.chipper.strings' ); - assert && assert( !!phet.chipper.locale, 'locale is required to look up the correct strings' ); - assert && assert( !!phet.chipper.mapString, 'mapString is needed for string test usage' ); - - const stringOverrides = JSON.parse( phet.chipper.queryParameters.strings || '{}' ); - - const stringTest = ( typeof window !== 'undefined' && phet.chipper.queryParameters.stringTest ) ? - phet.chipper.queryParameters.stringTest : - null; - - phet.chipper.strings.get = key => { - - // 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 ], stringTest ); - }; -} )(); \ No newline at end of file