Home Reference Source
import Cocorita from 'cocorita/src/cocorita.js'
public class | source

Cocorita

Extends:

events~EventEmitter → Cocorita

Cocorita class

Constructor Summary

Public Constructor
public

Constructor

Member Summary

Public Members
public

Translations object

public

Normally, when a target language translation is found missing, an empty string is emitted.

public

Array of target languages to be initialized if a translation is missing

public

Target language

public set

Set target language id

public get

get target language id

Method Summary

Public Methods
public

dump(format: String): String

Get translations data serialized to json or yaml format

public

Get a copy of translations data object

public

load(source: String | Object, format: String)

Load translations data from a database file or a translations object

public

tr(source: String, replaces: Object): String

Translate a string of source text in the target language.

Public Constructors

public constructor(options: Object): Cocorita source

Constructor

Params:

NameTypeAttributeDescription
options Object

Options object

options.language String
  • optional

The target language identifier

options.initialize String[]
  • optional

Array of languages whose translations will be initialized with the source text if not present

Return:

Cocorita

A new Cocorita instance

Throw:

Error

Throws a Error if something gone wrong

Public Members

public data: Object source

Translations object

public defaultTarget: * source

Normally, when a target language translation is found missing, an empty string is emitted. If defaultTarget value is set to 'source', the source text is emitted instead.

public initialize: String[] source

Array of target languages to be initialized if a translation is missing

public lang: String source

Target language

public set language: String source

Set target language id

public get language: String source

get target language id

Public Methods

public dump(format: String): String source

Get translations data serialized to json or yaml format

Params:

NameTypeAttributeDescription
format String

'json' or 'yaml'

Return:

String

Serialized translations data

Throw:

Error

public getData(): Object source

Get a copy of translations data object

Return:

Object

Translations data object

public load(source: String | Object, format: String) source

Load translations data from a database file or a translations object

Params:

NameTypeAttributeDescription
source String | Object

A json or yaml string or a valid translations object

format String
  • optional

'json' or 'yaml'. Required only if source is a file path.

Throw:

Error

public tr(source: String, replaces: Object): String source

Translate a string of source text in the target language.
If the translation is not available in the database the method return a blank string.
If the initialize option was passed to the constructor, then this method will check all the target languages translations for the source text and will initialize them with a blank string were are not available.

Params:

NameTypeAttributeDescription
source String

The source text

replaces Object

Replaces map object

Return:

String

Translated text in current target language

Emit:

EVT_INIT_KEY

Emitted if target translation does not exists in data base

Throw:

Error