Skip to content

eo-translatorjs


eo-translatorjs / EOTranslator

Class: EOTranslator ​

Defined in: index.ts:6

Description ​

The translator class — manages a multilingual dictionary and provides methods to translate strings and DOM elements.

Constructors ​

Constructor ​

ts
new EOTranslator(dict, lang?): EOTranslator;

Defined in: index.ts:110

Parameters ​

ParameterTypeDefault valueDescription
dictTDictionaryundefinedThe translation dictionary
lang?string""The default language key. When omitted, the first of the document's lang attribute (in a browser) and "en" that the dictionary has is used, then the dictionary's first language. With an empty dictionary, the document's lang attribute or "en" is used.

Returns ​

EOTranslator

Description ​

Instantiates a translator object.

Throws ​

When the dictionary is not a valid plain object

Throws ​

When the language is not a string

Throws ​

When the language key does not exist in a non-empty dictionary

Accessors ​

dictionary ​

Get Signature ​

ts
get dictionary(): TDictionary;

Defined in: index.ts:45

Description ​

Gets the current translation dictionary.

Returns ​

TDictionary

The current dictionary

Set Signature ​

ts
set dictionary(dict): void;

Defined in: index.ts:27

Description ​

Sets the translation dictionary.

The dictionary must be a plain object whose top-level keys are language codes and whose values are plain objects mapping translation keys to translated strings (or further nested objects for namespaced keys).

When the new dictionary lacks the active language, the language falls back to the default one, picked as in the constructor.

Throws ​

When the value is not a valid plain object

Parameters ​
ParameterTypeDescription
dictTDictionaryThe new dictionary value
Returns ​

void


language ​

Get Signature ​

ts
get language(): string;

Defined in: index.ts:78

Description ​

Gets the active language key.

Returns ​

string

The current language

Set Signature ​

ts
set language(lang): void;

Defined in: index.ts:60

Description ​

Sets the active language.

The language must be a string that exists as a top-level key in the dictionary, unless the dictionary is empty or the string itself is empty.

Throws ​

When the value is not a string

Throws ​

When the language key does not exist in the dictionary

Parameters ​
ParameterTypeDescription
langstringThe new language value
Returns ​

void


languages ​

Get Signature ​

ts
get languages(): string[];

Defined in: index.ts:88

Description ​

Gets the list of all language keys present in the dictionary.

Returns ​

string[]

The list of available language keys

Methods ​

translate() ​

ts
translate(input?, options?): string;

Defined in: index.ts:174

Parameters ​

ParameterTypeDefault valueDescription
input?string""The translation key (supports dot notation)
options?TTranslateOptions{}Translation options

Returns ​

string

The translated string, or the fallback value

Description ​

Translates a key against the active (or specified) language.

Keys may be dot-separated to access nested translations (e.g. "section.title"). When no matching translation is found the fallback value is returned — which defaults to the key itself.

Throws ​

When the key is not a string


translateElement() ​

ts
translateElement(DOMElement?, lang?): void;

Defined in: index.ts:206

Parameters ​

ParameterTypeDescription
DOMElement?Element | nullThe DOM element to translate
lang?stringLanguage override for this call

Returns ​

void

Description ​

Translates the content of a single DOM element.

The element must carry the eo-translator attribute whose value is the translation key. Optional companion attributes:

AttributeDescription
eo-translator(required) The translation key. When empty, the element's content is used and stored here
eo-translator-fallbackFallback text when the key is not found
eo-translator-paramsJSON object of interpolation parameters
eo-translator-html"true" to set innerHTML instead of textContent

translateDOM() ​

ts
translateDOM(DOMContainer?, lang?): void;

Defined in: index.ts:260

Parameters ​

ParameterTypeDescription
DOMContainer?ParentNode | nullThe root container to search within
lang?stringLanguage override for this call

Returns ​

void

Description ​

Translates all [eo-translator] elements inside a container, and the container itself when it carries the attribute.

When no container is provided, the entire document is used (browser only).


add() ​

ts
add(
   lang, 
   key, 
   translation
): void;

Defined in: index.ts:289

Parameters ​

ParameterTypeDescription
langstringThe language key to add the translation to
keystringThe translation key (supports dot notation)
translationstringThe translated string

Returns ​

void

Description ​

Adds or updates a translation entry in the dictionary.

Supports dot-separated keys for nested entries (e.g. "section.title" will create { section: { title: value } }). If the language does not yet exist in the dictionary it is created.

Throws ​

When the language or the key is not a string


remove() ​

ts
remove(lang, key): void;

Defined in: index.ts:320

Parameters ​

ParameterTypeDescription
langstringThe language key to remove the translation from
keystringThe translation key to remove (supports dot notation)

Returns ​

void

Description ​

Removes a translation entry from the dictionary.

Supports dot-separated keys for nested entries. Does nothing if the language or key does not exist.

Throws ​

When the language or the key is not a string


isValidLanguage() ​

ts
isValidLanguage(lang): boolean;

Defined in: index.ts:340

Parameters ​

ParameterTypeDescription
langstringThe language key to check

Returns ​

boolean

true when the language exists in the dictionary

Description ​

Checks whether a language key is present in the dictionary.

Released under the MIT License.