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)
payload(Object):
words(Array)
t(Integer): word type, see word types reference
w(String): string to translate
languageTo(String): translation destination language code
callback(Function)
Weglot.getCurrentLang(): String
None
Weglot.getLanguageName(code): String
code(String): the ISO 639-1 2-letter code of a language supported by Weglot
Weglot.getBestAvailableLanguage(): String
None
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
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
Weglot.switchTo(code): void
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
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
The callback function will be called with two optional arguments:
newLanguage (String): the 2-letter code of the language the page changed to
previousLanguage (String): the 2-letter code of the previous language of the page
Example:
This event is only available with our Javascript integration, not with subdomain or subdirectory integration.
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
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.
The callback function will be called with one optional argument: initialLanguage
Example:
Weglot.off(eventName, callback): Boolean
With Weglot.off
, you can unsubscribe the events you subscribed to with Weglot.on
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
Weglot.initialize(options): void
Called to initialize Weglot. See Initialization code
Last updated