Lang

core. Lang

Handles loading and translation of language strings.

Constructor

new Lang(options)

Source:
Parameters:
Name Type Description
options Object
Properties
Name Type Attributes Description
dependencies Object Key/value map of dependency configs (each with a rootDir)
defaultLang String The default language for translations
rootDir String The application root directory
log function <optional>
Optional logging function (level, id, ...args)

Members

defaultLang :String

Description:
  • The default language for translations
Source:
The default language for translations
Type:
  • String

log :function

Description:
  • Optional logging function (level, id, ...args)
Source:
Optional logging function (level, id, ...args)
Type:
  • function

phrases :Object

Description:
  • The loaded language phrases
Source:
The loaded language phrases
Type:
  • Object

supportedLanguages :Array.<String>

Description:
  • Returns the languages supported by the application
Source:
Returns the languages supported by the application
Type:
  • Array.<String>

Methods

loadPhrases(dependencies, appRootDir, logopt)

Description:
  • Loads and merges all language phrases from dependencies
Source:
Parameters:
Name Type Attributes Description
dependencies Object Key/value map of dependency configs (each with a rootDir)
appRootDir String The application root directory
log function <optional>
Optional logging function (level, id, ...args)

substituteData(s, lang, data) → {String}

Description:
  • Replaces placeholders in a translated string with data values. Supports ${key} for simple substitution, and $map{key:attrs:delim} for mapping over array values.
Source:
Parameters:
Name Type Description
s String The translated string
lang String The target language
data Object Key/value pairs to substitute
Returns:
Type
String

translate(lang, key, data) → {String}

Description:
  • Returns translated language string. If key is an Error, translates using the error code as the key and error data for substitution. Non-Error, non-string values are returned unchanged.
Source:
Parameters:
Name Type Description
lang String The target language (falls back to defaultLang)
key String | Error The unique string key, or an Error to translate
data Object Dynamic data to be inserted into translated string
Returns:
Type
String