Custom Java Scripts

The configuration keys ‘js-conf’ and ‘js-exec’ provide an opportunity to customize the java script on admin pages. Scripts defined via ‘js-conf’ run before the global object qTranslateX is constructed and provide opportunity to alter associative array qTranslateConfig, which is used to construct object qTranslateX. Oppositely, scripts defined by ‘js-exec’ tokens run when object qTranslateX already exists, providing opportunity to alter its functionality.

Object qTranslateX holds and maintain all Language Switching Buttons (LSB) and all multilingual fields hooked. When an LSB is pressed, the current content of the multilingual fields is stashed away into an internal storage, and the values for new language are fetched from internal storage and applied. You may supply a code to execute before and/or after the language switch is executed on each LSB click.

Below is the list of provided java interface functions:

  • getLanguages() – returns an array keyed by two-letter language code. Example of usage:
    var langs = getLanguages();
    for(var lang_code in langs) {
    var lang_conf = langs[lang_code];
    // variables available:
    //lang_conf.name
    //lang_conf.flag
    //lang_conf.locale
    // and may be more properties later
    }
    
  • isLanguageEnabled(lang) – return true if two-letter code ‘lang’ defines an enabled language.
  • getFlagLocation() – returns URL to folder with flag images.
  • addLanguageSwitchBeforeListener(func) – the function passed will be called when user presses one of the Language Switching Buttons before the content of all fields hooked is replaced with an appropriate language. Two arguments are supplied:
    • two-letter language code of currently active language from which the edit language is being switched.
    • the language code to which the edit language is being switched.

    The value of “this” is set to the only global instance of qTranslateX object.

  • addLanguageSwitchAfterListener(func) – the function passed will be called when user presses one of the Language Switching Buttons after the content of all fields hooked is replaced with an appropriate language. Two arguments are supplied:
    • two-letter language code of active language to which the edit language is already switched.
    • the language code from which the edit language is being switched.

    The value of “this” is set to the only global instance of qTranslateX object.

  • enableLanguageSwitchingButtons(on)  – show/hide all the Language Switching Buttons on the page.

All functions are defined in qTranslateConfig.qtx object and can be called like:

qTranslateConfig.qtx.addLanguageSwitchBeforeListener(function(lang_from, lang_to){ your code here;});

Integrating plugin WPBakery Visual Composer & qTranslate-X may be looked at as an example example of ‘js-exec’ usage.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s