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
new EOTranslator(dict, lang?): EOTranslator;Defined in: index.ts:110
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
dict | TDictionary | undefined | The 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
get dictionary(): TDictionary;Defined in: index.ts:45
Description
Gets the current translation dictionary.
Returns
The current dictionary
Set Signature
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
| Parameter | Type | Description |
|---|---|---|
dict | TDictionary | The new dictionary value |
Returns
void
language
Get Signature
get language(): string;Defined in: index.ts:78
Description
Gets the active language key.
Returns
string
The current language
Set Signature
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
| Parameter | Type | Description |
|---|---|---|
lang | string | The new language value |
Returns
void
languages
Get Signature
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()
translate(input?, options?): string;Defined in: index.ts:174
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
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()
translateElement(DOMElement?, lang?): void;Defined in: index.ts:206
Parameters
| Parameter | Type | Description |
|---|---|---|
DOMElement? | Element | null | The DOM element to translate |
lang? | string | Language 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:
| Attribute | Description |
|---|---|
eo-translator | (required) The translation key. When empty, the element's content is used and stored here |
eo-translator-fallback | Fallback text when the key is not found |
eo-translator-params | JSON object of interpolation parameters |
eo-translator-html | "true" to set innerHTML instead of textContent |
translateDOM()
translateDOM(DOMContainer?, lang?): void;Defined in: index.ts:260
Parameters
| Parameter | Type | Description |
|---|---|---|
DOMContainer? | ParentNode | null | The root container to search within |
lang? | string | Language 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()
add(
lang,
key,
translation
): void;Defined in: index.ts:289
Parameters
| Parameter | Type | Description |
|---|---|---|
lang | string | The language key to add the translation to |
key | string | The translation key (supports dot notation) |
translation | string | The 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()
remove(lang, key): void;Defined in: index.ts:320
Parameters
| Parameter | Type | Description |
|---|---|---|
lang | string | The language key to remove the translation from |
key | string | The 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()
isValidLanguage(lang): boolean;Defined in: index.ts:340
Parameters
| Parameter | Type | Description |
|---|---|---|
lang | string | The 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.