# Javascript functions

The JS library exposes functions that you can use anywhere in your code. (just make sure the library is already loaded when calling one of these functions)

### Weglot.translate(payload, callback)

{% tabs %}
{% tab title="Parameters" %}

* payload(Object):
  * words(Array)
    * t(Integer): word type, see [word types reference](/api/reference.md#wordtype)
    * w(String): string to translate
  * languageTo(String): translation destination language code
* callback(Function)
  {% endtab %}

{% tab title="Response" %}
Promise containing translated strings
{% endtab %}

{% tab title="Example" %}

```javascript
Weglot.translate(
    {
    'words':[ { "t":1,"w": "Red" } ],
    'languageTo':'fr'
    } 
    , function(data) { console.log(data) }
);
```

{% endtab %}
{% endtabs %}

### Weglot.getCurrentLang(): String

{% tabs %}
{% tab title="Parameters" %}
None
{% endtab %}

{% tab title="Response" %}
String: the ISO 639-1 2-letter code of the current language on the page
{% endtab %}
{% endtabs %}

### Weglot.getLanguageName(code): String

{% tabs %}
{% tab title="Parameters" %}

* code(String): the ISO 639-1 2-letter code of [a language supported by Weglot](https://weglot.com/documentation/available-languages)
  {% endtab %}

{% tab title="Response" %}
String: the local name of the language, as defined by ISO 639-1

For example, calling `Weglot.getLanguageName("es")` returns `"Español"`
{% endtab %}
{% endtabs %}

### Weglot.getBestAvailableLanguage(): String <a href="#weglot-getlanguagename-code" id="weglot-getlanguagename-code"></a>

{% tabs %}
{% tab title="Parameters" %}
None
{% endtab %}

{% tab title="Response" %}

* code(String): the ISO 639-1 2-letter code of the best available language
  {% endtab %}
  {% endtabs %}

This function checks the visitors preferred languages, and finds the best match among the languages you support on Weglot. It's the function used internally when you use the `autoSwitch` function

### Weglot.search(term, callback): Boolean

{% tabs %}
{% tab title="Parameters" %}

* term(String): Any term written in the current language of the page.
* callback(Function): A function that will be called whenever the search term in the original language is available. Takes one String argument
  {% endtab %}

{% tab title="Response" %}
`true` if there is a search to be made (the current language of the page differs from the original language)

`false` if there is no search to be made. In this case the callback is called immediately with the original term
{% endtab %}
{% endtabs %}

### Weglot.switchTo(code): void

{% tabs %}
{% tab title="Parameters" %}

* code(String): the ISO 639-1 2-letter code of a language you're supporting on your website

  code is either **originalLanguage** or one of the **destinationLanguages**
  {% endtab %}

{% tab title="Response" %}
Nothing
{% endtab %}
{% endtabs %}

### Weglot.on(eventName, callback)

You can subscribe your own code to Weglot-specific events. When these events occur, the callback function you have defined will be called.

#### languageChanged (only JS integration)

This is called right after a language has changed on the page

```javascript
Weglot.on("languageChanged", callbackFunction)
```

The callback function will be called with two optional arguments:

1. newLanguage (String): the 2-letter code of the language the page changed to
2. previousLanguage (String): the 2-letter code of the previous language of the page

Example:

```javascript
Weglot.on("languageChanged", function(newLang, prevLang) {
    console.log("The language on the page just changed to (code): " + newLang)
    console.log("The full name of the language is: " + Weglot.getLanguageName(newLang))
})
```

{% hint style="warning" %}
This event is only available with our Javascript integration, not with subdomain or subdirectory integration.
{% endhint %}

#### initialized

This is called right after the call to **Weglot.initialize(options)** has been successful, but before the switchers are created and the page is translated

```javascript
Weglot.on("initialized", callbackFunction)
```

The callback function will be called with no argument.

{% hint style="info" %}
If you register your callback **after** Weglot has been initialized, this will have no effect.

To check whether Weglot is already initialized or not, you can read the boolean `Weglot.initialized`
{% endhint %}

#### switchersReady

This is called right after the switchers have been created.

```javascript
Weglot.on("switchersReady", callbackFunction)
```

The callback function will be called with one optional argument: `initialLanguage`

Example:

```javascript
Weglot.on("switchersReady", function(initialLanguage) {
  console.log("the switchers are ready, I can tweak them")
})
```

### Weglot.off(eventName, callback): Boolean

With `Weglot.off`, you can unsubscribe the events you subscribed to with `Weglot.on`

{% tabs %}
{% tab title="Parameters" %}
eventName(String): **mandatory field**, the name of the event you want to unsubscribe from

callbackFunction(Function): **optional field**, if you'd like to target a specific function
{% endtab %}

{% tab title="Response" %}
**true** if one or more events have been unregistered

**false** otherwise
{% endtab %}
{% endtabs %}

### Weglot.initialize(options): void

Called to initialize Weglot. See [Initialization code](/javascript/javascript.md#initialization-code)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.weglot.com/javascript/javascript-functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
