Skip to content

LocalizedString

Overview

Sets up a system of Properties to handle translation fallback and phet-io support for a single translated string.

@author Jonathan Olson <jonathan.olson>

Class LocalizedString

import { LocalizedString } from 'scenerystack/chipper';

Constructor

new LocalizedString( stringKey : string, localeToTranslationMap : LocalizedStringStateDelta, tandem : Tandem, metadata? : Record<string, unknown> )

Instance Methods

getStateDelta() : LocalizedStringStateDelta

Returns an object that shows the changes of strings from their initial values. This includes whether strings are marked as "overridden"

setStateDelta( state : LocalizedStringStateDelta )

Take a state from getStateDelta, and apply it.

getLocaleSpecificProperty( locale : Locale ) : TProperty<string>

Returns the locale-specific Property for any locale (lazily creating it if necessary)

dispose()

restoreInitialValue( locale : Locale )

Reset to the initial value for the specified locale, used for testing.

Instance Properties

property : LocalizedStringProperty

(readonly)

Public-facing IProperty<string>, used by string modules

Static Methods

getLocaleFallbacks( locale : Locale ) : Locale[]

getNestedStringProperties( stringData : StringData ) : ToStringPropertyMap<StringData>

Computes a nested map of LocalizedStringProperties from a nested input structure of strings. The string Properties will change values based on the current locale (stored in localeProperty).

For instance, if we have an example StringMap:

const stringMap = { en: { a: 'a', nest: { x: 'x', y: 'y' } }, hi: { 'a': 'ए', 'b': 'ब', nest: { x: 'एक्स', } } }

Then the resulting LocalizedString.getNestedStringProperties( stringMap ) would have the structure:

{ aStringProperty: LocalizedStringProperty, bStringProperty: LocalizedStringProperty, nest: { xStringProperty: LocalizedStringProperty, yStringProperty: LocalizedStringProperty } }

It is recommended to put each translation file into a separate JSON file, and import them into a single file that will be passed to this function.

@param stringData

Type LocalizedStringStateDelta

import type { LocalizedStringStateDelta } from 'scenerystack/chipper';

Partial<Record<Locale, TranslationString>>

Type StringsStateStateObject

Where "string" is a phetioID

import type { StringsStateStateObject } from 'scenerystack/chipper';

Source Code

See the source for LocalizedString.ts in the chipper repository.