Skip to content

Commit

Permalink
formatStringValues implementation to use forEachString, bug fix in fo…
Browse files Browse the repository at this point in the history
…rEachString to ignore arrays, phetsims/rosetta#193
  • Loading branch information
zepumph committed Sep 23, 2019
1 parent 79429ef commit 2291324
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
33 changes: 13 additions & 20 deletions js/common/ChipperStringUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,29 +118,17 @@

/**
* Recurse through a string file and format each string value appropriately
* @param {StringMapNode} stringMapNode
* @param {StringMap} stringMap
* @param {boolean} isRTL - is right to left language
* @public
*/
formatStringValues: function( stringMapNode, isRTL ) {
for ( const stringKey in stringMapNode ) {
if ( stringMapNode.hasOwnProperty( stringKey ) ) {

// This will either have a "value" key, or be an object with keys that will eventually have 'value' in it
const element = stringMapNode[ stringKey ];
if ( element.hasOwnProperty( 'value' ) ) {

// remove leading/trailing whitespace, see chipper#619. Do this before addDirectionalFormatting
element.value = element.value.trim();
element.value = ChipperStringUtils.addDirectionalFormatting( element.value, isRTL );
}
else {

// Recurse a level deeper
ChipperStringUtils.formatStringValues( element, isRTL );
}
}
}
formatStringValues: function( stringMap, isRTL ) {
ChipperStringUtils.forEachString( stringMap, ( key, stringObject ) => {

// remove leading/trailing whitespace, see chipper#619. Do this before addDirectionalFormatting
stringObject.value = stringObject.value.trim();
stringObject.value = ChipperStringUtils.addDirectionalFormatting( stringObject.value, isRTL );
} );
},

/**
Expand Down Expand Up @@ -196,6 +184,11 @@
if ( map.hasOwnProperty( key ) ) {
const nextKey = keySoFar ? `${keySoFar}.${key}` : key; // don't start with period, assumes '' is falsey
const stringObject = map[ key ];

// no need to support arrays in the string map, for example stringObject.history in locale specific files.
if ( Array.isArray( stringObject ) ) {
return;
}
if ( stringObject.value ) {
func( nextKey, stringObject );
}
Expand Down
6 changes: 6 additions & 0 deletions js/requirejs-plugins/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ define( require => {

// Cache miss: load the file parse, enter into cache and return it
text.get( url, loadedText => {

// the cache didn't exist before the text load, but it may exist now.
if ( cache[ url ] ) {
return callback( cache[ url ] );
}

const parsed = JSON.parse( loadedText );

const isRTL = localeInfo[ phet.chipper.queryParameters.locale ].direction === 'rtl';
Expand Down

0 comments on commit 2291324

Please sign in to comment.