Javascript functions
All functions you can call in your Javascript code
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): Promise<Array<String>>

Parameters
Response
Example
  • payload(Object):
    • words(Array)
    • languageTo(String): translation destination language code
  • callback(Function)
Promise containing translated strings
1
Weglot.translate(
2
{
3
'words':[ { "t":1,"w": "Red" } ],
4
'languageTo':'fr'
5
}
6
, function(data) { console.log(data) }
7
);
Copied!

Weglot.getCurrentLang(): String

Parameters
Response
None
String: the ISO 639-1 2-letter code of the current language on the page

Weglot.getLanguageName(code): String

Parameters
Response
String: the local name of the language, as defined by ISO 639-1
For example, calling Weglot.getLanguageName("es") returns "Español"

Weglot.getBestAvailableLanguage(): String

Parameters
Response
None
  • code(String): the ISO 639-1 2-letter code of the best available language
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

Parameters
Response
  • 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
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

Weglot.switchTo(code): void

Parameters
Response
  • 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
Nothing

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

This is called right after a language has changed on the page
1
Weglot.on("languageChanged", callbackFunction)
Copied!
The callback function will be called with two optional arguments:
  1. 1.
    newLanguage (String): the 2-letter code of the language the page changed to
  2. 2.
    previousLanguage (String): the 2-letter code of the previous language of the page
Example:
1
Weglot.on("languageChanged", function(newLang, prevLang) {
2
console.log("The language on the page just changed to (code): " + newLang)
3
console.log("The full name of the language is: " + Weglot.getLanguageName(newLang))
4
})
Copied!

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
1
Weglot.on("initialized", callbackFunction)
Copied!
The callback function will be called with no argument.
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

switchersReady

This is called right after the switchers have been created.
1
Weglot.on("switchersReady", callbackFunction)
Copied!
The callback function will be called with one optional argument: initialLanguage
Example:
1
Weglot.on("switchersReady", function(initialLanguage) {
2
console.log("the switchers are ready, I can tweak them")
3
})
Copied!

Weglot.off(eventName, callback): Boolean

With Weglot.off, you can unsubscribe the events you subscribed to with Weglot.on
Parameters
Response
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
true if one or more events have been unregistered
false otherwise

Weglot.initialize(options): void

Called to initialize Weglot. See Initialization code
Last modified 3mo ago