Weglot
WebsiteGithubSlack
  • Developer Documentation
  • WordPress
    • Getting Started
    • Helpers Functions
    • Hooks
      • Translations Hooks
      • Other Hooks
    • Use cases
      • Lang attribute
      • Translate link
      • Implementing Custom Link Translation
      • Wp-rocket Lazyload
      • Deactivate Weglot on Elementor
      • Weglot translate on Elementor
      • Language Selector Styling
      • Change flag size
      • Exclude a Custom Post Type
      • Exclude the search page
      • Exclude draft or private status post
      • Plugin Woocommerce: Translate all email sent to customers
      • Plugin WooCommerce: Translate Product URLs
      • Use custom language code (URL, lang and hreflang attribute)
      • Theme OceanWP: Use language selector in the main menu
      • Theme Avada: Use language selector in the main menu
      • LinkedIn Share Post: Translate oEmbed WP URL
      • Hide button on excluded page
      • Auto switch only for HomePage
      • Proxify Url
      • Translate pdf for Woocommerce pdf invoice
      • Translate pdf with Gravity pdf
      • Translate Dublin core meta (or other meta)
      • How to not translate specific ajax action
      • How to get translated url programmatically
      • How to set WP locale based on Weglot current language
      • How to translate mail from Photo Reviews for WooCommerce
      • How to translate schema from schema.org generate by Yoast plugin
      • How to translate email sending by plugin WP Mail SMTP
      • How to hide the Weglot admin button for non administrator roles
      • Exemple of snippet to translate search from JetSearch plugin
      • Example of snippet to translate your checkout element (override/added by the Funnel Builder plugin)
      • LScache
      • How to fix live builder issue with Weglot
      • Add translated url to Rankmath sitemap index
      • Advanced Woo Search X Weglot
      • Conditionally Disabling Weglot Translation for Specific URLs
    • Weglot filters
      • weglot_translate_email
      • weglot_cancel_init
      • weglot_translate_email_languages_forced
      • weglot_menu_parent_item_title
      • weglot_active_current_menu_item
      • weglot_ajax_no_translate
      • weglot_active_translation_before_process
      • weglot_active_translation_before_treat_page
      • weglot_debug_file
      • weglot_autoredirect_only_home
      • weglot_autoredirect_skip
      • weglot_remove_google_translate
      • weglot_add_hreflang
      • weglot_get_replace_modify_link
      • get_replace_modify_link_in_xml
      • weglot_get_flag_class
      • weglot_get_name_with_language_entry
      • weglot_get_class_dropdown
      • weglot_button_html
      • weglot_get_dom_checkers
      • weglot_replace_div_id
      • weglot_replace_weglot_menu
      • weglot_render_default_button
      • weglot_render_switcher_editor_button
      • weglot_href_lang
      • weglot_get_options_from_cdn_cache
      • weglot_get_options_from_cdn_cache_duration
      • weglot_get_slugs_from_cache
      • weglot_get_slugs_cache_duration
      • weglot_exclude_blocks
      • weglot_exclude_urls
      • weglot_get_parser_ignored_nodes
      • weglot_navigator_language
      • weglot_url_auto_redirect
      • weglot_replace_url
      • weglot_replace_link
      • weglot_ajax_replace_urls
      • weglot_proxify_urls
      • weglot_add_json_keys
      • weglot_json_treat_page
      • weglot_html_treat_page
      • Copy of weglot_xml_treat_page
      • weglot_render_dom
      • weglot_default_current_language_empty
    • Algolia integration
  • Proxy
    • Headers
  • Javascript
    • Getting Started
    • Options
    • Javascript functions
    • Language link Hooks
    • Translate iframe
    • Advanced concepts
      • Translation engines
  • CMS Specific
    • Shopify
  • API
    • Reference
  • Cookies
Powered by GitBook
On this page
  • Weglot.translate(payload, callback)
  • Weglot.getCurrentLang(): String
  • Weglot.getLanguageName(code): String
  • Weglot.getBestAvailableLanguage(): String
  • Weglot.search(term, callback): Boolean
  • Weglot.switchTo(code): void
  • Weglot.on(eventName, callback)
  • Weglot.off(eventName, callback): Boolean
  • Weglot.initialize(options): void
  1. Javascript

Javascript functions

All functions you can call in your Javascript code

PreviousOptionsNextLanguage link Hooks

Last updated 2 years ago

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

      • w(String): string to translate

    • languageTo(String): translation destination language code

  • callback(Function)

Promise containing translated strings

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

Weglot.getCurrentLang(): String

None

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

Weglot.getLanguageName(code): String

  • code(String): the ISO 639-1 2-letter code of

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

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

  • 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

  • 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 (only JS integration)

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

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:

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))
})

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

Weglot.on("initialized", callbackFunction)

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.

Weglot.on("switchersReady", callbackFunction)

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

Example:

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

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

word types reference
a language supported by Weglot
Initialization code