Skip to content

Commit

Permalink
update secondLocale initialization, clean up a few bugs and remove TO…
Browse files Browse the repository at this point in the history
…DO, #19
  • Loading branch information
zepumph committed Jan 14, 2023
1 parent 4f7c4cd commit a3b14a2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
7 changes: 3 additions & 4 deletions js/common/NumberSuiteCommonConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ const NUMBER_TO_STRING_KEY_PRIMARY: Record<number, LinkableProperty<string>> = {
20: NumberSuiteCommonStrings.twentyStringProperty
};

// TODO: Move strings from number-play to number-suite-common so we use NSC prefix here instead https://github.com/phetsims/number-suite-common/issues/23
const NUMBER_PLAY_STRING_KEY_PREFIX = 'NUMBER_PLAY/';
const NUMBER_SUITE_COMMON_STRING_KEY_PREFIX = 'NUMBER_SUITE_COMMON/';

const NumberSuiteCommonConstants = {

Expand Down Expand Up @@ -93,13 +92,13 @@ const NumberSuiteCommonConstants = {
*/
numberToWord: ( numberPlaySecondaryStrings: SecondLocaleStrings, number: number, isPrimaryLocale: boolean ): string => {
const string = isPrimaryLocale ? NUMBER_TO_STRING_KEY_PRIMARY[ number ].value :
numberPlaySecondaryStrings[ `${NUMBER_PLAY_STRING_KEY_PREFIX}${NUMBER_TO_STRING_KEY_SECONDARY[ number ]}` ];
numberPlaySecondaryStrings[ `${NUMBER_SUITE_COMMON_STRING_KEY_PREFIX}${NUMBER_TO_STRING_KEY_SECONDARY[ number ]}` ];
assert && assert( string, `no stringKey found for number=${number}` );

return string;
},

NUMBER_PLAY_STRING_KEY_PREFIX: NUMBER_PLAY_STRING_KEY_PREFIX,
NUMBER_SUITE_COMMON_STRING_KEY_PREFIX: NUMBER_SUITE_COMMON_STRING_KEY_PREFIX,

UNGROUPED_STORED_COUNTING_OBJECT_SCALE: 0.9,
GROUPED_STORED_COUNTING_OBJECT_SCALE: 0.7,
Expand Down
9 changes: 7 additions & 2 deletions js/common/NumberSuiteCommonQueryParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ const NumberSuiteCommonQueryParameters = QueryStringMachine.getAll( {
},

// specifies a second locale to make available on the 'Ten', 'Twenty', and 'Compare' screens. Values are specified
// with a locale code, e.g. 'en'.
// with a locale code, e.g. "en" or "zh_CN".
secondLocale: {
public: true,
type: 'string',
defaultValue: phet.chipper.locale
isValidValue: locale => locale === null || // default value
( !!locale && phet.chipper.strings.hasOwnProperty( locale ) &&

// This part is valuable if you tried this query parameter on the _en.html version
Object.keys( phet.chipper.strings ).length > 1 ),
defaultValue: null
},

// whether the paper ones are visible on the 'Lab' screen
Expand Down
23 changes: 9 additions & 14 deletions js/common/model/NumberSuiteCommonPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,36 @@ import NumberSuiteCommonQueryParameters from '../NumberSuiteCommonQueryParameter
import Property from '../../../../axon/js/Property.js';
import DerivedProperty from '../../../../axon/js/DerivedProperty.js';
import TReadOnlyProperty from '../../../../axon/js/TReadOnlyProperty.js';
import { Locale } from '../../../../joist/js/i18n/localeProperty.js';
import localeProperty, { availableRuntimeLocales, Locale } from '../../../../joist/js/i18n/localeProperty.js';

// TODO: type string map, perhaps getStringModule.TStringModule? https://github.com/phetsims/number-suite-common/issues/18
//TODO https://github.com/phetsims/number-suite-common/issues/18 replace any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type SecondLocaleStrings = any;

// preference Properties directly controlled by UI
class NumberSuiteCommonPreferences {

// preference Properties directly controlled by UI
// if a valid second locale was provided via a query parameter, display the second locale on sim startup
public readonly showSecondLocaleProperty: Property<boolean>;
public readonly secondLocaleProperty: Property<Locale>;
public readonly showLabOnesProperty: Property<boolean>;
public readonly readAloudProperty: Property<boolean>;

// True when the second locale can be turned on, because there are sufficient locales in the runtime.
public static readonly SECOND_LOCALE_SELECTION_AVAILABLE = availableRuntimeLocales.length > 1;

// helper Properties derived from preference Properties
public readonly secondLocaleStringsProperty: TReadOnlyProperty<SecondLocaleStrings>;

public constructor() {
this.readAloudProperty = new BooleanProperty( NumberSuiteCommonQueryParameters.readAloud );

const isSecondLocaleProvided = QueryStringMachine.containsKey( 'secondLocale' );
const isSecondLocaleValid = !!phet.chipper.strings[ NumberSuiteCommonQueryParameters.secondLocale! ] &&
Object.keys( phet.chipper.strings ).length > 1;

if ( isSecondLocaleProvided && !isSecondLocaleValid ) {
QueryStringMachine.addWarning( 'secondLocale', NumberSuiteCommonQueryParameters.secondLocale,
`Second locale doesn't exist: ${NumberSuiteCommonQueryParameters.secondLocale}` );
NumberSuiteCommonQueryParameters.secondLocale = phet.chipper.locale;
}
this.showSecondLocaleProperty = new BooleanProperty( !!NumberSuiteCommonQueryParameters.secondLocale );

this.showSecondLocaleProperty = new BooleanProperty( isSecondLocaleProvided && isSecondLocaleValid );

this.secondLocaleProperty = new Property<Locale>( NumberSuiteCommonQueryParameters.secondLocale! as Locale );
this.secondLocaleProperty = new Property<Locale>( NumberSuiteCommonQueryParameters.secondLocale as Locale || localeProperty.value, {
validValues: availableRuntimeLocales
} );

this.showLabOnesProperty = new BooleanProperty( NumberSuiteCommonQueryParameters.showLabOnes );

Expand Down
4 changes: 2 additions & 2 deletions js/common/view/NumberSuiteCommonPreferencesNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export default abstract class NumberSuiteCommonPreferencesNode<T extends NumberS
} );

// disable the second locale toggle and show the all_html link if there's only one locale available
if ( Object.keys( phet.chipper.strings ).length < 2 ) {
showSecondLocaleToggleSwitch.enabled = false;
if ( !NumberSuiteCommonPreferences.SECOND_LOCALE_SELECTION_AVAILABLE ) {
showSecondLocaleControl.enabled = false;
loadAllHtmlText.visible = true;
}

Expand Down

0 comments on commit a3b14a2

Please sign in to comment.