springroll.Languages Class

Keeps track of the user locale, by auto-detecting the browser language, allowing a user selection, and automatically modifying any url that runs through the CacheManager.

Constructor

springroll.Languages

(
  • config
)

Parameters:

  • config Object

    The language settings to be used.

    • default String

      The default language name to use if asked for one that is not present.

    • languages Array

      An array of all supported languages, with entries being locale ids (dialects allowed). Locale ids should be lower case.

    • [replace="%LANG%"] String optional

      A string to replace in urls with the current language.

destroy

()

Inherited from springroll.EventDispatcher but overwritten in destroy:264

Destroys the Languages object.

getFormattedString

(
  • key
  • args
)
String

Gets a formatted string from the current string table. See String.format() in the Core module.

Parameters:

  • key String

    The key of the string to get.

  • args Array |

    An array or list of arguments for formatting.

Returns:

String:

The translated string.

getPreferredLanguages

() Array

Gets the preferred languages from the browser.

Returns:

Array:

The list of preferred languages in order of preference.

getString

(
  • key
)
String

Defined in getString:224

Gets a string from the current string table.

Parameters:

  • key String

    The key of the string to get.

Returns:

String:

The translated string.

has

(
  • name
  • [callback]
)
Boolean

Checks if the EventDispatcher has a specific listener or any listener for a given event.

Parameters:

  • name String

    The name of the single event type to check for

  • [callback] Function optional

    The listener function to check for. If omitted, checks for any listener.

Returns:

Boolean:

If the EventDispatcher has the specified listener.

modifyUrl

(
  • url
)

Defined in modifyUrl:252

Modifies a url, replacing a specified value with the current language.

Parameters:

  • url String

    The url to modify to a language specific version.

off

(
  • name
  • callback
)
EventDispatcher

Remove the event listener

Parameters:

  • name String*

    The type of event string separated by spaces, if no name is specifed remove all listeners.

  • callback Function | Array*

    The listener function or collection of callback functions

Returns:

EventDispatcher:

Return this EventDispatcher for chaining calls.

on

(
  • name
  • callback
  • [priority=0]
)
EventDispatcher

Add an event listener. The parameters for the listener functions depend on the event.

Parameters:

  • name String | Object

    The type of event (can be multiple events separated by spaces), or a map of events to handlers

  • callback Function | Array*

    The callback function when event is fired or an array of callbacks.

  • [priority=0] Int optional

    The priority of the event listener. Higher numbers are handled first.

Returns:

EventDispatcher:

Return this EventDispatcher for chaining calls.

once

(
  • name
  • callback
  • [priority=0]
)
EventDispatcher

Add an event listener but only handle it one time.

Parameters:

  • name String | Object

    The type of event (can be multiple events separated by spaces), or a map of events to handlers

  • callback Function | Array*

    The callback function when event is fired or an array of callbacks.

  • [priority=0] Int optional

    The priority of the event listener. Higher numbers are handled first.

Returns:

EventDispatcher:

Return this EventDispatcher for chaining calls.

setConfig

(
  • config
)

Defined in setConfig:86

Configure

Parameters:

  • config Object

    The language settings to be used.

    • default String

      The default language name to use if asked for one that is not present.

    • languages Array

      An array of all supported languages, with entries being locale ids (dialects allowed). Locale ids should be lower case.

    • [replace="%LANG%"] String optional

      A string to replace in urls with the current language.

setLanguage

(
  • languageList
)

Defined in setLanguage:170

Sets the current language, based on specified preferences and what is available.

Parameters:

  • languageList Array | String

    The list of preferred languages in order of preference, or a single language.

setStringTable

(
  • dictionary
)

Defined in setStringTable:213

Sets the string table for later reference.

Parameters:

  • dictionary Dictionary

    The string table, with keys that you would use to reference the translations.

trigger

(
  • type
  • arguments
)

Dispatch an event

Parameters:

  • type String

    The type of event to trigger

  • arguments

    Additional parameters for the listener functions.

type

(
  • value
)
String
private

Return type of the value.

Parameters:

  • value

Returns:

String:

The type

_current

String private

Defined in _current:47

The current language.

_default

String private

Defined in _default:54

The default language.

_destroyed

Boolean protected

If the dispatcher is destroyed

_listeners

Object private

The collection of listeners

_replace

String private

Defined in _replace:39

The value to replace with the current language in URLS.

Default: "%LANG%"

_stringTable

Dictionary private

Defined in _stringTable:68

A dictionary of translated strings, set with setStringTable().

current

String public

Defined in current:132

The current language.

destroyed

Boolean

If the dispatcher is destroyed

instance

springroll.Languages public static

Defined in instance:117

Get the singleton instance of the Languages object.

languages

Array public

Defined in languages:61

Available languages.

changed

Defined in changed:80

Fired when the chosen language has changed.

Event Payload:

  • language String

    The newly chosen language.