BiStringBundle

This class is used to make creating multilingual applications easier. The string bundle is uses one hash table (objects in JavaScript) for every language and falls back on English ("en") in case the user language is not available.

This class extends BiEventTarget and therefore all methods and fields available for BiEventTarget are also available for BiStringBundle .

Constructor

new BiStringBundle ( )

Parameters

No parameters.

Properties

NameTypegetsetDescripton
bundlesObject[]checkedReturns the added bundles as an array
languageStringcheckedcheckedThe current language
languagesString[]checkedReturns the languages supported by the string bundle
majorLanguageStringcheckedThe current major language. For example if the current language is "en-us" then this returns "en".
userLanguageStringcheckedThe language of the user's operating environment

Methods

NameDescription
addBundleAdds a bundle and associates that with a certain language code
appendBundleThis adds key value pairs to an existing bundle. This allows you to modify an existing bundle with more strings
getBundleReturns the bundle (hash table) for the given language code. If fails an empty bundle will be returned.
getBundleForLanguageReturns the bundle (hash table) for the given language code. This will return the best match. If you pass "zh-CN" it will try to find the bundles in the following order; "zh-CN", "zh", language, majorLanguage, "en" and if all these fails an empty bundle (object) will be returned.
getFormattedStringReturns the string for the given string id. The string is then formatted so that %1 to %n are replaced by the extra arguments to this method. This method does not allow you to specify the language. If that is needed use getString followed by formatString.
getStringReturns the string for the given string id. The second argument is the language code to get the string for. If left out then the current language for the string bundle is used.
getStringKeysReturns the keys used to identify the strings in the bundles
removeBundleRemoves the string bundle with the given language code

Events

NameTypeBubblesDescripton
changeBiEventThis event is fired when the current strings changes in some way. This happens when the language is changed or a new bundle is added with the current language.

Static Methods

NameDescription
formatStringTakes a string containing %1 to %n and 0 to n extra arguments that will replace the %n strings.

Static Fields

None.

Remarks

The string bundles (that are added using addBundle should be plain JavaScript objects (hash tables) with the following form: var enBundle = { languageCode: "en", languageName: "English", selectLanguage: "Select Language:", checkingForIsLoggedIn: "Checking if already logged in...", windowCaption: "Download the Bindowsu2122 Framework" }; The languageCode and languageName fields are not special in any way. It is just a good practice to include them and they are often useful to the application. Typical usage looks something like this: var stringBundle = new BiStringBundle; stringBundle.addBundle("en", enBundle); ... someLabel.setText(stringBundle.getString("someLabelText"));

Method Details

addBundle

Adds a bundle and associates that with a certain language code

Syntax

object.addBundle ( sLanguage,oStringMap )

Parameters

NameTypeOptionalDefaultDescripton
sLanguageStringThe language code for the string bundle to add.
oStringMapObjectThe hash table used to map string identifiers to strings.

Return Type

void

appendBundle

This adds key value pairs to an existing bundle. This allows you to modify an existing bundle with more strings

Syntax

object.appendBundle ( sLanguage,oStringMap )

Parameters

NameTypeOptionalDefaultDescripton
sLanguageStringThe language code for the string bundle to append.
oStringMapObjectThe hash table used to map string identifiers to strings.

Return Type

void

getBundle

Returns the bundle (hash table) for the given language code. If fails an empty bundle will be returned.

Syntax

object.getBundle ( [ sLanguage ] )

Parameters

NameTypeOptionalDefaultDescripton
sLanguageStringcheckedThe language code of the string bundle to return. If left out the default language is used.

Return Type

Object

getBundleForLanguage

Returns the bundle (hash table) for the given language code. This will return the best match. If you pass "zh-CN" it will try to find the bundles in the following order; "zh-CN", "zh", language, majorLanguage, "en" and if all these fails an empty bundle (object) will be returned.

Syntax

object.getBundleForLanguage ( [ sLanguage ] )

Parameters

NameTypeOptionalDefaultDescripton
sLanguageStringcheckedThe language code of the string bundle to return. If left out the default language is used.

Return Type

Object

getFormattedString

Returns the string for the given string id. The string is then formatted so that %1 to %n are replaced by the extra arguments to this method. This method does not allow you to specify the language. If that is needed use getString followed by formatString.

Syntax

object.getFormattedString ( sStringId [ ,args ] )

Parameters

NameTypeOptionalDefaultDescripton
sStringIdStringThe id for the string to retreive
argsStringcheckedOptional arguments (as many as you wish) that will replace the '%n' in the pattern

Return Type

String

getString

Returns the string for the given string id. The second argument is the language code to get the string for. If left out then the current language for the string bundle is used.

Syntax

object.getString ( sStringId [ ,sLanguage ] )

Parameters

NameTypeOptionalDefaultDescripton
sStringIdStringThe id for the string to retreive
sLanguageStringcheckedThe language code of the string to return

Return Type

String

getStringKeys

Returns the keys used to identify the strings in the bundles

Syntax

object.getStringKeys ( [ sLanguage ] )

Parameters

NameTypeOptionalDefaultDescripton
sLanguageStringcheckedThe language code to use when getting the strings in the bundle. If left out the current language is is used

Return Type

String[]

removeBundle

Removes the string bundle with the given language code

Syntax

object.removeBundle ( sLanguage )

Parameters

NameTypeOptionalDefaultDescripton
sLanguageStringThe language code of the string bundle to remove

Return Type

void

Static Method Details

formatString

Takes a string containing %1 to %n and 0 to n extra arguments that will replace the %n strings.

Syntax

BiStringBundle . formatString ( sPattern [ ,args ] )

Parameters

NameTypeOptionalDefaultDescripton
sPatternStringA string containing '%n' where '%n' will be replaced by the remaining arguments.
argsStringcheckedOptional arguments (as many as you wish) that will replace the '%n' in the pattern

Return Type

String